Commit bf6d2e39 authored by Bob Halley's avatar Bob Halley

more renaming

parent de9282a1
......@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <isc/assertions.h>
#include <isc/memcluster.h>
#include <isc/task.h>
#include <isc/thread.h>
......@@ -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);
......
......@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <isc/assertions.h>
#include <isc/memcluster.h>
#include <isc/task.h>
#include <isc/thread.h>
......@@ -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);
......
......@@ -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
......
......@@ -8,6 +8,7 @@
#include <isc/memcluster.h>
#include <isc/boolean.h>
#include <isc/result.h>
/***
......@@ -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 *);
......
......@@ -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
......
......@@ -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
......
#include <isc/condition.h>
#include <errno.h>
#include <string.h>
#include <isc/condition.h>
#include <isc/unexpect.h>
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);
}
#ifndef CONDITION_H
#define CONDITION_H 1
#ifndef ISC_CONDITION_H
#define ISC_CONDITION_H 1
#include <pthread.h>
#include <isc/boolean.h>
#include <isc/result.h>
#include <isc/mutex.h>
#include <isc/time.h>
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 */
#ifndef MUTEX_H
#define MUTEX_H 1
#ifndef ISC_MUTEX_H
#define ISC_MUTEX_H 1
#include <pthread.h>
typedef pthread_mutex_t os_mutex_t;
#define OS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#include <isc/result.h>
#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 */
#ifndef THREAD_H
#define THREAD_H 1
#ifndef ISC_THREAD_H
#define ISC_THREAD_H 1
#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)) \
== 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 */
......@@ -6,7 +6,7 @@
#include <isc/thread.h>
#include <isc/mutex.h>
#include <isc/condition.h>
#include <isc/unexpect.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.
*
* 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;