From 81602e816f9d1e9ae0fa06482037e79bb8750806 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Sun, 26 May 2019 15:58:13 +0300 Subject: 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 --- src/daemon/proto_stdio.c | 116 ++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 41 deletions(-) (limited to 'src/daemon/proto_stdio.c') 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; } -- cgit v1.2.3