diff options
author | 2019-05-29 13:03:49 +0300 | |
---|---|---|
committer | 2019-05-29 13:03:49 +0300 | |
commit | 187fe61700fc97fff565ec53aac65c664042feae (patch) | |
tree | 498d0c00af96c4f666b4ef4dbb6db0966a485852 /src/daemon/proto_stdio.c | |
parent | aef4eba5572d6b42f8ef2913ec41c0e778731960 (diff) | |
download | usurpation-187fe61700fc97fff565ec53aac65c664042feae.tar.gz usurpation-187fe61700fc97fff565ec53aac65c664042feae.tar.bz2 usurpation-187fe61700fc97fff565ec53aac65c664042feae.zip |
daemon: get ready to use messaging interfaces.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
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); } |