]> prime8.dev >> repos - ttyd.git/commitdiff
protocol: replace sleep with pthread cond
authorShuanglei Tao <tsl0922@gmail.com>
Mon, 20 Aug 2018 05:33:17 +0000 (13:33 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Mon, 20 Aug 2018 05:33:17 +0000 (13:33 +0800)
src/protocol.c
src/server.c
src/server.h

index 1968b80c6bda83130dd49fe54d49b286a1a578cb..6862b4e94c7d938becb2cb4e29fa9ee12fc2f0e7 100644 (file)
@@ -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));
index d3fc2d0e64475692ec323c091412d156c6850520..4ec5919c9bb39c1d20d247551604eed807303659 100644 (file)
@@ -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);
                 }
             }
index cc73b91c430444bde509f561be62b4258e9efbc9..95e44ff4c5db5faf7f145e113ebae727de7c3df4 100644 (file)
@@ -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;
 };