2 * Copyright (C) 2004-2007, 2009-2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
28 * \brief The task system provides a lightweight execution context, which is
29 * basically an event queue.
31 * When a task's event queue is non-empty, the
32 * task is runnable. A small work crew of threads, typically one per CPU,
33 * execute runnable tasks by dispatching the events on the tasks' event
34 * queues. Context switching between tasks is fast.
37 * The module ensures appropriate synchronization of data structures it
38 * creates and manipulates.
39 * The caller must ensure that isc_taskmgr_destroy() is called only
40 * once for a given manager.
43 * No anticipated impact.
49 * No anticipated impact.
54 * \section purge Purging and Unsending
56 * Events which have been queued for a task but not delivered may be removed
57 * from the task's event queue by purging or unsending.
59 * With both types, the caller specifies a matching pattern that selects
60 * events based upon their sender, type, and tag.
62 * Purging calls isc_event_free() on the matching events.
64 * Unsending returns a list of events that matched the pattern.
65 * The caller is then responsible for them.
67 * Consumers of events should purge, not unsend.
69 * Producers of events often want to remove events when the caller indicates
70 * it is no longer interested in the object, e.g. by canceling a timer.
71 * Sometimes this can be done by purging, but for some event types, the
72 * calls to isc_event_free() cause deadlock because the event free routine
73 * wants to acquire a lock the caller is already holding. Unsending instead
74 * of purging solves this problem. As a general rule, producers should only
75 * unsend events which they have sent.
83 #include <isc/eventclass.h>
85 #include <isc/stdtime.h>
86 #include <isc/types.h>
89 #define ISC_TASKEVENT_FIRSTEVENT (ISC_EVENTCLASS_TASK + 0)
90 #define ISC_TASKEVENT_SHUTDOWN (ISC_EVENTCLASS_TASK + 1)
91 #define ISC_TASKEVENT_LASTEVENT (ISC_EVENTCLASS_TASK + 65535)
103 /*% Task and task manager methods */
104 typedef struct isc_taskmgrmethods {
105 void (*destroy)(isc_taskmgr_t **managerp);
106 isc_result_t (*taskcreate)(isc_taskmgr_t *manager,
107 unsigned int quantum,
109 void (*setexcltask)(isc_taskmgr_t *mgr, isc_task_t *task);
110 isc_result_t (*excltask)(isc_taskmgr_t *mgr, isc_task_t **taskp);
111 } isc_taskmgrmethods_t;
113 typedef struct isc_taskmethods {
114 void (*attach)(isc_task_t *source, isc_task_t **targetp);
115 void (*detach)(isc_task_t **taskp);
116 void (*destroy)(isc_task_t **taskp);
117 void (*send)(isc_task_t *task, isc_event_t **eventp);
118 void (*sendanddetach)(isc_task_t **taskp, isc_event_t **eventp);
119 unsigned int (*unsend)(isc_task_t *task, void *sender, isc_eventtype_t type,
120 void *tag, isc_eventlist_t *events);
121 isc_result_t (*onshutdown)(isc_task_t *task, isc_taskaction_t action,
123 void (*shutdown)(isc_task_t *task);
124 void (*setname)(isc_task_t *task, const char *name, void *tag);
125 unsigned int (*purgeevents)(isc_task_t *task, void *sender,
126 isc_eventtype_t type, void *tag);
127 unsigned int (*purgerange)(isc_task_t *task, void *sender,
128 isc_eventtype_t first, isc_eventtype_t last,
130 isc_result_t (*beginexclusive)(isc_task_t *task);
131 void (*endexclusive)(isc_task_t *task);
135 * This structure is actually just the common prefix of a task manager
136 * object implementation's version of an isc_taskmgr_t.
138 * Direct use of this structure by clients is forbidden. task implementations
139 * may change the structure. 'magic' must be ISCAPI_TASKMGR_MAGIC for any
140 * of the isc_task_ routines to work. task implementations must maintain
141 * all task invariants.
144 unsigned int impmagic;
146 isc_taskmgrmethods_t *methods;
149 #define ISCAPI_TASKMGR_MAGIC ISC_MAGIC('A','t','m','g')
150 #define ISCAPI_TASKMGR_VALID(m) ((m) != NULL && \
151 (m)->magic == ISCAPI_TASKMGR_MAGIC)
154 * This is the common prefix of a task object. The same note as
155 * that for the taskmgr structure applies.
158 unsigned int impmagic;
160 isc_taskmethods_t *methods;
163 #define ISCAPI_TASK_MAGIC ISC_MAGIC('A','t','s','t')
164 #define ISCAPI_TASK_VALID(s) ((s) != NULL && \
165 (s)->magic == ISCAPI_TASK_MAGIC)
168 isc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
175 *\li If 'quantum' is non-zero, then only that many events can be dispatched
176 * before the task must yield to other tasks waiting to execute. If
177 * quantum is zero, then the default quantum of the task manager will
180 *\li The 'quantum' option may be removed from isc_task_create() in the
181 * future. If this happens, isc_task_getquantum() and
182 * isc_task_setquantum() will be provided.
186 *\li 'manager' is a valid task manager.
188 *\li taskp != NULL && *taskp == NULL
192 *\li On success, '*taskp' is bound to the new task.
198 *\li #ISC_R_UNEXPECTED
199 *\li #ISC_R_SHUTTINGDOWN
203 isc_task_attach(isc_task_t *source, isc_task_t **targetp);
205 * Attach *targetp to source.
209 *\li 'source' is a valid task.
211 *\li 'targetp' points to a NULL isc_task_t *.
215 *\li *targetp is attached to source.
219 isc_task_detach(isc_task_t **taskp);
221 * Detach *taskp from its task.
225 *\li '*taskp' is a valid task.
231 *\li If '*taskp' is the last reference to the task, the task is idle (has
232 * an empty event queue), and has not been shutdown, the task will be
235 *\li If '*taskp' is the last reference to the task and
236 * the task has been shutdown,
237 * all resources used by the task will be freed.
241 isc_task_send(isc_task_t *task, isc_event_t **eventp);
243 * Send '*event' to 'task'.
247 *\li 'task' is a valid task.
248 *\li eventp != NULL && *eventp != NULL.
252 *\li *eventp == NULL.
256 isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp);
258 * Send '*event' to '*taskp' and then detach '*taskp' from its
263 *\li '*taskp' is a valid task.
264 *\li eventp != NULL && *eventp != NULL.
268 *\li *eventp == NULL.
272 *\li If '*taskp' is the last reference to the task, the task is
273 * idle (has an empty event queue), and has not been shutdown,
274 * the task will be shutdown.
276 *\li If '*taskp' is the last reference to the task and
277 * the task has been shutdown,
278 * all resources used by the task will be freed.
283 isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first,
284 isc_eventtype_t last, void *tag);
286 * Purge events from a task's event queue.
290 *\li 'task' is a valid task.
296 *\li Events in the event queue of 'task' whose sender is 'sender', whose
297 * type is >= first and <= last, and whose tag is 'tag' will be purged,
298 * unless they are marked as unpurgable.
300 *\li A sender of NULL will match any sender. A NULL tag matches any
305 *\li The number of events purged.
309 isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type,
312 * Purge events from a task's event queue.
316 *\li This function is equivalent to
319 * isc_task_purgerange(task, sender, type, type, tag);
324 *\li 'task' is a valid task.
328 *\li Events in the event queue of 'task' whose sender is 'sender', whose
329 * type is 'type', and whose tag is 'tag' will be purged, unless they
330 * are marked as unpurgable.
332 *\li A sender of NULL will match any sender. A NULL tag matches any
337 *\li The number of events purged.
341 isc_task_purgeevent(isc_task_t *task, isc_event_t *event);
343 * Purge 'event' from a task's event queue.
345 * XXXRTH: WARNING: This method may be removed before beta.
349 *\li If 'event' is on the task's event queue, it will be purged,
350 * unless it is marked as unpurgeable. 'event' does not have to be
351 * on the task's event queue; in fact, it can even be an invalid
352 * pointer. Purging only occurs if the event is actually on the task's
355 * \li Purging never changes the state of the task.
359 *\li 'task' is a valid task.
363 *\li 'event' is not in the event queue for 'task'.
367 *\li #ISC_TRUE The event was purged.
368 *\li #ISC_FALSE The event was not in the event queue,
369 * or was marked unpurgeable.
373 isc_task_unsendrange(isc_task_t *task, void *sender, isc_eventtype_t first,
374 isc_eventtype_t last, void *tag, isc_eventlist_t *events);
376 * Remove events from a task's event queue.
380 *\li 'task' is a valid task.
384 *\li *events is a valid list.
388 *\li Events in the event queue of 'task' whose sender is 'sender', whose
389 * type is >= first and <= last, and whose tag is 'tag' will be dequeued
390 * and appended to *events.
392 *\li A sender of NULL will match any sender. A NULL tag matches any
397 *\li The number of events unsent.
401 isc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
402 void *tag, isc_eventlist_t *events);
404 * Remove events from a task's event queue.
408 *\li This function is equivalent to
411 * isc_task_unsendrange(task, sender, type, type, tag, events);
416 *\li 'task' is a valid task.
418 *\li *events is a valid list.
422 *\li Events in the event queue of 'task' whose sender is 'sender', whose
423 * type is 'type', and whose tag is 'tag' will be dequeued and appended
428 *\li The number of events unsent.
432 isc_task_onshutdown(isc_task_t *task, isc_taskaction_t action,
435 * Send a shutdown event with action 'action' and argument 'arg' when
436 * 'task' is shutdown.
440 *\li Shutdown events are posted in LIFO order.
444 *\li 'task' is a valid task.
446 *\li 'action' is a valid task action.
450 *\li When the task is shutdown, shutdown events requested with
451 * isc_task_onshutdown() will be appended to the task's event queue.
458 *\li #ISC_R_TASKSHUTTINGDOWN Task is shutting down.
462 isc_task_shutdown(isc_task_t *task);
468 *\li Shutting down a task causes any shutdown events requested with
469 * isc_task_onshutdown() to be posted (in LIFO order). The task
470 * moves into a "shutting down" mode which prevents further calls
471 * to isc_task_onshutdown().
473 *\li Trying to shutdown a task that has already been shutdown has no
478 *\li 'task' is a valid task.
482 *\li Any shutdown events requested with isc_task_onshutdown() have been
483 * posted (in LIFO order).
487 isc_task_destroy(isc_task_t **taskp);
493 *\li This call is equivalent to:
496 * isc_task_shutdown(*taskp);
497 * isc_task_detach(taskp);
502 * '*taskp' is a valid task.
506 *\li Any shutdown events requested with isc_task_onshutdown() have been
507 * posted (in LIFO order).
511 *\li If '*taskp' is the last reference to the task,
512 * all resources used by the task will be freed.
516 isc_task_setname(isc_task_t *task, const char *name, void *tag);
522 *\li Only the first 15 characters of 'name' will be copied.
524 *\li Naming a task is currently only useful for debugging purposes.
528 *\li 'task' is a valid task.
532 isc_task_getname(isc_task_t *task);
534 * Get the name of 'task', as previously set using isc_task_setname().
537 *\li This function is for debugging purposes only.
540 *\li 'task' is a valid task.
543 *\li A non-NULL pointer to a null-terminated string.
544 * If the task has not been named, the string is
550 isc_task_gettag(isc_task_t *task);
552 * Get the tag value for 'task', as previously set using isc_task_settag().
555 *\li This function is for debugging purposes only.
558 *\li 'task' is a valid task.
562 isc_task_beginexclusive(isc_task_t *task);
564 * Request exclusive access for 'task', which must be the calling
565 * task. Waits for any other concurrently executing tasks to finish their
566 * current event, and prevents any new events from executing in any of the
567 * tasks sharing a task manager with 'task'.
569 * The exclusive access must be relinquished by calling
570 * isc_task_endexclusive() before returning from the current event handler.
573 *\li 'task' is the calling task.
576 *\li #ISC_R_SUCCESS The current task now has exclusive access.
577 *\li #ISC_R_LOCKBUSY Another task has already requested exclusive
582 isc_task_endexclusive(isc_task_t *task);
584 * Relinquish the exclusive access obtained by isc_task_beginexclusive(),
585 * allowing other tasks to execute.
588 *\li 'task' is the calling task, and has obtained
589 * exclusive access by calling isc_task_spl().
593 isc_task_getcurrenttime(isc_task_t *task, isc_stdtime_t *t);
595 * Provide the most recent timestamp on the task. The timestamp is considered
596 * as the "current time" in the second-order granularity.
599 *\li 'task' is a valid task.
600 *\li 't' is a valid non NULL pointer.
603 *\li '*t' has the "current time".
607 isc_task_exiting(isc_task_t *t);
609 * Returns ISC_TRUE if the task is in the process of shutting down,
610 * ISC_FALSE otherwise.
613 *\li 'task' is a valid task.
621 isc_taskmgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
622 unsigned int workers, unsigned int default_quantum,
623 isc_taskmgr_t **managerp);
625 isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
626 unsigned int default_quantum, isc_taskmgr_t **managerp);
628 * Create a new task manager. isc_taskmgr_createinctx() also associates
629 * the new manager with the specified application context.
633 *\li 'workers' in the number of worker threads to create. In general,
634 * the value should be close to the number of processors in the system.
635 * The 'workers' value is advisory only. An attempt will be made to
636 * create 'workers' threads, but if at least one thread creation
637 * succeeds, isc_taskmgr_create() may return ISC_R_SUCCESS.
639 *\li If 'default_quantum' is non-zero, then it will be used as the default
640 * quantum value when tasks are created. If zero, then an implementation
641 * defined default quantum will be used.
645 *\li 'mctx' is a valid memory context.
649 *\li managerp != NULL && *managerp == NULL
651 *\li 'actx' is a valid application context (for createinctx()).
655 *\li On success, '*managerp' will be attached to the newly created task
662 *\li #ISC_R_NOTHREADS No threads could be created.
663 *\li #ISC_R_UNEXPECTED An unexpected error occurred.
664 *\li #ISC_R_SHUTTINGDOWN The non-threaded, shared, task
665 * manager shutting down.
669 isc_taskmgr_destroy(isc_taskmgr_t **managerp);
671 * Destroy '*managerp'.
675 *\li Calling isc_taskmgr_destroy() will shutdown all tasks managed by
676 * *managerp that haven't already been shutdown. The call will block
677 * until all tasks have entered the done state.
679 *\li isc_taskmgr_destroy() must not be called by a task event action,
680 * because it would block forever waiting for the event action to
681 * complete. An event action that wants to cause task manager shutdown
682 * should request some non-event action thread of execution to do the
683 * shutdown, e.g. by signaling a condition variable or using
684 * isc_app_shutdown().
686 *\li Task manager references are not reference counted, so the caller
687 * must ensure that no attempt will be made to use the manager after
688 * isc_taskmgr_destroy() returns.
692 *\li '*managerp' is a valid task manager.
694 *\li isc_taskmgr_destroy() has not be called previously on '*managerp'.
698 *\li All resources used by the task manager, and any tasks it managed,
703 isc_taskmgr_setexcltask(isc_taskmgr_t *mgr, isc_task_t *task);
705 * Set a task which will be used for all task-exclusive operations.
708 *\li 'manager' is a valid task manager.
710 *\li 'task' is a valid task.
714 isc_taskmgr_excltask(isc_taskmgr_t *mgr, isc_task_t **taskp);
716 * Attach '*taskp' to the task set by isc_taskmgr_getexcltask().
717 * This task should be used whenever running in task-exclusive mode,
718 * so as to prevent deadlock between two exclusive tasks.
721 *\li 'manager' is a valid task manager.
723 *\li taskp != NULL && *taskp == NULL
730 isc_taskmgr_renderxml(isc_taskmgr_t *mgr, xmlTextWriterPtr writer);
735 * See isc_taskmgr_create() above.
738 (*isc_taskmgrcreatefunc_t)(isc_mem_t *mctx, unsigned int workers,
739 unsigned int default_quantum,
740 isc_taskmgr_t **managerp);
743 isc_task_register(isc_taskmgrcreatefunc_t createfunc);
745 * Register a new task management implementation and add it to the list of
746 * supported implementations. This function must be called when a different
747 * event library is used than the one contained in the ISC library.
751 isc__task_register(void);
753 * A short cut function that specifies the task management module in the ISC
754 * library for isc_task_register(). An application that uses the ISC library
755 * usually do not have to care about this function: it would call
756 * isc_lib_register(), which internally calls this function.
761 #endif /* ISC_TASK_H */