From: Shuanglei Tao Date: Mon, 20 Aug 2018 05:33:17 +0000 (+0800) Subject: protocol: replace sleep with pthread cond X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=176f3e18a39dfb5705aa5b9c75d16730f12d409d;p=ttyd.git protocol: replace sleep with pthread cond --- diff --git a/src/protocol.c b/src/protocol.c index 1968b80..6862b4e 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -199,9 +199,7 @@ thread_run_command(void *args) { while (client->running) { pthread_mutex_lock(&client->mutex); if (client->state == STATE_READY) { - pthread_mutex_unlock(&client->mutex); - usleep(5); - continue; + pthread_cond_wait(&client->cond, &client->mutex); } memset(client->pty_buffer, 0, sizeof(client->pty_buffer)); client->pty_len = read(pty, client->pty_buffer + LWS_PRE + 1, BUF_SIZE); @@ -255,6 +253,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, client->state = STATE_INIT; client->pty_len = 0; pthread_mutex_init(&client->mutex, NULL); + pthread_cond_init(&client->cond, NULL); lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), client->hostname, sizeof(client->hostname), client->address, sizeof(client->address)); diff --git a/src/server.c b/src/server.c index d3fc2d0..4ec5919 100644 --- a/src/server.c +++ b/src/server.c @@ -475,6 +475,8 @@ main(int argc, char **argv) { pthread_mutex_lock(&client->mutex); if (client->state != STATE_DONE) lws_callback_on_writable(client->wsi); + else + pthread_cond_signal(&client->cond); pthread_mutex_unlock(&client->mutex); } } diff --git a/src/server.h b/src/server.h index cc73b91..95e44ff 100644 --- a/src/server.h +++ b/src/server.h @@ -46,6 +46,7 @@ struct tty_client { ssize_t pty_len; pthread_t thread; pthread_mutex_t mutex; + pthread_cond_t cond; LIST_ENTRY(tty_client) list; };