summaryrefslogtreecommitdiffstats
path: root/src/daemon
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-26 15:58:13 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-26 15:58:13 +0300
commit81602e816f9d1e9ae0fa06482037e79bb8750806 (patch)
tree5cb4bb90fa9f6e1042077c2171b34eb07d7db436 /src/daemon
parent9e3e2a9d886f8191bccc69ccc356f683e4a0c561 (diff)
downloadusurpation-81602e816f9d1e9ae0fa06482037e79bb8750806.tar.gz
usurpation-81602e816f9d1e9ae0fa06482037e79bb8750806.tar.bz2
usurpation-81602e816f9d1e9ae0fa06482037e79bb8750806.zip
daemon: improve threading logic on null proto.
Functions now prevent interruptions that can produce an inconsistent state in the program. Also, cosmetic changes. Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/daemon')
-rw-r--r--src/daemon/proto_stdio.c116
-rw-r--r--src/daemon/proto_stdio_private.h2
2 files changed, 77 insertions, 41 deletions
diff --git a/src/daemon/proto_stdio.c b/src/daemon/proto_stdio.c
index 533dc76..f0a22a6 100644
--- a/src/daemon/proto_stdio.c
+++ b/src/daemon/proto_stdio.c
@@ -26,59 +26,93 @@
void message_receive(char *arg)
{
- int done = 0;
- while(!done)
- {
- pthread_mutex_lock(&state.out_m);
- if(!state.writebuf)
- {
- state.writebuf = strdup(arg);
- done = 1;
- }
- pthread_mutex_unlock(&state.out_m);
- }
+ int cancelstate;
+ int done = 0;
+
+ while (!done) {
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
+ pthread_mutex_lock(&state.out_m);
+ if (!state.writebuf) {
+ state.writebuf = strdup(arg);
+ done = 1;
+ }
+ pthread_mutex_unlock(&state.out_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+ }
}
-char * message_send(void)
+char *message_send(void)
{
- char * ret;
- pthread_mutex_lock(&state.out_m);
- ret = strdup(state.writebuf);
- free(state.writebuf);
- state.writebuf = NULL;
- pthread_mutex_unlock(&state.out_m);
- return ret;
+ char *ret;
+ int cancelstate;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
+ pthread_mutex_lock(&state.out_m);
+ ret = strdup(state.readbuf);
+ free(state.readbuf);
+ state.readbuf = NULL;
+ state.readbufsize = 0;
+ pthread_mutex_unlock(&state.out_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+
+ return ret;
}
static void *read_stdin(void *arg)
{
- while(1)
- {
- pthread_mutex_lock(&state.in_m);
- if(!state.readbuf)
- {
- state.readbufsize = getline(&state.readbuf,NULL, stdin);
- }
- pthread_mutex_unlock(&state.in_m);
- }
- return NULL;
+ int cancelstate;
+
+ (void) arg;
+
+ while(1) {
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
+ pthread_mutex_lock(&state.in_m);
+
+ if (!state.readbuf) {
+ state.readbufsize = getline(&state.readbuf, NULL, stdin);
+ }
+
+ pthread_mutex_unlock(&state.in_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+ nanosleep(&respite, NULL);
+ }
+
+ return NULL;
}
static void *write_stdout(void *arg)
{
- while(1)
- {
- pthread_mutex_lock(&state.out_m);
- if(state.writebuf)
- {
- printf("%s", state.writebuf);
- free(state.writebuf);
- state.writebuf = NULL;
- }
- pthread_mutex_unlock(&state.out_m);
- }
- return NULL;
+ int cancelstate;
+
+ (void) arg;
+
+ while(1) {
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
+ pthread_mutex_lock(&state.out_m);
+
+ if (state.writebuf) {
+ if (state.writebuf[strlen(state.writebuf) - 1] == '\n') {
+ printf("%s", state.writebuf);
+ } else {
+ printf("%s\n", state.writebuf);
+ }
+
+ free(state.writebuf);
+ state.writebuf = NULL;
+ state.writebufsize = 0;
+ }
+
+ pthread_mutex_unlock(&state.out_m);
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
+ pthread_testcancel();
+ nanosleep(&respite, NULL);
+ }
+
+ return NULL;
}
diff --git a/src/daemon/proto_stdio_private.h b/src/daemon/proto_stdio_private.h
index 4b0ab6f..209d191 100644
--- a/src/daemon/proto_stdio_private.h
+++ b/src/daemon/proto_stdio_private.h
@@ -42,4 +42,6 @@ static struct state {
static void *read_stdin(void *arg);
static void *write_stdout(void *arg);
+static const struct timespec respite = {0, 10 * 1000 * 1000}; /* 10ms */
+
#endif /* USURPATION_PROTO_STDIO_PRIVATE_H */