[srvx-commits] CVS: services/src timeq.c,1.7.2.1,1.7.2.2
Brought to you by:
entrope
From: Entrope <en...@us...> - 2001-08-19 00:40:22
|
Update of /cvsroot/srvx/services/src In directory usw-pr-cvs1:/tmp/cvs-serv14599/src Modified Files: Tag: rel-1_0 timeq.c Log Message: make a recursive mutex for protecting the timeq against modification from both threads Index: timeq.c =================================================================== RCS file: /cvsroot/srvx/services/src/timeq.c,v retrieving revision 1.7.2.1 retrieving revision 1.7.2.2 diff -C2 -r1.7.2.1 -r1.7.2.2 *** timeq.c 2001/03/29 00:20:52 1.7.2.1 --- timeq.c 2001/08/19 00:40:19 1.7.2.2 *************** *** 20,28 **** #include "config.h" - #ifdef HAVE_STDLIB_H #include <stdlib.h> - #endif #include "common.h" #include "heap.h" #include "timeq.h" --- 20,60 ---- #include "config.h" #include <stdlib.h> #include "common.h" + #if HAVE_PTHREAD_H && SOCKCHECK_USABLE /* we could have multiple threads setting glines */ + #include <pthread.h> + /* Sigh. Want recursive mutexes in standard! + * (The timeq mutex needs to be recursive since callers frequently do timeq_add from + * within timeq_run() callbacks.) */ + static pthread_mutex_t timeq_mutex = PTHREAD_MUTEX_INITIALIZER; + static pthread_cond_t timeq_condvar = PTHREAD_COND_INITIALIZER; + static pthread_t timeq_locker; + static int timeq_mutex_count; + + static void + timeq_mutex_lock(void) { + pthread_mutex_lock(&timeq_mutex); + if ((timeq_mutex_count > 0) && (timeq_locker != pthread_self())) { + pthread_cond_wait(&timeq_condvar, &timeq_mutex); + } + timeq_locker = pthread_self(); + timeq_mutex_count++; + pthread_mutex_unlock(&timeq_mutex); + } + + static void + timeq_mutex_unlock(void) { + pthread_mutex_lock(&timeq_mutex); + timeq_mutex_count--; + if (!timeq_mutex_count) pthread_cond_signal(&timeq_condvar); + pthread_mutex_unlock(&timeq_mutex); + } + + #else + #define timeq_mutex_lock() /*noop*/ + #define timeq_mutex_unlock() /*noop*/ + #endif + #include "heap.h" #include "timeq.h" *************** *** 39,42 **** --- 71,75 ---- { timeq = heap_new(int_comparator); + pthread_cond_signal(&timeq_condvar); } *************** *** 45,49 **** --- 78,84 ---- { void *time; + timeq_mutex_lock(); heap_peek(timeq, &time, 0); + timeq_mutex_unlock(); return TIME_T_CAST(time); } *************** *** 58,62 **** --- 93,99 ---- ent->data = data; w = (void*)TIME_T_CAST(when); + timeq_mutex_lock(); heap_insert(timeq, w, ent); + timeq_mutex_unlock(); } *************** *** 68,72 **** }; ! int timeq_del_matching(void *key, void *data, void *extra) { --- 105,109 ---- }; ! static int timeq_del_matching(void *key, void *data, void *extra) { *************** *** 88,92 **** --- 125,131 ---- extra.data = data; extra.mask = mask; + timeq_mutex_lock(); heap_remove_pred(timeq, timeq_del_matching, &extra); + timeq_mutex_unlock(); } *************** *** 96,99 **** --- 135,139 ---- void *k, *d; struct timeq_entry *ent; + timeq_mutex_lock(); while (1) { heap_peek(timeq, &k, &d); *************** *** 104,106 **** --- 144,147 ---- free(ent); } + timeq_mutex_unlock(); } |