diff options
Diffstat (limited to 'src/daemon/proto_stdio.c')
-rw-r--r-- | src/daemon/proto_stdio.c | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/daemon/proto_stdio.c b/src/daemon/proto_stdio.c index f0a22a6..9aecdf0 100644 --- a/src/daemon/proto_stdio.c +++ b/src/daemon/proto_stdio.c @@ -27,32 +27,31 @@ void message_receive(char *arg) { 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(); - } + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate); + pthread_mutex_lock(&state.out_m); + state.writebuf = strdup(arg); + pthread_mutex_unlock(&state.out_m); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate); + pthread_testcancel(); + nanosleep(&respite, NULL); } char *message_send(void) { - char *ret; + char *ret = NULL; 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; + + if (state.readbuf) { + 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(); @@ -140,12 +139,16 @@ int proto_stdio_init(void) void proto_stdio_close(void) { - if (pthread_mutex_trylock(&state.mutex) == EBUSY) { + int status; + + status = pthread_mutex_trylock(&state.mutex); + + if (status == EBUSY) { pthread_cancel(state.stdio_in); pthread_cancel(state.stdio_out); pthread_join(state.stdio_in, NULL); pthread_join(state.stdio_out, NULL); + } else if (!status) { + pthread_mutex_unlock(&state.mutex); } - - pthread_mutex_unlock(&state.mutex); } |