protocol: replace sleep with pthread cond

This commit is contained in:
Shuanglei Tao
2018-08-20 13:33:17 +08:00
parent d1d5336af1
commit 176f3e18a3
3 changed files with 5 additions and 3 deletions

View File

@@ -199,9 +199,7 @@ thread_run_command(void *args) {
while (client->running) { while (client->running) {
pthread_mutex_lock(&client->mutex); pthread_mutex_lock(&client->mutex);
if (client->state == STATE_READY) { if (client->state == STATE_READY) {
pthread_mutex_unlock(&client->mutex); pthread_cond_wait(&client->cond, &client->mutex);
usleep(5);
continue;
} }
memset(client->pty_buffer, 0, sizeof(client->pty_buffer)); memset(client->pty_buffer, 0, sizeof(client->pty_buffer));
client->pty_len = read(pty, client->pty_buffer + LWS_PRE + 1, BUF_SIZE); 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->state = STATE_INIT;
client->pty_len = 0; client->pty_len = 0;
pthread_mutex_init(&client->mutex, NULL); pthread_mutex_init(&client->mutex, NULL);
pthread_cond_init(&client->cond, NULL);
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi),
client->hostname, sizeof(client->hostname), client->hostname, sizeof(client->hostname),
client->address, sizeof(client->address)); client->address, sizeof(client->address));

View File

@@ -475,6 +475,8 @@ main(int argc, char **argv) {
pthread_mutex_lock(&client->mutex); pthread_mutex_lock(&client->mutex);
if (client->state != STATE_DONE) if (client->state != STATE_DONE)
lws_callback_on_writable(client->wsi); lws_callback_on_writable(client->wsi);
else
pthread_cond_signal(&client->cond);
pthread_mutex_unlock(&client->mutex); pthread_mutex_unlock(&client->mutex);
} }
} }

View File

@@ -46,6 +46,7 @@ struct tty_client {
ssize_t pty_len; ssize_t pty_len;
pthread_t thread; pthread_t thread;
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_cond_t cond;
LIST_ENTRY(tty_client) list; LIST_ENTRY(tty_client) list;
}; };