2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-30 05:57:52 +00:00

more renaming

This commit is contained in:
Bob Halley 1998-10-22 01:33:20 +00:00
parent de9282a1ea
commit bf6d2e3912
14 changed files with 333 additions and 256 deletions

View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <isc/assertions.h>
#include <isc/memcluster.h> #include <isc/memcluster.h>
#include <isc/task.h> #include <isc/task.h>
#include <isc/thread.h> #include <isc/thread.h>
@ -53,7 +54,7 @@ main(int argc, char *argv[]) {
unsigned int workers; unsigned int workers;
isc_timermgr_t timgr; isc_timermgr_t timgr;
isc_timer_t ti1, ti2; isc_timer_t ti1, ti2;
os_time_t absolute, interval; struct isc_time absolute, interval;
if (argc > 1) if (argc > 1)
workers = atoi(argv[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_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, "1", 0, &t1) ==
INSIST(isc_task_create(manager, my_shutdown, "2", 0, &t2)); ISC_R_SUCCESS);
INSIST(isc_task_create(manager, my_shutdown, "3", 0, &t3)); INSIST(isc_task_create(manager, my_shutdown, "2", 0, &t2) ==
INSIST(isc_task_create(manager, my_shutdown, "4", 0, &t4)); 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; timgr = NULL;
INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS); INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
@ -78,11 +84,11 @@ main(int argc, char *argv[]) {
interval.seconds = 1; interval.seconds = 1;
interval.nanoseconds = 0; interval.nanoseconds = 0;
INSIST(isc_timer_create(timgr, isc_timertype_ticker, INSIST(isc_timer_create(timgr, isc_timertype_ticker,
absolute, interval, &absolute, &interval,
t1, my_tick, "foo", &ti1) == ISC_R_SUCCESS); t1, my_tick, "foo", &ti1) == ISC_R_SUCCESS);
ti2 = NULL; ti2 = NULL;
INSIST(isc_timer_create(timgr, isc_timertype_ticker, INSIST(isc_timer_create(timgr, isc_timertype_ticker,
absolute, interval, &absolute, &interval,
t2, my_tick, "bar", &ti2) == ISC_R_SUCCESS); t2, my_tick, "bar", &ti2) == ISC_R_SUCCESS);
printf("task 1 = %p\n", t1); printf("task 1 = %p\n", t1);

View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <isc/assertions.h>
#include <isc/memcluster.h> #include <isc/memcluster.h>
#include <isc/task.h> #include <isc/task.h>
#include <isc/thread.h> #include <isc/thread.h>
@ -38,17 +39,17 @@ tick(isc_task_t task, isc_event_t event)
isc_timer_touch(ti3); isc_timer_touch(ti3);
if (tick_count == 7) { 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.seconds = 5;
expires.nanoseconds = 0; expires.nanoseconds = 0;
os_time_add(&now, &expires, &expires); isc_time_add(&now, &expires, &expires);
interval.seconds = 4; interval.seconds = 4;
interval.nanoseconds = 0; interval.nanoseconds = 0;
printf("*** resetting ti3 ***\n"); printf("*** resetting ti3 ***\n");
INSIST(isc_timer_reset(ti3, isc_timertype_once, expires, INSIST(isc_timer_reset(ti3, isc_timertype_once, &expires,
interval, ISC_TRUE) &interval, ISC_TRUE)
== ISC_R_SUCCESS); == ISC_R_SUCCESS);
} }
@ -82,7 +83,7 @@ main(int argc, char *argv[]) {
isc_taskmgr_t manager = NULL; isc_taskmgr_t manager = NULL;
isc_timermgr_t timgr = NULL; isc_timermgr_t timgr = NULL;
unsigned int workers; unsigned int workers;
os_time_t expires, interval, now; struct isc_time expires, interval, now;
if (argc > 1) if (argc > 1)
workers = atoi(argv[1]); workers = atoi(argv[1]);
@ -91,36 +92,41 @@ main(int argc, char *argv[]) {
printf("%d workers\n", workers); printf("%d workers\n", workers);
INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS); 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) ==
INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1)); ISC_R_SUCCESS);
INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2)); INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1) ==
INSIST(isc_task_create(manager, shutdown_task, "3", 0, &t3)); 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); INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
printf("task 1: %p\n", t1); printf("task 1: %p\n", t1);
printf("task 2: %p\n", t2); printf("task 2: %p\n", t2);
printf("task 3: %p\n", t3); printf("task 3: %p\n", t3);
(void)os_time_get(&now); (void)isc_time_get(&now);
expires.seconds = 0; expires.seconds = 0;
expires.nanoseconds = 0; expires.nanoseconds = 0;
interval.seconds = 2; interval.seconds = 2;
interval.nanoseconds = 0; 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); t2, timeout, "2", &ti2) == ISC_R_SUCCESS);
expires.seconds = 0; expires.seconds = 0;
expires.nanoseconds = 0; expires.nanoseconds = 0;
interval.seconds = 1; interval.seconds = 1;
interval.nanoseconds = 0; 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); t1, tick, "1", &ti1) == ISC_R_SUCCESS);
expires.seconds = 10; expires.seconds = 10;
expires.nanoseconds = 0; expires.nanoseconds = 0;
os_time_add(&now, &expires, &expires); isc_time_add(&now, &expires, &expires);
interval.seconds = 2; interval.seconds = 2;
interval.nanoseconds = 0; 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); t3, timeout, "3", &ti3) == ISC_R_SUCCESS);
isc_task_detach(&t1); isc_task_detach(&t1);

