task.c revision 677045ed612e1c26a32b5700479e26c25bcede58
754cb8a2b33fa6cfaa15d6470f66e5fb0eab4764Automatic Updater * Copyright (C) 1998-2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉/* $Id: task.c,v 1.68 2000/07/27 22:24:54 bwelling Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Principal Author: Bob Halley
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * XXXRTH Need to document the states a task can be in, and the rules
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * for changing states.
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define XTRACE(m) printf("task %p thread %lu: %s\n", \
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define XTTRACE(t, m) printf("task %p thread %lu: %s\n", \
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define XTHREADTRACE(m) printf("thread %lu: %s\n", \
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewstypedef enum {
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews task_state_idle, task_state_ready, task_state_running,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /* Not locked. */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /* Locked by task lock. */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int flags;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /* Locked by task manager lock. */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#define TASK_SHUTTINGDOWN(t) (((t)->flags & TASK_F_SHUTTINGDOWN) \
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews#define TASK_MANAGER_MAGIC 0x54534B4DU /* TSKM. */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /* Not locked. */
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson /* Locked by task manager lock. */
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence#define FINISHED(m) ((m)->exiting && EMPTY((m)->tasks))
26a5f97dd8770ced729025488091b77d8beb0ab6Brian Wellington * All tasks have completed and the
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * task manager is exiting. Wake up
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * any idle worker threads so they
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson isc_mem_put(manager->mctx, task, sizeof *task);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafssonisc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington task = isc_mem_get(manager->mctx, sizeof *task);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson if (isc_mutex_init(&task->lock) != ISC_R_SUCCESS) {
c885fad9b8bf204ae9e62c9acb0321e2bcca30a4Andreas Gustafsson isc_mem_put(manager->mctx, task, sizeof *task);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington "isc_mutex_init() failed");
c885fad9b8bf204ae9e62c9acb0321e2bcca30a4Andreas Gustafsson memset(task->name, 0, sizeof task->name);
if (exiting) {
return (ISC_R_SHUTTINGDOWN);
return (ISC_R_SUCCESS);
static inline isc_boolean_t
return (was_idle);
static inline isc_boolean_t
return (ISC_TRUE);
return (ISC_FALSE);
if (was_idle)
static inline isc_boolean_t
return (was_idle);
if (was_idle) {
unsigned int count = 0;
count++;
return (count);
unsigned int count;
ISC_TRUE);
return (count);
void *tag)
return (ISC_FALSE);
return (ISC_TRUE);
ISC_FALSE));
ISC_FALSE));
NULL,
arg,
sizeof *event);
return (ISC_R_NOMEMORY);
if (disallowed)
return (result);
if (was_idle)
#ifdef ISC_TASK_NAMES
(void)name;
(void)tag;
static isc_threadresult_t
#ifdef _WIN32
unsigned int dispatch_count = 0;
} while (!done);
if (finished)
if (requeue) {
return ((isc_threadresult_t)0);
unsigned int i, started = 0;
return (ISC_R_NOMEMORY);
return (ISC_R_NOMEMORY);
return (ISC_R_UNEXPECTED);
if (default_quantum == 0)
return (ISC_R_UNEXPECTED);
for (i = 0; i < workers; i++) {
started++;
if (started == 0) {
return (ISC_R_NOTHREADS);
return (ISC_R_SUCCESS);