From 95f58c95b6915c7003a52a652fa6aff2bb86b613 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Mon, 6 May 2002 11:46:36 +0000 Subject: Destroy our mutex too. (e_thread_new): Add thread to a new list of all 2002-05-06 Not Zed * e-msgport.c (e_thread_destroy): Destroy our mutex too. (e_thread_new): Add thread to a new list of all threads. (e_thread_destroy): Remove thread from list of all threads. (e_thread_busy): New function, returh true if we're busy somewhere processing work. svn path=/trunk/; revision=16683 --- e-util/ChangeLog | 8 ++++++++ e-util/e-msgport.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ e-util/e-msgport.h | 1 + 3 files changed, 55 insertions(+) diff --git a/e-util/ChangeLog b/e-util/ChangeLog index 4b76ec6458..e6c3ddb2b8 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,11 @@ +2002-05-06 Not Zed + + * e-msgport.c (e_thread_destroy): Destroy our mutex too. + (e_thread_new): Add thread to a new list of all threads. + (e_thread_destroy): Remove thread from list of all threads. + (e_thread_busy): New function, returh true if we're busy somewhere + processing work. + 2002-04-19 Jeffrey Stedfast * e-passwords.c (e_passwords_get_password): Don't leak the base64 diff --git a/e-util/e-msgport.c b/e-util/e-msgport.c index 2bb4311b18..e04fa8dc9a 100644 --- a/e-util/e-msgport.c +++ b/e-util/e-msgport.c @@ -248,6 +248,9 @@ struct _thread_info { }; struct _EThread { + struct _EThread *next; + struct _EThread *prev; + EMsgPort *server_port; EMsgPort *reply_port; pthread_mutex_t mutex; @@ -268,6 +271,10 @@ struct _EThread { void *lost_data; }; +/* All active threads */ +static EDList ethread_list = E_DLIST_INITIALISER(ethread_list); +static pthread_mutex_t ethread_lock = PTHREAD_MUTEX_INITIALIZER; + #define E_THREAD_NONE ((pthread_t)~0) #define E_THREAD_QUIT_REPLYPORT ((struct _EMsgPort *)~0) @@ -317,6 +324,10 @@ EThread *e_thread_new(e_thread_t type) e->id = E_THREAD_NONE; e->queue_limit = INT_MAX; + pthread_mutex_lock(ðread_lock); + e_dlist_addtail(ðread_list, (EDListNode *)e); + pthread_mutex_unlock(ðread_lock); + return e; } @@ -393,6 +404,11 @@ void e_thread_destroy(EThread *e) return; } + pthread_mutex_lock(ðread_lock); + e_dlist_remove((EDListNode *)e); + pthread_mutex_unlock(ðread_lock); + + pthread_mutex_destroy(&e->mutex); e_msgport_destroy(e->server_port); g_free(e); } @@ -437,6 +453,36 @@ void e_thread_set_msg_received(EThread *e, EThreadFunc received, void *data) pthread_mutex_unlock(&e->mutex); } +/* find out if we're busy doing any work, e==NULL, check for all work */ +int e_thread_busy(EThread *e) +{ + int busy = FALSE; + + if (e == NULL) { + pthread_mutex_lock(ðread_lock); + e = (EThread *)ethread_list.head; + while (e->next && !busy) { + busy = e_thread_busy(e); + e = e->next; + } + pthread_mutex_unlock(ðread_lock); + } else { + pthread_mutex_lock(&e->mutex); + switch (e->type) { + case E_THREAD_QUEUE: + case E_THREAD_DROP: + busy = e->waiting != 1 && e->id != E_THREAD_NONE; + break; + case E_THREAD_NEW: + busy = e->waiting != g_list_length(e->id_list); + break; + } + pthread_mutex_unlock(&e->mutex); + } + + return busy; +} + static void thread_destroy_msg(EThread *e, EMsg *m) { diff --git a/e-util/e-msgport.h b/e-util/e-msgport.h index d181ac7fb7..03dc514b07 100644 --- a/e-util/e-msgport.h +++ b/e-util/e-msgport.h @@ -64,6 +64,7 @@ void e_thread_set_msg_destroy(EThread *e, EThreadFunc destroy, void *data); void e_thread_set_reply_port(EThread *e, EMsgPort *reply_port); void e_thread_set_msg_received(EThread *e, EThreadFunc received, void *data); void e_thread_put(EThread *e, EMsg *msg); +int e_thread_busy(EThread *e); /* sigh, another mutex interface, this one allows different mutex types, portably */ typedef struct _EMutex EMutex; -- cgit v1.2.3