From 86d26cd770c9fedf33c0739c412cba02ffb57306 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Mon, 20 Apr 2009 22:12:00 -0400 Subject: [PATCH] Use a pth_msgport_t for message passing. Also wait for an PTH_EVENT_MSG event in client_msg_thread(). --- src/common.h | 5 ++--- src/pwmd.c | 47 +++++++++++++++++++++++------------------------ src/status.c | 59 +++++++++++++++++++---------------------------------------- 3 files changed, 44 insertions(+), 67 deletions(-) diff --git a/src/common.h b/src/common.h index a75077e6..42f5e5f6 100644 --- a/src/common.h +++ b/src/common.h @@ -139,10 +139,8 @@ typedef struct { struct client_thread_s { pth_t tid; - GSList *msg_list; - pth_mutex_t msg_list_mutex; + pth_msgport_t mp; pth_t msg_tid; - pth_cond_t msg_cond; gint fd; struct client_s *cl; }; @@ -222,5 +220,6 @@ struct client_crypto_s *init_client_crypto(); gpg_error_t do_assuan_command(assuan_context_t ctx, void *(*cb)(void *data), void *data); void close_file_header(file_header_internal_t *fh); +void cleanup_ev_cb(void *arg); #endif diff --git a/src/pwmd.c b/src/pwmd.c index 313926f6..01788c81 100644 --- a/src/pwmd.c +++ b/src/pwmd.c @@ -358,15 +358,13 @@ void close_file_header(file_header_internal_t *fh) if (!fh) return; - if (fh) { - if (fh->fd != -1 || (cmdline == TRUE && fh->fd != STDOUT_FILENO)) - close(fh->fd); - - if (fh->doc) - gcry_free(fh->doc); + if (fh->fd != -1 || (cmdline == TRUE && fh->fd != STDOUT_FILENO)) + close(fh->fd); - g_free(fh); - } + if (fh->doc) + gcry_free(fh->doc); + + g_free(fh); } void cleanup_crypto(struct client_crypto_s **c) @@ -421,26 +419,26 @@ static void cleanup_cb(void *arg) pth_join(cn->msg_tid, NULL); } - for (;;) { - struct status_msg_s *m = g_slist_nth_data(cn->msg_list, 0); - - if (!m) - break; + while (pth_msgport_pending(cn->mp)) { + pth_message_t *msg = pth_msgport_get(cn->mp); - cn->msg_list = g_slist_remove(cn->msg_list, m); - g_free(m); + g_free(msg->m_data); + g_free(msg); } - if (cl && cl->freed == FALSE) + if (!cl) + goto done; + + if (!cl->freed) cleanup_client(cl); - if (cl && cl->ctx) + if (cl->ctx) assuan_deinit_server(cl->ctx); - else if (cl && cl->thd && cl->thd->fd != -1) + else if (cl->thd && cl->thd->fd != -1) close(cl->thd->fd); #ifdef WITH_PINENTRY - if (cl && cl->pinentry) + if (cl->pinentry) cleanup_pinentry(cl->pinentry); #endif @@ -448,6 +446,7 @@ static void cleanup_cb(void *arg) cleanup_crypto(&cl->crypto); g_free(cl); +done: log_write(N_("exiting, fd=%i"), cn->fd); g_free(cn); send_status_all(STATUS_CLIENTS); @@ -504,10 +503,11 @@ static void *client_thread(void *data) } #endif - pth_mutex_init(&thd->msg_list_mutex); + //pth_mutex_init(&thd->msg_list_mutex); attr = pth_attr_new(); pth_attr_init(attr); pth_attr_set(attr, PTH_ATTR_JOINABLE, TRUE); + thd->mp = pth_msgport_create(NULL); thd->msg_tid = pth_spawn(attr, client_msg_thread, thd); n = errno; pth_attr_destroy(attr); @@ -610,8 +610,7 @@ static void *client_thread(void *data) } /* - * Client cleanup (including XML data) is done in cleanup_cb() from - * the cleanup thread. + * Client cleanup (including XML data) is done in cleanup_cb(). */ done: fail: @@ -1451,7 +1450,7 @@ static void *adjust_cache_timer_thread(void *arg) return NULL; } -static void keepalive_cleanup(void *arg) +void cleanup_ev_cb(void *arg) { pth_event_t ev = arg; @@ -1469,7 +1468,7 @@ static void *keepalive_thread(void *arg) for (;;) { pth_event_t ev = pth_event(PTH_EVENT_TIME, pth_timeout(to, 0)); - pth_cleanup_push(keepalive_cleanup, ev); + pth_cleanup_push(cleanup_ev_cb, ev); pth_wait(ev); send_status_all(STATUS_KEEPALIVE); pth_cleanup_pop(1); diff --git a/src/status.c b/src/status.c index 9e169e84..18a47752 100644 --- a/src/status.c +++ b/src/status.c @@ -105,36 +105,25 @@ gpg_error_t send_status(assuan_context_t ctx, status_msg_t which, void *client_msg_thread(void *arg) { struct client_thread_s *thd = arg; - pth_mutex_t m; pth_attr_t attr = pth_attr_of(pth_self()); pth_attr_set(attr, PTH_ATTR_NAME, __FUNCTION__); pth_attr_destroy(attr); - pth_mutex_init(&m); - pth_cond_init(&thd->msg_cond); - pth_mutex_acquire(&m, FALSE, NULL); for (;;) { - pth_cond_await(&thd->msg_cond, &m, NULL); - pth_cancel_point(); + pth_event_t ev = pth_event(PTH_EVENT_MSG, thd->mp); - for (;;) { - status_msg_t *msg; - gpg_error_t rc; - - MUTEX_LOCK(&thd->msg_list_mutex); - msg = g_slist_nth_data(thd->msg_list, 0); + pth_cleanup_push(cleanup_ev_cb, ev); + pth_wait(ev); - if (msg) - thd->msg_list = g_slist_remove(thd->msg_list, msg); - - MUTEX_UNLOCK(&thd->msg_list_mutex); - - if (!msg) - break; + while (pth_msgport_pending(thd->mp)) { + pth_message_t *msg = pth_msgport_get(thd->mp); + status_msg_t *s = msg->m_data; + gpg_error_t rc; - rc = send_status(thd->cl->ctx, *msg, NULL); g_free(msg); + rc = send_status(thd->cl->ctx, *s, NULL); + g_free(s); pth_cancel_point(); if (rc) { @@ -148,20 +137,6 @@ void *client_msg_thread(void *arg) return NULL; } -static gboolean msg_list_dup(GSList *list, status_msg_t which) -{ - guint i, t; - - for (t = g_slist_length(list), i = 0; i < t; i++) { - status_msg_t *m = g_slist_nth_data(list, i); - - if (*m == which) - return TRUE; - } - - return FALSE; -} - void send_status_all(status_msg_t which) { guint i, t; @@ -170,23 +145,27 @@ void send_status_all(status_msg_t which) for (t = g_slist_length(cn_thread_list), i = 0; i < t; i++) { struct client_thread_s *cn = g_slist_nth_data(cn_thread_list, i); + pth_message_t *msg; status_msg_t *m; - if (msg_list_dup(cn->msg_list, which)) + msg = g_malloc0(sizeof(pth_message_t)); + + if (!msg) { + pth_cancel(cn->tid); continue; + } m = g_malloc(sizeof(status_msg_t)); if (!m) { - log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM)); + g_free(msg); + pth_cancel(cn->tid); continue; } *m = which; - MUTEX_LOCK(&cn->msg_list_mutex); - cn->msg_list = g_slist_append(cn->msg_list, m); - MUTEX_UNLOCK(&cn->msg_list_mutex); - pth_cond_notify(&cn->msg_cond, FALSE); + msg->m_data = m; + pth_msgport_put(cn->mp, msg); } MUTEX_UNLOCK(&cn_mutex); -- 2.11.4.GIT