View File

@ -9,6 +9,8 @@ typedef unsigned int isc_result_t;
#define ISC_R_SUCCESS 0 #define ISC_R_SUCCESS 0
#define ISC_R_NOMEMORY 1 #define ISC_R_NOMEMORY 1
#define ISC_R_TIMEDOUT 2
#define ISC_R_NOTHREADS 3
#define ISC_R_UNEXPECTED 0xFFFFFFFFL #define ISC_R_UNEXPECTED 0xFFFFFFFFL
#define isc_result_totext __isc_result_totext #define isc_result_totext __isc_result_totext

View File

@ -8,6 +8,7 @@
#include <isc/memcluster.h> #include <isc/memcluster.h>
#include <isc/boolean.h> #include <isc/boolean.h>
#include <isc/result.h>
/*** /***
@ -64,16 +65,16 @@ void isc_event_free(isc_event_t *);
*** Tasks. *** Tasks.
***/ ***/
isc_boolean_t isc_task_create(isc_taskmgr_t, isc_result_t isc_task_create(isc_taskmgr_t,
isc_taskaction_t, isc_taskaction_t,
void *, void *,
unsigned int, unsigned int,
isc_task_t *); isc_task_t *);
void isc_task_attach(isc_task_t, void isc_task_attach(isc_task_t,
isc_task_t *); isc_task_t *);
void isc_task_detach(isc_task_t *); void isc_task_detach(isc_task_t *);
isc_boolean_t isc_task_send(isc_task_t, void isc_task_send(isc_task_t,
isc_event_t *); isc_event_t *);
void isc_task_purge(isc_task_t, void *, void isc_task_purge(isc_task_t, void *,
isc_eventtype_t); isc_eventtype_t);
void isc_task_shutdown(isc_task_t); void isc_task_shutdown(isc_task_t);
@ -83,7 +84,7 @@ void isc_task_destroy(isc_task_t *);
*** Task Manager. *** Task Manager.
***/ ***/
unsigned int isc_taskmgr_create(isc_memctx_t, isc_result_t isc_taskmgr_create(isc_memctx_t,
unsigned int, unsigned int,
unsigned int, unsigned int,
isc_taskmgr_t *); isc_taskmgr_t *);

View File

