diff --git a/bin/tests/task_test.c b/bin/tests/task_test.c index b61376951f..3c81906a98 100644 --- a/bin/tests/task_test.c +++ b/bin/tests/task_test.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -53,7 +54,7 @@ main(int argc, char *argv[]) { unsigned int workers; isc_timermgr_t timgr; isc_timer_t ti1, ti2; - os_time_t absolute, interval; + struct isc_time absolute, interval; if (argc > 1) workers = atoi(argv[1]); @@ -63,12 +64,17 @@ main(int argc, char *argv[]) { INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS); - INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == workers); + INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == + ISC_R_SUCCESS); - INSIST(isc_task_create(manager, my_shutdown, "1", 0, &t1)); - INSIST(isc_task_create(manager, my_shutdown, "2", 0, &t2)); - INSIST(isc_task_create(manager, my_shutdown, "3", 0, &t3)); - INSIST(isc_task_create(manager, my_shutdown, "4", 0, &t4)); + INSIST(isc_task_create(manager, my_shutdown, "1", 0, &t1) == + ISC_R_SUCCESS); + INSIST(isc_task_create(manager, my_shutdown, "2", 0, &t2) == + ISC_R_SUCCESS); + INSIST(isc_task_create(manager, my_shutdown, "3", 0, &t3) == + ISC_R_SUCCESS); + INSIST(isc_task_create(manager, my_shutdown, "4", 0, &t4) == + ISC_R_SUCCESS); timgr = NULL; INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS); @@ -78,11 +84,11 @@ main(int argc, char *argv[]) { interval.seconds = 1; interval.nanoseconds = 0; INSIST(isc_timer_create(timgr, isc_timertype_ticker, - absolute, interval, + &absolute, &interval, t1, my_tick, "foo", &ti1) == ISC_R_SUCCESS); ti2 = NULL; INSIST(isc_timer_create(timgr, isc_timertype_ticker, - absolute, interval, + &absolute, &interval, t2, my_tick, "bar", &ti2) == ISC_R_SUCCESS); printf("task 1 = %p\n", t1); diff --git a/bin/tests/timer_test.c b/bin/tests/timer_test.c index c6cab855a3..41873760fc 100644 --- a/bin/tests/timer_test.c +++ b/bin/tests/timer_test.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -38,17 +39,17 @@ tick(isc_task_t task, isc_event_t event) isc_timer_touch(ti3); if (tick_count == 7) { - os_time_t expires, interval, now; + struct isc_time expires, interval, now; - (void)os_time_get(&now); + (void)isc_time_get(&now); expires.seconds = 5; expires.nanoseconds = 0; - os_time_add(&now, &expires, &expires); + isc_time_add(&now, &expires, &expires); interval.seconds = 4; interval.nanoseconds = 0; printf("*** resetting ti3 ***\n"); - INSIST(isc_timer_reset(ti3, isc_timertype_once, expires, - interval, ISC_TRUE) + INSIST(isc_timer_reset(ti3, isc_timertype_once, &expires, + &interval, ISC_TRUE) == ISC_R_SUCCESS); } @@ -82,7 +83,7 @@ main(int argc, char *argv[]) { isc_taskmgr_t manager = NULL; isc_timermgr_t timgr = NULL; unsigned int workers; - os_time_t expires, interval, now; + struct isc_time expires, interval, now; if (argc > 1) workers = atoi(argv[1]); @@ -91,36 +92,41 @@ main(int argc, char *argv[]) { printf("%d workers\n", workers); INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS); - INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == workers); - INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1)); - INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2)); - INSIST(isc_task_create(manager, shutdown_task, "3", 0, &t3)); + INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == + ISC_R_SUCCESS); + INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1) == + ISC_R_SUCCESS); + INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2) == + ISC_R_SUCCESS); + INSIST(isc_task_create(manager, shutdown_task, "3", 0, &t3) == + ISC_R_SUCCESS); INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS); printf("task 1: %p\n", t1); printf("task 2: %p\n", t2); printf("task 3: %p\n", t3); - (void)os_time_get(&now); + (void)isc_time_get(&now); expires.seconds = 0; expires.nanoseconds = 0; interval.seconds = 2; interval.nanoseconds = 0; - INSIST(isc_timer_create(timgr, isc_timertype_once, expires, interval, + INSIST(isc_timer_create(timgr, isc_timertype_once, &expires, &interval, t2, timeout, "2", &ti2) == ISC_R_SUCCESS); expires.seconds = 0; expires.nanoseconds = 0; interval.seconds = 1; interval.nanoseconds = 0; - INSIST(isc_timer_create(timgr, isc_timertype_ticker, expires, interval, + INSIST(isc_timer_create(timgr, isc_timertype_ticker, + &expires, &interval, t1, tick, "1", &ti1) == ISC_R_SUCCESS); expires.seconds = 10; expires.nanoseconds = 0; - os_time_add(&now, &expires, &expires); + isc_time_add(&now, &expires, &expires); interval.seconds = 2; interval.nanoseconds = 0; - INSIST(isc_timer_create(timgr, isc_timertype_once, expires, interval, + INSIST(isc_timer_create(timgr, isc_timertype_once, &expires, &interval, t3, timeout, "3", &ti3) == ISC_R_SUCCESS); isc_task_detach(&t1); diff --git a/lib/isc/include/isc/result.h b/lib/isc/include/isc/result.h index 989edc152f..2bcf9b137b 100644 --- a/lib/isc/include/isc/result.h +++ b/lib/isc/include/isc/result.h @@ -9,6 +9,8 @@ typedef unsigned int isc_result_t; #define ISC_R_SUCCESS 0 #define ISC_R_NOMEMORY 1 +#define ISC_R_TIMEDOUT 2 +#define ISC_R_NOTHREADS 3 #define ISC_R_UNEXPECTED 0xFFFFFFFFL #define isc_result_totext __isc_result_totext diff --git a/lib/isc/include/isc/task.h b/lib/isc/include/isc/task.h index 5d07353fed..8833ec08eb 100644 --- a/lib/isc/include/isc/task.h +++ b/lib/isc/include/isc/task.h @@ -8,6 +8,7 @@ #include #include +#include /*** @@ -64,16 +65,16 @@ void isc_event_free(isc_event_t *); *** Tasks. ***/ -isc_boolean_t isc_task_create(isc_taskmgr_t, - isc_taskaction_t, - void *, - unsigned int, - isc_task_t *); +isc_result_t isc_task_create(isc_taskmgr_t, + isc_taskaction_t, + void *, + unsigned int, + isc_task_t *); void isc_task_attach(isc_task_t, isc_task_t *); void isc_task_detach(isc_task_t *); -isc_boolean_t isc_task_send(isc_task_t, - isc_event_t *); +void isc_task_send(isc_task_t, + isc_event_t *); void isc_task_purge(isc_task_t, void *, isc_eventtype_t); void isc_task_shutdown(isc_task_t); @@ -83,7 +84,7 @@ void isc_task_destroy(isc_task_t *); *** Task Manager. ***/ -unsigned int isc_taskmgr_create(isc_memctx_t, +isc_result_t isc_taskmgr_create(isc_memctx_t, unsigned int, unsigned int, isc_taskmgr_t *); diff --git a/lib/isc/include/isc/timer.h b/lib/isc/include/isc/timer.h index 6c19746ab6..3e807f44b8 100644 --- a/lib/isc/include/isc/timer.h +++ b/lib/isc/include/isc/timer.h @@ -91,8 +91,8 @@ typedef struct isc_timerevent { isc_result_t isc_timer_create(isc_timermgr_t manager, isc_timertype_t type, - os_time_t expires, - os_time_t interval, + isc_time_t expires, + isc_time_t interval, isc_task_t task, isc_taskaction_t action, void *arg, @@ -146,8 +146,8 @@ isc_timer_create(isc_timermgr_t manager, isc_result_t isc_timer_reset(isc_timer_t timer, isc_timertype_t type, - os_time_t expires, - os_time_t interval, + isc_time_t expires, + isc_time_t interval, isc_boolean_t purge); /* * Change the timer's type, expires, and interval values to the given diff --git a/lib/isc/mem.c b/lib/isc/mem.c index ecc6d4d078..9111581b4f 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -32,7 +32,7 @@ #endif #if !defined(LINT) && !defined(CODECENTER) -static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.6 1998/10/21 22:00:56 halley Exp $"; +static char rcsid[] __attribute__((unused)) = "$Id: mem.c,v 1.7 1998/10/22 01:33:03 halley Exp $"; #endif /* not lint */ /* @@ -68,7 +68,7 @@ struct isc_memctx { unsigned char * lowest; unsigned char * highest; struct stats * stats; - os_mutex_t mutex; + isc_mutex_t mutex; }; /* Forward. */ @@ -84,8 +84,10 @@ static size_t quantize(size_t); #define TABLE_INCREMENT 1024 #ifdef MULTITHREADED -#define LOCK_CONTEXT(ctx) INSIST(os_mutex_lock(&(ctx)->mutex)) -#define UNLOCK_CONTEXT(ctx) INSIST(os_mutex_unlock(&(ctx)->mutex)) +#define LOCK_CONTEXT(ctx) \ + INSIST(isc_mutex_lock(&(ctx)->mutex) == ISC_R_SUCCESS) +#define UNLOCK_CONTEXT(ctx) \ + INSIST(isc_mutex_unlock(&(ctx)->mutex) == ISC_R_SUCCESS) #else #define LOCK_CONTEXT(ctx) #define UNLOCK_CONTEXT(ctx) @@ -152,12 +154,12 @@ isc_memctx_create(size_t init_max_size, size_t target_size, ctx->basic_table_size = 0; ctx->lowest = NULL; ctx->highest = NULL; - if (!os_mutex_init(&ctx->mutex)) { + if (isc_mutex_init(&ctx->mutex) != ISC_R_SUCCESS) { free(ctx->stats); free(ctx->freelists); free(ctx); UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_mutex_init() failed"); + "isc_mutex_init() failed"); return (ISC_R_UNEXPECTED); } *ctxp = ctx; @@ -181,7 +183,7 @@ isc_memctx_destroy(isc_memctx_t *ctxp) { free(ctx->freelists); free(ctx->stats); free(ctx->basic_table); - (void)os_mutex_destroy(&ctx->mutex); + (void)isc_mutex_destroy(&ctx->mutex); free(ctx); *ctxp = NULL; @@ -453,8 +455,7 @@ meminit(size_t init_max_size, size_t target_size) { /* need default_context lock here */ if (default_context != NULL) return (-1); - return (isc_mem_create(init_max_size, target_size, - &default_context)); + return (isc_mem_create(init_max_size, target_size, &default_context)); } isc_memctx_t diff --git a/lib/isc/pthreads/condition.c b/lib/isc/pthreads/condition.c index 9bbe45ef8b..4429501699 100644 --- a/lib/isc/pthreads/condition.c +++ b/lib/isc/pthreads/condition.c @@ -1,23 +1,26 @@ +#include +#include + #include -#include +#include -isc_boolean_t -os_condition_waituntil(os_condition_t *c, os_mutex_t *m, os_time_t *t, - isc_boolean_t *timeout) +isc_result_t +isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t t) { - int result; + int presult; struct timespec ts; ts.tv_sec = t->seconds; ts.tv_nsec = t->nanoseconds; - result = pthread_cond_timedwait(c, m, &ts); - if (result == 0) { - *timeout = ISC_FALSE; - return (ISC_TRUE); - } else if (result == ETIMEDOUT) { - *timeout = ISC_TRUE; - return (ISC_TRUE); - } - return (ISC_FALSE); + presult = pthread_cond_timedwait(c, m, &ts); + if (presult == 0) + return (ISC_R_SUCCESS); + if (presult == ETIMEDOUT) + return (ISC_R_TIMEDOUT); + + UNEXPECTED_ERROR(__FILE__, __LINE__, + "pthread_cond_timedwait() returned %s", + strerror(presult)); + return (ISC_R_UNEXPECTED); } diff --git a/lib/isc/pthreads/include/isc/condition.h b/lib/isc/pthreads/include/isc/condition.h index ef5b151c7d..1c0c45b8ae 100644 --- a/lib/isc/pthreads/include/isc/condition.h +++ b/lib/isc/pthreads/include/isc/condition.h @@ -1,25 +1,37 @@ -#ifndef CONDITION_H -#define CONDITION_H 1 +#ifndef ISC_CONDITION_H +#define ISC_CONDITION_H 1 #include #include +#include #include #include -typedef pthread_cond_t os_condition_t; -#define OS_CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER +typedef pthread_cond_t isc_condition_t; -#define os_condition_init(cp) (pthread_cond_init((cp), NULL) == 0) -#define os_condition_wait(cp, mp) (pthread_cond_wait((cp), (mp)) == 0) -#define os_condition_signal(cp) (pthread_cond_signal((cp)) == 0) -#define os_condition_broadcast(cp) (pthread_cond_broadcast((cp)) == 0) -#define os_condition_destroy(cp) (pthread_cond_destroy((cp)) == 0) +#define isc_condition_init(cp) \ + ((pthread_cond_init((cp), NULL) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) -isc_boolean_t os_condition_waituntil(os_condition_t *, - os_mutex_t *, - os_time_t *, - isc_boolean_t *); +#define isc_condition_wait(cp, mp) \ + ((pthread_cond_wait((cp), (mp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) -#endif /* CONDITION_H */ +#define isc_condition_signal(cp) \ + ((pthread_cond_signal((cp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +#define isc_condition_broadcast(cp) \ + ((pthread_cond_broadcast((cp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +#define isc_condition_destroy(cp) \ + ((pthread_cond_destroy((cp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +isc_result_t isc_condition_waituntil(isc_condition_t *, isc_mutex_t *, + isc_time_t); + +#endif /* ISC_CONDITION_H */ diff --git a/lib/isc/pthreads/include/isc/mutex.h b/lib/isc/pthreads/include/isc/mutex.h index 724408a0c2..e53d4fb683 100644 --- a/lib/isc/pthreads/include/isc/mutex.h +++ b/lib/isc/pthreads/include/isc/mutex.h @@ -1,15 +1,26 @@ -#ifndef MUTEX_H -#define MUTEX_H 1 +#ifndef ISC_MUTEX_H +#define ISC_MUTEX_H 1 #include -typedef pthread_mutex_t os_mutex_t; -#define OS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#include -#define os_mutex_init(mp) (pthread_mutex_init((mp), NULL) == 0) -#define os_mutex_lock(mp) (pthread_mutex_lock((mp)) == 0) -#define os_mutex_unlock(mp) (pthread_mutex_unlock((mp)) == 0) -#define os_mutex_destroy(mp) (pthread_mutex_destroy((mp)) == 0) +typedef pthread_mutex_t isc_mutex_t; -#endif /* MUTEX_H */ +/* XXX We could do fancier error handling... */ + +#define isc_mutex_init(mp) \ + ((pthread_mutex_init((mp), NULL) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) +#define isc_mutex_lock(mp) \ + ((pthread_mutex_lock((mp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) +#define isc_mutex_unlock(mp) \ + ((pthread_mutex_unlock((mp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) +#define isc_mutex_destroy(mp) \ + ((pthread_mutex_destroy((mp)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +#endif /* ISC_MUTEX_H */ diff --git a/lib/isc/pthreads/include/isc/thread.h b/lib/isc/pthreads/include/isc/thread.h index 080dc3e462..6b5045c76a 100644 --- a/lib/isc/pthreads/include/isc/thread.h +++ b/lib/isc/pthreads/include/isc/thread.h @@ -1,17 +1,28 @@ -#ifndef THREAD_H -#define THREAD_H 1 +#ifndef ISC_THREAD_H +#define ISC_THREAD_H 1 #include -#include +#include -typedef pthread_t os_thread_t; +typedef pthread_t isc_thread_t; -#define os_thread_create(s, a, tp) (pthread_create((tp), NULL, (s), (a)) \ - == 0) -#define os_thread_detach(t) (pthread_detach((t)) == 0) -#define os_thread_join(t) (pthread_join((t), NULL) == 0) -#define os_thread_self pthread_self +/* XXX We could do fancier error handling... */ -#endif /* THREAD_H */ +#define isc_thread_create(s, a, tp) \ + ((pthread_create((tp), NULL, (s), (a)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +#define isc_thread_detach(t) \ + ((pthread_detach((t)) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +#define isc_thread_join(t) \ + ((pthread_join((t), NULL) == 0) ? \ + ISC_R_SUCCESS : ISC_R_UNEXPECTED) + +#define isc_thread_self \ + pthread_self + +#endif /* ISC_THREAD_H */ diff --git a/lib/isc/task.c b/lib/isc/task.c index 7a1d7fff64..23f91c125b 100644 --- a/lib/isc/task.c +++ b/lib/isc/task.c @@ -6,7 +6,7 @@ #include #include #include - +#include #include @@ -15,20 +15,28 @@ ***/ /* - * We use macros instead of calling the os_ routines directly because + * We use macros instead of calling the routines directly because * the capital letters make the locking stand out. * * We INSIST that they succeed since there's no way for us to continue * if they fail. */ -#define LOCK(lp) INSIST(os_mutex_lock((lp))) -#define UNLOCK(lp) INSIST(os_mutex_unlock((lp))) -#define WAIT(cvp, lp) INSIST(os_condition_wait((cvp), (lp))) -#define BROADCAST(cvp) INSIST(os_condition_broadcast((cvp))) + +#define LOCK(lp) \ + INSIST(isc_mutex_lock((lp)) == ISC_R_SUCCESS); +#define UNLOCK(lp) \ + INSIST(isc_mutex_unlock((lp)) == ISC_R_SUCCESS); +#define BROADCAST(cvp) \ + INSIST(isc_condition_broadcast((cvp)) == ISC_R_SUCCESS); +#define WAIT(cvp, lp) \ + INSIST(isc_condition_wait((cvp), (lp)) == ISC_R_SUCCESS); +#define WAITUNTIL(cvp, lp, tp, bp) \ + INSIST(isc_condition_waituntil((cvp), (lp), (tp), (bp)) == \ + ISC_R_SUCCESS); #ifdef ISC_TASK_TRACE #define XTRACE(m) printf("%s task %p thread %p\n", (m), \ - task, os_thread_self()) + task, isc_thread_self()) #else #define XTRACE(m) #endif @@ -51,7 +59,7 @@ struct isc_task { /* Not locked. */ unsigned int magic; isc_taskmgr_t manager; - os_mutex_t lock; + isc_mutex_t lock; /* Locked by task lock. */ task_state_t state; unsigned int references; @@ -72,15 +80,15 @@ struct isc_taskmgr { /* Not locked. */ unsigned int magic; isc_memctx_t mctx; - os_mutex_t lock; + isc_mutex_t lock; /* Locked by task manager lock. */ unsigned int default_quantum; LIST(struct isc_task) tasks; LIST(struct isc_task) ready_tasks; - os_condition_t work_available; + isc_condition_t work_available; isc_boolean_t exiting; unsigned int workers; - os_condition_t no_workers; + isc_condition_t no_workers; }; #define DEFAULT_DEFAULT_QUANTUM 5 @@ -160,14 +168,14 @@ task_free(isc_task_t task) { BROADCAST(&manager->work_available); } UNLOCK(&manager->lock); - (void)os_mutex_destroy(&task->lock); + (void)isc_mutex_destroy(&task->lock); if (task->shutdown_event != NULL) isc_event_free(&task->shutdown_event); task->magic = 0; isc_mem_put(manager->mctx, task, sizeof *task); } -isc_boolean_t +isc_result_t isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action, void *shutdown_arg, unsigned int quantum, isc_task_t *taskp) { @@ -178,13 +186,15 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action, task = isc_mem_get(manager->mctx, sizeof *task); if (task == NULL) - return (ISC_FALSE); + return (ISC_R_NOMEMORY); task->magic = TASK_MAGIC; task->manager = manager; - if (!os_mutex_init(&task->lock)) { + if (isc_mutex_init(&task->lock) != ISC_R_SUCCESS) { isc_mem_put(manager->mctx, task, sizeof *task); - return (ISC_FALSE); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_mutex_init() failed"); + return (ISC_R_UNEXPECTED); } task->state = task_state_idle; task->references = 1; @@ -198,9 +208,9 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action, shutdown_arg, sizeof *task->shutdown_event); if (task->shutdown_event == NULL) { - (void)os_mutex_destroy(&task->lock); + (void)isc_mutex_destroy(&task->lock); isc_mem_put(manager->mctx, task, sizeof *task); - return (ISC_FALSE); + return (ISC_R_NOMEMORY); } INIT_LINK(task, link); INIT_LINK(task, ready_link); @@ -213,7 +223,7 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action, *taskp = task; - return (ISC_TRUE); + return (ISC_R_SUCCESS); } void @@ -253,7 +263,7 @@ isc_task_detach(isc_task_t *taskp) { *taskp = NULL; } -isc_boolean_t +void isc_task_send(isc_task_t task, isc_event_t *eventp) { isc_boolean_t was_idle = ISC_FALSE; isc_boolean_t discard = ISC_FALSE; @@ -289,7 +299,7 @@ isc_task_send(isc_task_t task, isc_event_t *eventp) { if (discard) { isc_event_free(&event); *eventp = NULL; - return (ISC_TRUE); + return; } if (was_idle) { @@ -335,7 +345,6 @@ isc_task_send(isc_task_t task, isc_event_t *eventp) { *eventp = NULL; XTRACE("sent"); - return (ISC_TRUE); } void @@ -685,49 +694,55 @@ void *run(void *uap) { static void manager_free(isc_taskmgr_t manager) { - (void)os_condition_destroy(&manager->work_available); - (void)os_condition_destroy(&manager->no_workers); - (void)os_mutex_destroy(&manager->lock); + (void)isc_condition_destroy(&manager->work_available); + (void)isc_condition_destroy(&manager->no_workers); + (void)isc_mutex_destroy(&manager->lock); manager->magic = 0; isc_mem_put(manager->mctx, manager, sizeof *manager); } -unsigned int +isc_result_t isc_taskmgr_create(isc_memctx_t mctx, unsigned int workers, unsigned int default_quantum, isc_taskmgr_t *managerp) { unsigned int i, started = 0; isc_taskmgr_t manager; - os_thread_t thread; + isc_thread_t thread; + + REQUIRE(workers > 0); - if (workers == 0) - return (0); manager = isc_mem_get(mctx, sizeof *manager); if (manager == NULL) - return (0); + return (ISC_R_NOMEMORY); manager->magic = TASK_MANAGER_MAGIC; manager->mctx = mctx; - if (!os_mutex_init(&manager->lock)) { + if (isc_mutex_init(&manager->lock) != ISC_R_SUCCESS) { isc_mem_put(mctx, manager, sizeof *manager); - return (0); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_mutex_init() failed"); + return (ISC_R_UNEXPECTED); } if (default_quantum == 0) default_quantum = DEFAULT_DEFAULT_QUANTUM; manager->default_quantum = default_quantum; INIT_LIST(manager->tasks); INIT_LIST(manager->ready_tasks); - if (!os_condition_init(&manager->work_available)) { - (void)os_mutex_destroy(&manager->lock); + if (isc_condition_init(&manager->work_available) != ISC_R_SUCCESS) { + (void)isc_mutex_destroy(&manager->lock); isc_mem_put(mctx, manager, sizeof *manager); - return (0); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_condition_init() failed"); + return (ISC_R_UNEXPECTED); } manager->exiting = ISC_FALSE; manager->workers = 0; - if (!os_condition_init(&manager->no_workers)) { - (void)os_condition_destroy(&manager->work_available); - (void)os_mutex_destroy(&manager->lock); + if (isc_condition_init(&manager->no_workers) != ISC_R_SUCCESS) { + (void)isc_condition_destroy(&manager->work_available); + (void)isc_mutex_destroy(&manager->lock); isc_mem_put(mctx, manager, sizeof *manager); - return (0); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_condition_init() failed"); + return (ISC_R_UNEXPECTED); } LOCK(&manager->lock); @@ -735,22 +750,23 @@ isc_taskmgr_create(isc_memctx_t mctx, unsigned int workers, * Start workers. */ for (i = 0; i < workers; i++) { - if (os_thread_create(run, manager, &thread)) { + if (isc_thread_create(run, manager, &thread) == + ISC_R_SUCCESS) { manager->workers++; started++; - (void)os_thread_detach(thread); + (void)isc_thread_detach(thread); } } UNLOCK(&manager->lock); if (started == 0) { manager_free(manager); - return (0); + return (ISC_R_NOTHREADS); } *managerp = manager; - return (started); + return (ISC_R_SUCCESS); } void diff --git a/lib/isc/timer.c b/lib/isc/timer.c index c0571ca8d8..dd7ae21d78 100644 --- a/lib/isc/timer.c +++ b/lib/isc/timer.c @@ -11,18 +11,23 @@ #include /* - * We use macros instead of calling the os_ routines directly because + * We use macros instead of calling the routines directly because * the capital letters make the locking stand out. * * We INSIST that they succeed since there's no way for us to continue * if they fail. */ -#define LOCK(lp) INSIST(os_mutex_lock((lp))) -#define UNLOCK(lp) INSIST(os_mutex_unlock((lp))) -#define BROADCAST(cvp) INSIST(os_condition_broadcast((cvp))) -#define WAIT(cvp, lp) INSIST(os_condition_wait((cvp), (lp))) -#define WAITUNTIL(cvp, lp, tp, bp) INSIST(os_condition_waituntil((cvp), \ - (lp), (tp), (bp))) + +#define LOCK(lp) \ + INSIST(isc_mutex_lock((lp)) == ISC_R_SUCCESS); +#define UNLOCK(lp) \ + INSIST(isc_mutex_unlock((lp)) == ISC_R_SUCCESS); +#define BROADCAST(cvp) \ + INSIST(isc_condition_broadcast((cvp)) == ISC_R_SUCCESS); +#define WAIT(cvp, lp) \ + INSIST(isc_condition_wait((cvp), (lp)) == ISC_R_SUCCESS); +#define WAITUNTIL(cvp, lp, tp) \ + isc_condition_waituntil((cvp), (lp), (tp)) #define ZERO(t) ((t).seconds == 0 && \ (t).nanoseconds == 0) @@ -48,19 +53,19 @@ struct isc_timer { /* Not locked. */ unsigned int magic; isc_timermgr_t manager; - os_mutex_t lock; + isc_mutex_t lock; /* Locked by timer lock. */ unsigned int references; - os_time_t idle; + struct isc_time idle; /* Locked by manager lock. */ isc_timertype_t type; - os_time_t expires; - os_time_t interval; + struct isc_time expires; + struct isc_time interval; isc_task_t task; isc_taskaction_t action; void * arg; unsigned int index; - os_time_t due; + struct isc_time due; LINK(struct isc_timer) link; }; @@ -72,22 +77,22 @@ struct isc_timermgr { /* Not locked. */ unsigned int magic; isc_memctx_t mctx; - os_mutex_t lock; + isc_mutex_t lock; /* Locked by manager lock. */ isc_boolean_t done; LIST(struct isc_timer) timers; unsigned int nscheduled; - os_time_t due; - os_condition_t wakeup; - os_thread_t thread; + struct isc_time due; + isc_condition_t wakeup; + isc_thread_t thread; isc_heap_t heap; }; static inline isc_result -schedule(isc_timer_t timer, os_time_t *nowp, isc_boolean_t broadcast_ok) { +schedule(isc_timer_t timer, isc_time_t now, isc_boolean_t broadcast_ok) { isc_result result; isc_timermgr_t manager; - os_time_t due; + struct isc_time due; int cmp; /* @@ -98,13 +103,13 @@ schedule(isc_timer_t timer, os_time_t *nowp, isc_boolean_t broadcast_ok) { * Compute the new due time. */ if (timer->type == isc_timertype_ticker) - os_time_add(nowp, &timer->interval, &due); + isc_time_add(now, &timer->interval, &due); else { if (ZERO(timer->idle)) due = timer->expires; else if (ZERO(timer->expires)) due = timer->idle; - else if (os_time_compare(&timer->idle, &timer->expires) < 0) + else if (isc_time_compare(&timer->idle, &timer->expires) < 0) due = timer->idle; else due = timer->expires; @@ -118,7 +123,7 @@ schedule(isc_timer_t timer, os_time_t *nowp, isc_boolean_t broadcast_ok) { /* * Already scheduled. */ - cmp = os_time_compare(&due, &timer->due); + cmp = isc_time_compare(&due, &timer->due); timer->due = due; switch (cmp) { case -1: @@ -198,20 +203,20 @@ destroy(isc_timer_t timer) { UNLOCK(&manager->lock); isc_task_detach(&timer->task); - (void)os_mutex_destroy(&timer->lock); + (void)isc_mutex_destroy(&timer->lock); timer->magic = 0; isc_mem_put(manager->mctx, timer, sizeof *timer); } isc_result isc_timer_create(isc_timermgr_t manager, isc_timertype_t type, - os_time_t expires, os_time_t interval, + isc_time_t expires, isc_time_t interval, isc_task_t task, isc_taskaction_t action, void *arg, isc_timer_t *timerp) { isc_timer_t timer; isc_result result; - os_time_t now; + struct isc_time now; /* * Create a new 'type' timer managed by 'manager'. The timers @@ -224,16 +229,16 @@ isc_timer_create(isc_timermgr_t manager, isc_timertype_t type, REQUIRE(VALID_MANAGER(manager)); REQUIRE(task != NULL); REQUIRE(action != NULL); - REQUIRE(!(ZERO(expires) && ZERO(interval))); + REQUIRE(!(ZERO(*expires) && ZERO(*interval))); REQUIRE(timerp != NULL && *timerp == NULL); /* * Get current time. */ - result = os_time_get(&now); + result = isc_time_get(&now); if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_time_get() failed: %s", + "isc_time_get() failed: %s", isc_result_totext(result)); return (ISC_R_UNEXPECTED); } @@ -245,23 +250,24 @@ isc_timer_create(isc_timermgr_t manager, isc_timertype_t type, timer->magic = TIMER_MAGIC; timer->manager = manager; timer->references = 1; - if (type == isc_timertype_once && !ZERO(interval)) - os_time_add(&now, &interval, &timer->idle); + if (type == isc_timertype_once && !ZERO(*interval)) + isc_time_add(&now, interval, &timer->idle); else { timer->idle.seconds = 0; timer->idle.nanoseconds = 0; } timer->type = type; - timer->expires = expires; - timer->interval = interval; + timer->expires = *expires; + timer->interval = *interval; timer->task = NULL; isc_task_attach(task, &timer->task); timer->action = action; timer->arg = arg; timer->index = 0; - if (!os_mutex_init(&timer->lock)) { + if (isc_mutex_init(&timer->lock) != ISC_R_SUCCESS) { isc_mem_put(manager->mctx, timer, sizeof *timer); - UNEXPECTED_ERROR(__FILE__, __LINE__, "os_mutex_init() failed"); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_mutex_init() failed"); return (ISC_R_UNEXPECTED); } @@ -285,9 +291,9 @@ isc_timer_create(isc_timermgr_t manager, isc_timertype_t type, isc_result isc_timer_reset(isc_timer_t timer, isc_timertype_t type, - os_time_t expires, os_time_t interval, isc_boolean_t purge) + isc_time_t expires, isc_time_t interval, isc_boolean_t purge) { - os_time_t now; + struct isc_time now; isc_timermgr_t manager; isc_result result; @@ -300,15 +306,15 @@ isc_timer_reset(isc_timer_t timer, isc_timertype_t type, REQUIRE(VALID_TIMER(timer)); manager = timer->manager; REQUIRE(VALID_MANAGER(manager)); - REQUIRE(!(ZERO(expires) && ZERO(interval))); + REQUIRE(!(ZERO(*expires) && ZERO(*interval))); /* * Get current time. */ - result = os_time_get(&now); + result = isc_time_get(&now); if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_time_get() failed: %s", + "isc_time_get() failed: %s", isc_result_totext(result)); return (ISC_R_UNEXPECTED); } @@ -321,10 +327,10 @@ isc_timer_reset(isc_timer_t timer, isc_timertype_t type, if (purge) isc_task_purge(timer->task, timer, ISC_TASKEVENT_ANYEVENT); timer->type = type; - timer->expires = expires; - timer->interval = interval; - if (type == isc_timertype_once && !ZERO(interval)) - os_time_add(&now, &interval, &timer->idle); + timer->expires = *expires; + timer->interval = *interval; + if (type == isc_timertype_once && !ZERO(*interval)) + isc_time_add(&now, interval, &timer->idle); else { timer->idle.seconds = 0; timer->idle.nanoseconds = 0; @@ -340,7 +346,7 @@ isc_timer_reset(isc_timer_t timer, isc_timertype_t type, isc_result isc_timer_touch(isc_timer_t timer) { isc_result result; - os_time_t now; + struct isc_time now; /* * Set the last-touched time of 'timer' to the current time. @@ -352,14 +358,14 @@ isc_timer_touch(isc_timer_t timer) { INSIST(timer->type == isc_timertype_once); - result = os_time_get(&now); + result = isc_time_get(&now); if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_time_get() failed: %s", + "isc_time_get() failed: %s", isc_result_totext(result)); return (ISC_R_UNEXPECTED); } - os_time_add(&now, &timer->interval, &timer->idle); + isc_time_add(&now, &timer->interval, &timer->idle); UNLOCK(&timer->lock); @@ -409,7 +415,7 @@ isc_timer_detach(isc_timer_t *timerp) { } static void -dispatch(isc_timermgr_t manager, os_time_t *nowp) { +dispatch(isc_timermgr_t manager, isc_time_t now) { isc_boolean_t done = ISC_FALSE, post_event, need_schedule; isc_event_t event; isc_eventtype_t type = 0; @@ -418,19 +424,19 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) { while (manager->nscheduled > 0 && !done) { timer = isc_heap_element(manager->heap, 1); - if (os_time_compare(nowp, &timer->due) >= 0) { + if (isc_time_compare(now, &timer->due) >= 0) { if (timer->type == isc_timertype_ticker) { type = ISC_TIMEREVENT_TICK; post_event = ISC_TRUE; need_schedule = ISC_TRUE; } else if (!ZERO(timer->expires) && - os_time_compare(nowp, + isc_time_compare(now, &timer->expires) >= 0) { type = ISC_TIMEREVENT_LIFE; post_event = ISC_TRUE; need_schedule = ISC_FALSE; } else if (!ZERO(timer->idle) && - os_time_compare(nowp, + isc_time_compare(now, &timer->idle) >= 0) { type = ISC_TIMEREVENT_IDLE; post_event = ISC_TRUE; @@ -454,8 +460,7 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) { sizeof *event); if (event != NULL) - INSIST(isc_task_send(timer->task, - &event)); + isc_task_send(timer->task, &event); else UNEXPECTED_ERROR(__FILE__, __LINE__, "couldn't allocate event"); @@ -466,7 +471,7 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) { manager->nscheduled--; if (need_schedule) { - result = schedule(timer, nowp, ISC_FALSE); + result = schedule(timer, now, ISC_FALSE); if (result != ISC_R_SUCCESS) UNEXPECTED_ERROR(__FILE__, __LINE__, "couldn't schedule timer: %s", @@ -482,12 +487,12 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) { static void * run(void *uap) { isc_timermgr_t manager = uap; - isc_boolean_t timeout; - os_time_t now; + struct isc_time now; + isc_result_t result; LOCK(&manager->lock); while (!manager->done) { - INSIST(os_time_get(&now) == ISC_R_SUCCESS); + INSIST(isc_time_get(&now) == ISC_R_SUCCESS); XTRACETIME("running", now); @@ -495,8 +500,10 @@ run(void *uap) { if (manager->nscheduled > 0) { XTRACETIME("waituntil", manager->due); - WAITUNTIL(&manager->wakeup, &manager->lock, - &manager->due, &timeout); + result = WAITUNTIL(&manager->wakeup, &manager->lock, + &manager->due); + INSIST(result == ISC_R_SUCCESS || + result == ISC_R_TIMEDOUT); } else { XTRACE("wait"); WAIT(&manager->wakeup, &manager->lock); @@ -517,7 +524,7 @@ sooner(void *v1, void *v2) { REQUIRE(VALID_TIMER(t1)); REQUIRE(VALID_TIMER(t2)); - if (os_time_compare(&t1->due, &t2->due) < 0) + if (isc_time_compare(&t1->due, &t2->due) < 0) return (ISC_TRUE); return (ISC_FALSE); } @@ -561,27 +568,29 @@ isc_timermgr_create(isc_memctx_t mctx, isc_timermgr_t *managerp) { isc_mem_put(mctx, manager, sizeof *manager); return (ISC_R_NOMEMORY); } - if (!os_mutex_init(&manager->lock)) { - isc_heap_destroy(&manager->heap); - isc_mem_put(mctx, manager, sizeof *manager); - UNEXPECTED_ERROR(__FILE__, __LINE__, "os_mutex_init() failed"); - return (ISC_R_UNEXPECTED); - } - if (!os_condition_init(&manager->wakeup)) { - (void)os_mutex_destroy(&manager->lock); + if (isc_mutex_init(&manager->lock) != ISC_R_SUCCESS) { isc_heap_destroy(&manager->heap); isc_mem_put(mctx, manager, sizeof *manager); UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_condition_init() failed"); + "isc_mutex_init() failed"); return (ISC_R_UNEXPECTED); } - if (!os_thread_create(run, manager, &manager->thread)) { - (void)os_condition_destroy(&manager->wakeup); - (void)os_mutex_destroy(&manager->lock); + if (isc_condition_init(&manager->wakeup) != ISC_R_SUCCESS) { + (void)isc_mutex_destroy(&manager->lock); isc_heap_destroy(&manager->heap); isc_mem_put(mctx, manager, sizeof *manager); UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_thread_create() failed"); + "isc_condition_init() failed"); + return (ISC_R_UNEXPECTED); + } + if (isc_thread_create(run, manager, &manager->thread) != + ISC_R_SUCCESS) { + (void)isc_condition_destroy(&manager->wakeup); + (void)isc_mutex_destroy(&manager->lock); + isc_heap_destroy(&manager->heap); + isc_mem_put(mctx, manager, sizeof *manager); + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_thread_create() failed"); return (ISC_R_UNEXPECTED); } @@ -615,15 +624,15 @@ isc_timermgr_destroy(isc_timermgr_t *managerp) { /* * Wait for thread to exit. */ - if (!os_thread_join(manager->thread)) + if (isc_thread_join(manager->thread) != ISC_R_SUCCESS) UNEXPECTED_ERROR(__FILE__, __LINE__, - "os_thread_join() failed"); + "isc_thread_join() failed"); /* * Clean up. */ - (void)os_condition_destroy(&manager->wakeup); - (void)os_mutex_destroy(&manager->lock); + (void)isc_condition_destroy(&manager->wakeup); + (void)isc_mutex_destroy(&manager->lock); isc_heap_destroy(&manager->heap); manager->magic = 0; isc_mem_put(manager->mctx, manager, sizeof *manager); diff --git a/lib/isc/unix/include/isc/time.h b/lib/isc/unix/include/isc/time.h index 1d1f00f0f3..850a826fa2 100644 --- a/lib/isc/unix/include/isc/time.h +++ b/lib/isc/unix/include/isc/time.h @@ -10,19 +10,19 @@ * to represent intervals. */ -typedef struct os_time_t { +typedef struct isc_time { time_t seconds; long nanoseconds; -} os_time_t; +} *isc_time_t; -isc_result -os_time_get(os_time_t *timep); +isc_result_t +isc_time_get(isc_time_t t); /* - * Set *timep to the current absolute time (secs + nsec since January 1, 1970). + * Set 't' to the current absolute time (secs + nsec since January 1, 1970). * * Requires: * - * 'timep' is a valid pointer. + * 't' is a valid pointer. * * Returns: * @@ -31,41 +31,41 @@ os_time_get(os_time_t *timep); */ int -os_time_compare(os_time_t *t1p, os_time_t *t2p); +isc_time_compare(isc_time_t t1, isc_time_t t2); /* - * Compare the times referenced by 't1p' and 't2p' + * Compare the times referenced by 't1' and 't2' * * Requires: * - * 't1p' and 't2p' are a valid. + * 't1' and 't2' are a valid. * * Returns: * - * -1 *tp1 < *t2p - * 0 *tp1 = *t2p - * 1 *tp1 > *t2p + * -1 t1 < t2 (comparing times, not pointers) + * 0 t1 = t2 + * 1 t1 > t2 */ void -os_time_add(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p); +isc_time_add(isc_time_t t1, isc_time_t t2, isc_time_t t3); /* - * Add 't1p' to 't2p', storing the result in 't3p'. + * Add 't1' to 't2', storing the result in 't3'. * * Requires: * - * 't1p', 't2p', and 't3p' are valid. + * 't1', 't2', and 't3' are valid. */ void -os_time_subtract(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p); +isc_time_subtract(isc_time_t t1, isc_time_t t2, isc_time_t t3); /* - * Subtract 't2p' from 't1p', storing the result in 't3p'. + * Subtract 't2' from 't1', storing the result in 't3'. * * Requires: * - * 't1p', 't2p', and 't3p' are valid. + * 't1', 't2', and 't3' are valid. * - * *tp1 >= *t2p + * t1 >= t2 (comparing times, not pointers) */ #endif /* ISC_TIME_H */ diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c index 41be11a85a..81d2d42b88 100644 --- a/lib/isc/unix/time.c +++ b/lib/isc/unix/time.c @@ -5,13 +5,12 @@ #include #include -#include #include #include #include isc_result -os_time_get(os_time_t *timep) { +isc_time_get(isc_time_t timep) { struct timeval tv; /* @@ -33,56 +32,56 @@ os_time_get(os_time_t *timep) { } int -os_time_compare(os_time_t *t1p, os_time_t *t2p) { +isc_time_compare(isc_time_t t1, isc_time_t t2) { /* - * Compare the times referenced by 't1p' and 't2p' + * Compare the times referenced by 't1' and 't2' */ - REQUIRE(t1p != NULL && t2p != NULL); + REQUIRE(t1 != NULL && t2 != NULL); - if (t1p->seconds < t2p->seconds) + if (t1->seconds < t2->seconds) return (-1); - if (t1p->seconds > t2p->seconds) + if (t1->seconds > t2->seconds) return (1); - if (t1p->nanoseconds < t2p->nanoseconds) + if (t1->nanoseconds < t2->nanoseconds) return (-1); - if (t1p->nanoseconds > t2p->nanoseconds) + if (t1->nanoseconds > t2->nanoseconds) return (1); return (0); } void -os_time_add(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p) +isc_time_add(isc_time_t t1, isc_time_t t2, isc_time_t t3) { /* - * Add 't1p' to 't2p', storing the result in 't3p'. + * Add 't1' to 't2', storing the result in 't3'. */ - REQUIRE(t1p != NULL && t2p != NULL && t3p != NULL); + REQUIRE(t1 != NULL && t2 != NULL && t3 != NULL); - t3p->seconds = t1p->seconds + t2p->seconds; - t3p->nanoseconds = t1p->nanoseconds + t2p->nanoseconds; - if (t3p->nanoseconds > 1000000000) { - t3p->seconds++; - t3p->nanoseconds -= 1000000000; + t3->seconds = t1->seconds + t2->seconds; + t3->nanoseconds = t1->nanoseconds + t2->nanoseconds; + if (t3->nanoseconds > 1000000000) { + t3->seconds++; + t3->nanoseconds -= 1000000000; } } void -os_time_subtract(os_time_t *t1p, os_time_t *t2p, os_time_t *t3p) { +isc_time_subtract(isc_time_t t1, isc_time_t t2, isc_time_t t3) { /* - * Subtract 't2p' from 't1p', storing the result in 't1p'. + * Subtract 't2' from 't1', storing the result in 't1'. */ - REQUIRE(t1p != NULL && t2p != NULL && t3p != NULL); - REQUIRE(os_time_compare(t1p, t2p) >= 0); + REQUIRE(t1 != NULL && t2 != NULL && t3 != NULL); + REQUIRE(isc_time_compare(t1, t2) >= 0); - t3p->seconds = t1p->seconds - t2p->seconds; - if (t1p->nanoseconds >= t2p->nanoseconds) - t3p->nanoseconds = t1p->nanoseconds - t2p->nanoseconds; + t3->seconds = t1->seconds - t2->seconds; + if (t1->nanoseconds >= t2->nanoseconds) + t3->nanoseconds = t1->nanoseconds - t2->nanoseconds; else { - t3p->nanoseconds = 1000000000 - t2p->nanoseconds + - t1p->nanoseconds; - t3p->seconds--; + t3->nanoseconds = 1000000000 - t2->nanoseconds + + t1->nanoseconds; + t3->seconds--; } }