@ -91,8 +91,8 @@ typedef struct isc_timerevent {
isc_result_t isc_result_t
isc_timer_create(isc_timermgr_t manager, isc_timer_create(isc_timermgr_t manager,
isc_timertype_t type, isc_timertype_t type,
os_time_t expires, isc_time_t expires,
os_time_t interval, isc_time_t interval,
isc_task_t task, isc_task_t task,
isc_taskaction_t action, isc_taskaction_t action,
void *arg, void *arg,
@ -146,8 +146,8 @@ isc_timer_create(isc_timermgr_t manager,
isc_result_t isc_result_t
isc_timer_reset(isc_timer_t timer, isc_timer_reset(isc_timer_t timer,
isc_timertype_t type, isc_timertype_t type,
os_time_t expires, isc_time_t expires,
os_time_t interval, isc_time_t interval,
isc_boolean_t purge); isc_boolean_t purge);
/* /*
* Change the timer's type, expires, and interval values to the given * Change the timer's type, expires, and interval values to the given

View File

@ -32,7 +32,7 @@
#endif #endif
#if !defined(LINT) && !defined(CODECENTER) #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 */ #endif /* not lint */
/* /*
@ -68,7 +68,7 @@ struct isc_memctx {
unsigned char * lowest; unsigned char * lowest;
unsigned char * highest; unsigned char * highest;
struct stats * stats; struct stats * stats;
os_mutex_t mutex; isc_mutex_t mutex;
}; };
/* Forward. */ /* Forward. */
@ -84,8 +84,10 @@ static size_t quantize(size_t);
#define TABLE_INCREMENT 1024 #define TABLE_INCREMENT 1024
#ifdef MULTITHREADED #ifdef MULTITHREADED
#define LOCK_CONTEXT(ctx) INSIST(os_mutex_lock(&(ctx)->mutex)) #define LOCK_CONTEXT(ctx) \
#define UNLOCK_CONTEXT(ctx) INSIST(os_mutex_unlock(&(ctx)->mutex)) INSIST(isc_mutex_lock(&(ctx)->mutex) == ISC_R_SUCCESS)
#define UNLOCK_CONTEXT(ctx) \
INSIST(isc_mutex_unlock(&(ctx)->mutex) == ISC_R_SUCCESS)
#else #else
#define LOCK_CONTEXT(ctx) #define LOCK_CONTEXT(ctx)
#define UNLOCK_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->basic_table_size = 0;
ctx->lowest = NULL; ctx->lowest = NULL;
ctx->highest = NULL; ctx->highest = NULL;
if (!os_mutex_init(&ctx->mutex)) { if (isc_mutex_init(&ctx->mutex) != ISC_R_SUCCESS) {
free(ctx->stats); free(ctx->stats);
free(ctx->freelists); free(ctx->freelists);
free(ctx); free(ctx);
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_mutex_init() failed"); "isc_mutex_init() failed");
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
*ctxp = ctx; *ctxp = ctx;
@ -181,7 +183,7 @@ isc_memctx_destroy(isc_memctx_t *ctxp) {
free(ctx->freelists); free(ctx->freelists);
free(ctx->stats); free(ctx->stats);
free(ctx->basic_table); free(ctx->basic_table);
(void)os_mutex_destroy(&ctx->mutex); (void)isc_mutex_destroy(&ctx->mutex);
free(ctx); free(ctx);
*ctxp = NULL; *ctxp = NULL;
@ -453,8 +455,7 @@ meminit(size_t init_max_size, size_t target_size) {
/* need default_context lock here */ /* need default_context lock here */
if (default_context != NULL) if (default_context != NULL)
return (-1); return (-1);
return (isc_mem_create(init_max_size, target_size, return (isc_mem_create(init_max_size, target_size, &default_context));
&default_context));
} }
isc_memctx_t isc_memctx_t

View File

@ -1,23 +1,26 @@
#include <errno.h>
#include <string.h>
#include <isc/condition.h> #include <isc/condition.h>
#include <errno.h> #include <isc/unexpect.h>
isc_boolean_t isc_result_t
os_condition_waituntil(os_condition_t *c, os_mutex_t *m, os_time_t *t, isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t t)
isc_boolean_t *timeout)
{ {
int result; int presult;
struct timespec ts; struct timespec ts;
ts.tv_sec = t->seconds; ts.tv_sec = t->seconds;
ts.tv_nsec = t->nanoseconds; ts.tv_nsec = t->nanoseconds;
result = pthread_cond_timedwait(c, m, &ts); presult = pthread_cond_timedwait(c, m, &ts);
if (result == 0) { if (presult == 0)
*timeout = ISC_FALSE; return (ISC_R_SUCCESS);
return (ISC_TRUE); if (presult == ETIMEDOUT)
} else if (result == ETIMEDOUT) { return (ISC_R_TIMEDOUT);
*timeout = ISC_TRUE;
return (ISC_TRUE); UNEXPECTED_ERROR(__FILE__, __LINE__,
} "pthread_cond_timedwait() returned %s",
return (ISC_FALSE); strerror(presult));
return (ISC_R_UNEXPECTED);
} }

View File

@ -1,25 +1,37 @@
#ifndef CONDITION_H #ifndef ISC_CONDITION_H
#define CONDITION_H 1 #define ISC_CONDITION_H 1
#include <pthread.h> #include <pthread.h>
#include <isc/boolean.h> #include <isc/boolean.h>
#include <isc/result.h>
#include <isc/mutex.h> #include <isc/mutex.h>
#include <isc/time.h> #include <isc/time.h>
typedef pthread_cond_t os_condition_t; typedef pthread_cond_t isc_condition_t;
#define OS_CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER
#define os_condition_init(cp) (pthread_cond_init((cp), NULL) == 0) #define isc_condition_init(cp) \
#define os_condition_wait(cp, mp) (pthread_cond_wait((cp), (mp)) == 0) ((pthread_cond_init((cp), NULL) == 0) ? \
#define os_condition_signal(cp) (pthread_cond_signal((cp)) == 0) ISC_R_SUCCESS : ISC_R_UNEXPECTED)
#define os_condition_broadcast(cp) (pthread_cond_broadcast((cp)) == 0)
#define os_condition_destroy(cp) (pthread_cond_destroy((cp)) == 0)
isc_boolean_t os_condition_waituntil(os_condition_t *, #define isc_condition_wait(cp, mp) \
os_mutex_t *, ((pthread_cond_wait((cp), (mp)) == 0) ? \
os_time_t *, ISC_R_SUCCESS : ISC_R_UNEXPECTED)
isc_boolean_t *);
#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 */

View File

@ -1,15 +1,26 @@
#ifndef MUTEX_H #ifndef ISC_MUTEX_H
#define MUTEX_H 1 #define ISC_MUTEX_H 1
#include <pthread.h> #include <pthread.h>
typedef pthread_mutex_t os_mutex_t; #include <isc/result.h>
#define OS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#define os_mutex_init(mp) (pthread_mutex_init((mp), NULL) == 0) typedef pthread_mutex_t isc_mutex_t;
#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)
#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 */

View File

@ -1,17 +1,28 @@
#ifndef THREAD_H #ifndef ISC_THREAD_H
#define THREAD_H 1 #define ISC_THREAD_H 1
#include <pthread.h> #include <pthread.h>
#include <isc/assertions.h> #include <isc/result.h>
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)) \ /* XXX We could do fancier error handling... */
== 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
#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 */

View File

@ -6,7 +6,7 @@
#include <isc/thread.h> #include <isc/thread.h>
#include <isc/mutex.h> #include <isc/mutex.h>
#include <isc/condition.h> #include <isc/condition.h>
#include <isc/unexpect.h>
#include <isc/task.h> #include <isc/task.h>
@ -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. * the capital letters make the locking stand out.
* *
* We INSIST that they succeed since there's no way for us to continue * We INSIST that they succeed since there's no way for us to continue
* if they fail. * if they fail.
*/ */
#define LOCK(lp) INSIST(os_mutex_lock((lp)))
#define UNLOCK(lp) INSIST(os_mutex_unlock((lp))) #define LOCK(lp) \
#define WAIT(cvp, lp) INSIST(os_condition_wait((cvp), (lp))) INSIST(isc_mutex_lock((lp)) == ISC_R_SUCCESS);
#define BROADCAST(cvp) INSIST(os_condition_broadcast((cvp))) #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 #ifdef ISC_TASK_TRACE
#define XTRACE(m) printf("%s task %p thread %p\n", (m), \ #define XTRACE(m) printf("%s task %p thread %p\n", (m), \
task, os_thread_self()) task, isc_thread_self())
#else #else
#define XTRACE(m) #define XTRACE(m)
#endif #endif
@ -51,7 +59,7 @@ struct isc_task {
/* Not locked. */ /* Not locked. */
unsigned int magic; unsigned int magic;
isc_taskmgr_t manager; isc_taskmgr_t manager;
os_mutex_t lock; isc_mutex_t lock;
/* Locked by task lock. */ /* Locked by task lock. */
task_state_t state; task_state_t state;
unsigned int references; unsigned int references;
@ -72,15 +80,15 @@ struct isc_taskmgr {
/* Not locked. */ /* Not locked. */
unsigned int magic; unsigned int magic;
isc_memctx_t mctx; isc_memctx_t mctx;
os_mutex_t lock; isc_mutex_t lock;
/* Locked by task manager lock. */ /* Locked by task manager lock. */
unsigned int default_quantum; unsigned int default_quantum;
LIST(struct isc_task) tasks; LIST(struct isc_task) tasks;
LIST(struct isc_task) ready_tasks; LIST(struct isc_task) ready_tasks;
os_condition_t work_available; isc_condition_t work_available;
isc_boolean_t exiting; isc_boolean_t exiting;
unsigned int workers; unsigned int workers;
os_condition_t no_workers; isc_condition_t no_workers;
}; };
#define DEFAULT_DEFAULT_QUANTUM 5 #define DEFAULT_DEFAULT_QUANTUM 5
@ -160,14 +168,14 @@ task_free(isc_task_t task) {
BROADCAST(&manager->work_available); BROADCAST(&manager->work_available);
} }
UNLOCK(&manager->lock); UNLOCK(&manager->lock);
(void)os_mutex_destroy(&task->lock); (void)isc_mutex_destroy(&task->lock);
if (task->shutdown_event != NULL) if (task->shutdown_event != NULL)
isc_event_free(&task->shutdown_event); isc_event_free(&task->shutdown_event);
task->magic = 0; task->magic = 0;
isc_mem_put(manager->mctx, task, sizeof *task); 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, isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action,
void *shutdown_arg, unsigned int quantum, isc_task_t *taskp) 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); task = isc_mem_get(manager->mctx, sizeof *task);
if (task == NULL) if (task == NULL)
return (ISC_FALSE); return (ISC_R_NOMEMORY);
task->magic = TASK_MAGIC; task->magic = TASK_MAGIC;
task->manager = manager; 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); 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->state = task_state_idle;
task->references = 1; task->references = 1;
@ -198,9 +208,9 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action,
shutdown_arg, shutdown_arg,
sizeof *task->shutdown_event); sizeof *task->shutdown_event);
if (task->shutdown_event == NULL) { 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); isc_mem_put(manager->mctx, task, sizeof *task);
return (ISC_FALSE); return (ISC_R_NOMEMORY);
} }
INIT_LINK(task, link); INIT_LINK(task, link);
INIT_LINK(task, ready_link); INIT_LINK(task, ready_link);
@ -213,7 +223,7 @@ isc_task_create(isc_taskmgr_t manager, isc_taskaction_t shutdown_action,
*taskp = task; *taskp = task;
return (ISC_TRUE); return (ISC_R_SUCCESS);
} }
void void
@ -253,7 +263,7 @@ isc_task_detach(isc_task_t *taskp) {
*taskp = NULL; *taskp = NULL;
} }
isc_boolean_t void
isc_task_send(isc_task_t task, isc_event_t *eventp) { isc_task_send(isc_task_t task, isc_event_t *eventp) {
isc_boolean_t was_idle = ISC_FALSE; isc_boolean_t was_idle = ISC_FALSE;
isc_boolean_t discard = 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) { if (discard) {
isc_event_free(&event); isc_event_free(&event);
*eventp = NULL; *eventp = NULL;
return (ISC_TRUE); return;
} }
if (was_idle) { if (was_idle) {
@ -335,7 +345,6 @@ isc_task_send(isc_task_t task, isc_event_t *eventp) {
*eventp = NULL; *eventp = NULL;
XTRACE("sent"); XTRACE("sent");
return (ISC_TRUE);
} }
void void
@ -685,49 +694,55 @@ void *run(void *uap) {
static void static void
manager_free(isc_taskmgr_t manager) { manager_free(isc_taskmgr_t manager) {
(void)os_condition_destroy(&manager->work_available); (void)isc_condition_destroy(&manager->work_available);
(void)os_condition_destroy(&manager->no_workers); (void)isc_condition_destroy(&manager->no_workers);
(void)os_mutex_destroy(&manager->lock); (void)isc_mutex_destroy(&manager->lock);
manager->magic = 0; manager->magic = 0;
isc_mem_put(manager->mctx, manager, sizeof *manager); isc_mem_put(manager->mctx, manager, sizeof *manager);
} }
unsigned int isc_result_t
isc_taskmgr_create(isc_memctx_t mctx, unsigned int workers, isc_taskmgr_create(isc_memctx_t mctx, unsigned int workers,
unsigned int default_quantum, isc_taskmgr_t *managerp) unsigned int default_quantum, isc_taskmgr_t *managerp)
{ {
unsigned int i, started = 0; unsigned int i, started = 0;
isc_taskmgr_t manager; 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); manager = isc_mem_get(mctx, sizeof *manager);
if (manager == NULL) if (manager == NULL)
return (0); return (ISC_R_NOMEMORY);
manager->magic = TASK_MANAGER_MAGIC; manager->magic = TASK_MANAGER_MAGIC;
manager->mctx = mctx; 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); 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) if (default_quantum == 0)
default_quantum = DEFAULT_DEFAULT_QUANTUM; default_quantum = DEFAULT_DEFAULT_QUANTUM;
manager->default_quantum = default_quantum; manager->default_quantum = default_quantum;
INIT_LIST(manager->tasks); INIT_LIST(manager->tasks);
INIT_LIST(manager->ready_tasks); INIT_LIST(manager->ready_tasks);
if (!os_condition_init(&manager->work_available)) { if (isc_condition_init(&manager->work_available) != ISC_R_SUCCESS) {
(void)os_mutex_destroy(&manager->lock); (void)isc_mutex_destroy(&manager->lock);
isc_mem_put(mctx, manager, sizeof *manager); 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->exiting = ISC_FALSE;
manager->workers = 0; manager->workers = 0;
if (!os_condition_init(&manager->no_workers)) { if (isc_condition_init(&manager->no_workers) != ISC_R_SUCCESS) {
(void)os_condition_destroy(&manager->work_available); (void)isc_condition_destroy(&manager->work_available);
(void)os_mutex_destroy(&manager->lock); (void)isc_mutex_destroy(&manager->lock);
isc_mem_put(mctx, manager, sizeof *manager); isc_mem_put(mctx, manager, sizeof *manager);
return (0); UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_condition_init() failed");
return (ISC_R_UNEXPECTED);
} }
LOCK(&manager->lock); LOCK(&manager->lock);
@ -735,22 +750,23 @@ isc_taskmgr_create(isc_memctx_t mctx, unsigned int workers,
* Start workers. * Start workers.
*/ */
for (i = 0; i < workers; i++) { 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++; manager->workers++;
started++; started++;
(void)os_thread_detach(thread); (void)isc_thread_detach(thread);
} }
} }
UNLOCK(&manager->lock); UNLOCK(&manager->lock);
if (started == 0) { if (started == 0) {
manager_free(manager); manager_free(manager);
return (0); return (ISC_R_NOTHREADS);
} }
*managerp = manager; *managerp = manager;
return (started); return (ISC_R_SUCCESS);
} }
void void

View File

@ -11,18 +11,23 @@
#include <isc/timer.h> #include <isc/timer.h>
/* /*
* 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. * the capital letters make the locking stand out.
* *
* We INSIST that they succeed since there's no way for us to continue * We INSIST that they succeed since there's no way for us to continue
* if they fail. * if they fail.
*/ */
#define LOCK(lp) INSIST(os_mutex_lock((lp)))
#define UNLOCK(lp) INSIST(os_mutex_unlock((lp))) #define LOCK(lp) \
#define BROADCAST(cvp) INSIST(os_condition_broadcast((cvp))) INSIST(isc_mutex_lock((lp)) == ISC_R_SUCCESS);
#define WAIT(cvp, lp) INSIST(os_condition_wait((cvp), (lp))) #define UNLOCK(lp) \
#define WAITUNTIL(cvp, lp, tp, bp) INSIST(os_condition_waituntil((cvp), \ INSIST(isc_mutex_unlock((lp)) == ISC_R_SUCCESS);
(lp), (tp), (bp))) #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 && \ #define ZERO(t) ((t).seconds == 0 && \
(t).nanoseconds == 0) (t).nanoseconds == 0)
@ -48,19 +53,19 @@ struct isc_timer {
/* Not locked. */ /* Not locked. */
unsigned int magic; unsigned int magic;
isc_timermgr_t manager; isc_timermgr_t manager;
os_mutex_t lock; isc_mutex_t lock;
/* Locked by timer lock. */ /* Locked by timer lock. */
unsigned int references; unsigned int references;
os_time_t idle; struct isc_time idle;
/* Locked by manager lock. */ /* Locked by manager lock. */
isc_timertype_t type; isc_timertype_t type;
os_time_t expires; struct isc_time expires;
os_time_t interval; struct isc_time interval;
isc_task_t task; isc_task_t task;
isc_taskaction_t action; isc_taskaction_t action;
void * arg; void * arg;
unsigned int index; unsigned int index;
os_time_t due; struct isc_time due;
LINK(struct isc_timer) link; LINK(struct isc_timer) link;
}; };
@ -72,22 +77,22 @@ struct isc_timermgr {
/* Not locked. */ /* Not locked. */
unsigned int magic; unsigned int magic;
isc_memctx_t mctx; isc_memctx_t mctx;
os_mutex_t lock; isc_mutex_t lock;
/* Locked by manager lock. */ /* Locked by manager lock. */
isc_boolean_t done; isc_boolean_t done;
LIST(struct isc_timer) timers; LIST(struct isc_timer) timers;
unsigned int nscheduled; unsigned int nscheduled;
os_time_t due; struct isc_time due;
os_condition_t wakeup; isc_condition_t wakeup;
os_thread_t thread; isc_thread_t thread;
isc_heap_t heap; isc_heap_t heap;
}; };
static inline isc_result 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_result result;
isc_timermgr_t manager; isc_timermgr_t manager;
os_time_t due; struct isc_time due;
int cmp; 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. * Compute the new due time.
*/ */
if (timer->type == isc_timertype_ticker) if (timer->type == isc_timertype_ticker)
os_time_add(nowp, &timer->interval, &due); isc_time_add(now, &timer->interval, &due);
else { else {
if (ZERO(timer->idle)) if (ZERO(timer->idle))
due = timer->expires; due = timer->expires;
else if (ZERO(timer->expires)) else if (ZERO(timer->expires))
due = timer->idle; 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; due = timer->idle;
else else
due = timer->expires; due = timer->expires;
@ -118,7 +123,7 @@ schedule(isc_timer_t timer, os_time_t *nowp, isc_boolean_t broadcast_ok) {
/* /*
* Already scheduled. * Already scheduled.
*/ */
cmp = os_time_compare(&due, &timer->due); cmp = isc_time_compare(&due, &timer->due);
timer->due = due; timer->due = due;
switch (cmp) { switch (cmp) {
case -1: case -1:
@ -198,20 +203,20 @@ destroy(isc_timer_t timer) {
UNLOCK(&manager->lock); UNLOCK(&manager->lock);
isc_task_detach(&timer->task); isc_task_detach(&timer->task);
(void)os_mutex_destroy(&timer->lock); (void)isc_mutex_destroy(&timer->lock);
timer->magic = 0; timer->magic = 0;
isc_mem_put(manager->mctx, timer, sizeof *timer); isc_mem_put(manager->mctx, timer, sizeof *timer);
} }
isc_result isc_result
isc_timer_create(isc_timermgr_t manager, isc_timertype_t type, 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_task_t task, isc_taskaction_t action, void *arg,
isc_timer_t *timerp) isc_timer_t *timerp)
{ {
isc_timer_t timer; isc_timer_t timer;
isc_result result; isc_result result;
os_time_t now; struct isc_time now;
/* /*
* Create a new 'type' timer managed by 'manager'. The timers * 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(VALID_MANAGER(manager));
REQUIRE(task != NULL); REQUIRE(task != NULL);
REQUIRE(action != NULL); REQUIRE(action != NULL);
REQUIRE(!(ZERO(expires) && ZERO(interval))); REQUIRE(!(ZERO(*expires) && ZERO(*interval)));
REQUIRE(timerp != NULL && *timerp == NULL); REQUIRE(timerp != NULL && *timerp == NULL);
/* /*
* Get current time. * Get current time.
*/ */
result = os_time_get(&now); result = isc_time_get(&now);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_time_get() failed: %s", "isc_time_get() failed: %s",
isc_result_totext(result)); isc_result_totext(result));
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
@ -245,23 +250,24 @@ isc_timer_create(isc_timermgr_t manager, isc_timertype_t type,
timer->magic = TIMER_MAGIC; timer->magic = TIMER_MAGIC;
timer->manager = manager; timer->manager = manager;
timer->references = 1; timer->references = 1;
if (type == isc_timertype_once && !ZERO(interval)) if (type == isc_timertype_once && !ZERO(*interval))
os_time_add(&now, &interval, &timer->idle); isc_time_add(&now, interval, &timer->idle);
else { else {
timer->idle.seconds = 0; timer->idle.seconds = 0;
timer->idle.nanoseconds = 0; timer->idle.nanoseconds = 0;
} }
timer->type = type; timer->type = type;
timer->expires = expires; timer->expires = *expires;
timer->interval = interval; timer->interval = *interval;
timer->task = NULL; timer->task = NULL;
isc_task_attach(task, &timer->task); isc_task_attach(task, &timer->task);
timer->action = action; timer->action = action;
timer->arg = arg; timer->arg = arg;
timer->index = 0; 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); 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); return (ISC_R_UNEXPECTED);
} }
@ -285,9 +291,9 @@ isc_timer_create(isc_timermgr_t manager, isc_timertype_t type,
isc_result isc_result
isc_timer_reset(isc_timer_t timer, isc_timertype_t type, 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_timermgr_t manager;
isc_result result; isc_result result;
@ -300,15 +306,15 @@ isc_timer_reset(isc_timer_t timer, isc_timertype_t type,
REQUIRE(VALID_TIMER(timer)); REQUIRE(VALID_TIMER(timer));
manager = timer->manager; manager = timer->manager;
REQUIRE(VALID_MANAGER(manager)); REQUIRE(VALID_MANAGER(manager));
REQUIRE(!(ZERO(expires) && ZERO(interval))); REQUIRE(!(ZERO(*expires) && ZERO(*interval)));
/* /*
* Get current time. * Get current time.
*/ */
result = os_time_get(&now); result = isc_time_get(&now);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_time_get() failed: %s", "isc_time_get() failed: %s",
isc_result_totext(result)); isc_result_totext(result));
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
@ -321,10 +327,10 @@ isc_timer_reset(isc_timer_t timer, isc_timertype_t type,
if (purge) if (purge)
isc_task_purge(timer->task, timer, ISC_TASKEVENT_ANYEVENT); isc_task_purge(timer->task, timer, ISC_TASKEVENT_ANYEVENT);
timer->type = type; timer->type = type;
timer->expires = expires; timer->expires = *expires;
timer->interval = interval; timer->interval = *interval;
if (type == isc_timertype_once && !ZERO(interval)) if (type == isc_timertype_once && !ZERO(*interval))
os_time_add(&now, &interval, &timer->idle); isc_time_add(&now, interval, &timer->idle);
else { else {
timer->idle.seconds = 0; timer->idle.seconds = 0;
timer->idle.nanoseconds = 0; timer->idle.nanoseconds = 0;
@ -340,7 +346,7 @@ isc_timer_reset(isc_timer_t timer, isc_timertype_t type,
isc_result isc_result
isc_timer_touch(isc_timer_t timer) { isc_timer_touch(isc_timer_t timer) {
isc_result result; isc_result result;
os_time_t now; struct isc_time now;
/* /*
* Set the last-touched time of 'timer' to the current time. * 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); INSIST(timer->type == isc_timertype_once);
result = os_time_get(&now); result = isc_time_get(&now);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_time_get() failed: %s", "isc_time_get() failed: %s",
isc_result_totext(result)); isc_result_totext(result));
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
os_time_add(&now, &timer->interval, &timer->idle); isc_time_add(&now, &timer->interval, &timer->idle);
UNLOCK(&timer->lock); UNLOCK(&timer->lock);
@ -409,7 +415,7 @@ isc_timer_detach(isc_timer_t *timerp) {
} }
static void 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_boolean_t done = ISC_FALSE, post_event, need_schedule;
isc_event_t event; isc_event_t event;
isc_eventtype_t type = 0; isc_eventtype_t type = 0;
@ -418,19 +424,19 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) {
while (manager->nscheduled > 0 && !done) { while (manager->nscheduled > 0 && !done) {
timer = isc_heap_element(manager->heap, 1); 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) { if (timer->type == isc_timertype_ticker) {
type = ISC_TIMEREVENT_TICK; type = ISC_TIMEREVENT_TICK;
post_event = ISC_TRUE; post_event = ISC_TRUE;
need_schedule = ISC_TRUE; need_schedule = ISC_TRUE;
} else if (!ZERO(timer->expires) && } else if (!ZERO(timer->expires) &&
os_time_compare(nowp, isc_time_compare(now,
&timer->expires) >= 0) { &timer->expires) >= 0) {
type = ISC_TIMEREVENT_LIFE; type = ISC_TIMEREVENT_LIFE;
post_event = ISC_TRUE; post_event = ISC_TRUE;
need_schedule = ISC_FALSE; need_schedule = ISC_FALSE;
} else if (!ZERO(timer->idle) && } else if (!ZERO(timer->idle) &&
os_time_compare(nowp, isc_time_compare(now,
&timer->idle) >= 0) { &timer->idle) >= 0) {
type = ISC_TIMEREVENT_IDLE; type = ISC_TIMEREVENT_IDLE;
post_event = ISC_TRUE; post_event = ISC_TRUE;
@ -454,8 +460,7 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) {
sizeof *event); sizeof *event);
if (event != NULL) if (event != NULL)
INSIST(isc_task_send(timer->task, isc_task_send(timer->task, &event);
&event));
else else
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"couldn't allocate event"); "couldn't allocate event");
@ -466,7 +471,7 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) {
manager->nscheduled--; manager->nscheduled--;
if (need_schedule) { if (need_schedule) {
result = schedule(timer, nowp, ISC_FALSE); result = schedule(timer, now, ISC_FALSE);
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"couldn't schedule timer: %s", "couldn't schedule timer: %s",
@ -482,12 +487,12 @@ dispatch(isc_timermgr_t manager, os_time_t *nowp) {
static void * static void *
run(void *uap) { run(void *uap) {
isc_timermgr_t manager = uap; isc_timermgr_t manager = uap;
isc_boolean_t timeout; struct isc_time now;
os_time_t now; isc_result_t result;
LOCK(&manager->lock); LOCK(&manager->lock);
while (!manager->done) { while (!manager->done) {
INSIST(os_time_get(&now) == ISC_R_SUCCESS); INSIST(isc_time_get(&now) == ISC_R_SUCCESS);
XTRACETIME("running", now); XTRACETIME("running", now);
@ -495,8 +500,10 @@ run(void *uap) {
if (manager->nscheduled > 0) { if (manager->nscheduled > 0) {
XTRACETIME("waituntil", manager->due); XTRACETIME("waituntil", manager->due);
WAITUNTIL(&manager->wakeup, &manager->lock, result = WAITUNTIL(&manager->wakeup, &manager->lock,
&manager->due, &timeout); &manager->due);
INSIST(result == ISC_R_SUCCESS ||
result == ISC_R_TIMEDOUT);
} else { } else {
XTRACE("wait"); XTRACE("wait");
WAIT(&manager->wakeup, &manager->lock); WAIT(&manager->wakeup, &manager->lock);
@ -517,7 +524,7 @@ sooner(void *v1, void *v2) {
REQUIRE(VALID_TIMER(t1)); REQUIRE(VALID_TIMER(t1));
REQUIRE(VALID_TIMER(t2)); 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_TRUE);
return (ISC_FALSE); 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); isc_mem_put(mctx, manager, sizeof *manager);
return (ISC_R_NOMEMORY); return (ISC_R_NOMEMORY);
} }
if (!os_mutex_init(&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_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
if (!os_condition_init(&manager->wakeup)) {
(void)os_mutex_destroy(&manager->lock);
isc_heap_destroy(&manager->heap); isc_heap_destroy(&manager->heap);
isc_mem_put(mctx, manager, sizeof *manager); isc_mem_put(mctx, manager, sizeof *manager);
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_condition_init() failed"); "isc_mutex_init() failed");
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
if (!os_thread_create(run, manager, &manager->thread)) { if (isc_condition_init(&manager->wakeup) != ISC_R_SUCCESS) {
(void)os_condition_destroy(&manager->wakeup); (void)isc_mutex_destroy(&manager->lock);
(void)os_mutex_destroy(&manager->lock);
isc_heap_destroy(&manager->heap); isc_heap_destroy(&manager->heap);
isc_mem_put(mctx, manager, sizeof *manager); isc_mem_put(mctx, manager, sizeof *manager);
UNEXPECTED_ERROR(__FILE__, __LINE__, 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); return (ISC_R_UNEXPECTED);
} }
@ -615,15 +624,15 @@ isc_timermgr_destroy(isc_timermgr_t *managerp) {
/* /*
* Wait for thread to exit. * Wait for thread to exit.
*/ */
if (!os_thread_join(manager->thread)) if (isc_thread_join(manager->thread) != ISC_R_SUCCESS)
UNEXPECTED_ERROR(__FILE__, __LINE__, UNEXPECTED_ERROR(__FILE__, __LINE__,
"os_thread_join() failed"); "isc_thread_join() failed");
/* /*
* Clean up. * Clean up.
*/ */
(void)os_condition_destroy(&manager->wakeup); (void)isc_condition_destroy(&manager->wakeup);
(void)os_mutex_destroy(&manager->lock); (void)isc_mutex_destroy(&manager->lock);
isc_heap_destroy(&manager->heap); isc_heap_destroy(&manager->heap);
manager->magic = 0; manager->magic = 0;
isc_mem_put(manager->mctx, manager, sizeof *manager); isc_mem_put(manager->mctx, manager, sizeof *manager);

View File

@ -10,19 +10,19 @@
* to represent intervals. * to represent intervals.
*/ */
typedef struct os_time_t { typedef struct isc_time {
time_t seconds; time_t seconds;
long nanoseconds; long nanoseconds;
} os_time_t; } *isc_time_t;
isc_result isc_result_t
os_time_get(os_time_t *timep); 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: * Requires:
* *
* 'timep' is a valid pointer. * 't' is a valid pointer.
* *
* Returns: * Returns:
* *
@ -31,41 +31,41 @@ os_time_get(os_time_t *timep);
*/ */
int 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: * Requires:
* *
* 't1p' and 't2p' are a valid. * 't1' and 't2' are a valid.
* *
* Returns: * Returns:
* *
* -1 *tp1 < *t2p * -1 t1 < t2 (comparing times, not pointers)
* 0 *tp1 = *t2p * 0 t1 = t2
* 1 *tp1 > *t2p * 1 t1 > t2
*/ */
void 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: * Requires:
* *
* 't1p', 't2p', and 't3p' are valid. * 't1', 't2', and 't3' are valid.
*/ */
void 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: * 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 */ #endif /* ISC_TIME_H */

View File

@ -5,13 +5,12 @@
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include <isc/assertions.h>
#include <isc/assertions.h> #include <isc/assertions.h>
#include <isc/unexpect.h> #include <isc/unexpect.h>
#include <isc/time.h> #include <isc/time.h>
isc_result isc_result
os_time_get(os_time_t *timep) { isc_time_get(isc_time_t timep) {
struct timeval tv; struct timeval tv;
/* /*
@ -33,56 +32,56 @@ os_time_get(os_time_t *timep) {
} }
int 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); return (-1);
if (t1p->seconds > t2p->seconds) if (t1->seconds > t2->seconds)
return (1); return (1);
if (t1p->nanoseconds < t2p->nanoseconds) if (t1->nanoseconds < t2->nanoseconds)
return (-1); return (-1);
if (t1p->nanoseconds > t2p->nanoseconds) if (t1->nanoseconds > t2->nanoseconds)
return (1); return (1);
return (0); return (0);
} }
void 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; t3->seconds = t1->seconds + t2->seconds;
t3p->nanoseconds = t1p->nanoseconds + t2p->nanoseconds; t3->nanoseconds = t1->nanoseconds + t2->nanoseconds;
if (t3p->nanoseconds > 1000000000) { if (t3->nanoseconds > 1000000000) {
t3p->seconds++; t3->seconds++;
t3p->nanoseconds -= 1000000000; t3->nanoseconds -= 1000000000;
} }
} }
void 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(t1 != NULL && t2 != NULL && t3 != NULL);
REQUIRE(os_time_compare(t1p, t2p) >= 0); REQUIRE(isc_time_compare(t1, t2) >= 0);
t3p->seconds = t1p->seconds - t2p->seconds; t3->seconds = t1->seconds - t2->seconds;
if (t1p->nanoseconds >= t2p->nanoseconds) if (t1->nanoseconds >= t2->nanoseconds)
t3p->nanoseconds = t1p->nanoseconds - t2p->nanoseconds; t3->nanoseconds = t1->nanoseconds - t2->nanoseconds;
else { else {
t3p->nanoseconds = 1000000000 - t2p->nanoseconds + t3->nanoseconds = 1000000000 - t2->nanoseconds +
t1p->nanoseconds; t1->nanoseconds;
t3p->seconds--; t3->seconds--;
} }
} }