task.c revision 918bae0f4bf1dc9e66a6667b25978451b50c35b1
#include "attribute.h"
#include <isc/assertions.h>
#include <isc/condition.h>
/***
*** General Macros.
***/
/*
* We use macros instead of calling the os_ 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.
*/
#ifdef TASK_TRACE
task, os_thread_self())
#else
#define XTRACE(m)
#endif
/***
*** Types.
***/
typedef enum {
} task_state_t;
#define VALID_TASK(t) ((t) != NULL && \
(t)->magic == TASK_MAGIC)
struct task {
/* Not locked. */
unsigned int magic;
/* Locked by task lock. */
unsigned int references;
unsigned int quantum;
/* Locked by task manager lock. */
};
#define VALID_MANAGER(m) ((m) != NULL && \
(m)->magic == TASK_MANAGER_MAGIC)
struct task_manager {
/* Not locked. */
unsigned int magic;
/* Locked by task manager lock. */
unsigned int default_quantum;
unsigned int workers;
};
#define DEFAULT_DEFAULT_QUANTUM 5
/***
*** Events.
***/
static inline task_event_t
{
return (NULL);
return (event);
}
{
if (size < sizeof (struct task_event))
return (NULL);
if (type < 0)
return (NULL);
return (NULL);
}
void
}
/***
*** Tasks.
***/
static void
XTRACE("free task");
/*
* All tasks have completed and the
* task manager is exiting. Wake up
* any idle worker threads so they
* can exit.
*/
}
}
{
return (FALSE);
return (FALSE);
}
NULL,
sizeof *task->shutdown_event);
return (FALSE);
}
return (TRUE);
}
void
task->references++;
}
void
XTRACE("task_detach");
task->references--;
if (free_task)
}
XTRACE("sending");
/*
* We're trying hard to hold locks for as short a time as possible.
* We're also trying to hold as few locks as possible. This is why
* some processing is deferred until after a lock is released.
*/
if (task->enqueue_allowed) {
}
} else
if (discard) {
return (TRUE);
}
if (was_idle) {
/*
* We need to add this task to the ready queue.
*
* We've waited until now to do it, rather than doing it
* while holding the task lock, because we don't want to
* block while holding the task lock.
*
* We've changed the state to ready, so no one else will
* be trying to add this task to the ready queue. It
* thus doesn't matter if more events have been added to
* the queue after we gave up the task lock.
*
* Shutting down a task requires posting a shutdown event
* to the task's queue and then executing it, so there's
* no way the task can disappear. A task is always on the
* task manager's 'tasks' list, so the task manager can
* always post a shutdown event to all tasks if it is
* requested to shutdown.
*/
need_wakeup = TRUE;
/*
* If the runnable queue is empty, the worker threads could
* either be executing tasks or waiting for something to do.
* We wakeup anyone who is sleeping.
*/
if (need_wakeup)
}
XTRACE("sent");
return (TRUE);
}
void
/*
* Purge events matching 'sender' and 'type'. sender == NULL means
* "any sender". type == NULL means any type. Task manager events
* cannot be purged.
*/
event = next_event) {
}
}
event = next_event) {
}
}
void
/*
* This routine is very similar to task_send_event() above.
*/
if (task->enqueue_allowed) {
}
} else
if (discard)
return;
if (was_idle) {
need_wakeup = TRUE;
if (need_wakeup)
}
}
void
}
/***
*** Task Manager.
***/
static
void *task_manager_run(void *uap) {
XTRACE("start");
/*
* Again we're trying to hold the lock for as short a time as possible
* and to do as little locking and unlocking as possible.
*
* In both while loops, the appropriate lock must be held before the
* while body starts. Code which acquired the lock at the top of
* the loop would be more readable, but would result in a lot of
* extra locking. Compare:
*
* Straightforward:
*
* LOCK();
* ...
* UNLOCK();
* while (expression) {
* LOCK();
* ...
* UNLOCK();
*
* Unlocked part here...
*
* LOCK();
* ...
* UNLOCK();
* }
*
* Note how if the loop continues we unlock and then immediately lock.
* For N iterations of the loop, this code does 2N+1 locks and 2N+1
* unlocks. Also note that the lock is not held when the while
* condition is tested, which may or may not be important, depending
* on the expression.
*
* As written:
*
* LOCK();
* while (expression) {
* ...
* UNLOCK();
*
* Unlocked part here...
*
* LOCK();
* ...
* }
* UNLOCK();
*
* For N iterations of the loop, this code does N+1 locks and N+1
* unlocks. The while expression is always protected by the lock.
*/
/*
* For reasons similar to those given in the comment in
* task_send_event() above, it is safe for us to dequeue
* the task while only holding the manager lock, and then
* change the task to running state while only holding the
* task lock.
*/
XTRACE("wait");
XTRACE("awake");
}
XTRACE("working");
unsigned int dispatch_count = 0;
/*
* Note we only unlock the manager lock if we actually
* have a task to do. We must reacquire the manager
* lock before exiting the 'if (task != NULL)' block.
*/
/*
* The task became runnable, but all events
* in the run queue were subsequently purged.
* Put the task to sleep.
*/
XTRACE("ready but empty");
} else
while (!done) {
is_shutdown = TRUE;
else
is_shutdown = FALSE;
/*
* Execute the event action.
*/
XTRACE("execute action");
else
if (wants_shutdown || is_shutdown) {
/*
* The event action has either
* requested shutdown, or the event
* we just executed was the shutdown
* event.
*
* Since no more events can be
* delivered to the task, we purge
* any remaining events (but defer
* freeing them until we've released
* the lock).
*/
XTRACE("wants shutdown");
}
if (task->references == 0)
/*
* Nothing else to do for this task.
* Put it to sleep.
*/
XTRACE("empty");
/*
* Our quantum has expired, but
* there is more work to be done.
* We'll requeue it to the ready
* queue later.
*
* We don't check quantum until
* dispatching at least one event,
* so the minimum quantum is one.
*/
XTRACE("quantum");
}
}
if (discard_remaining) {
event = next_event) {
}
}
if (free_task)
if (requeue) {
/*
* We know we're awake, so we don't have
* to wakeup any sleeping threads if the
* ready queue is empty before we requeue.
*
* A possible optimization if the queue is
* empty is to 'goto' the 'if (task != NULL)'
* block, avoiding the ENQUEUE of the task
* and the subsequent immediate DEQUEUE
* (since it is the only executable task).
* We don't do this because then we'd be
* skipping the exit_requested check. The
* cost of ENQUEUE is low anyway, especially
* when you consider that we'd have to do
* an extra EMPTY check to see if we could
* do the optimization. If the ready queue
* were usually nonempty, the 'optimization'
* might even hurt rather than help.
*/
}
}
}
no_workers = TRUE;
if (no_workers)
XTRACE("exit");
return (NULL);
}
static void
}
unsigned int
{
unsigned int i, started = 0;
if (workers == 0)
return (0);
return (0);
return (0);
}
if (default_quantum == 0)
return (0);
}
return (0);
}
/*
* Start workers.
*/
for (i = 0; i < workers; i++) {
started++;
(void)os_thread_detach(thread);
}
}
if (started == 0) {
return (0);
}
return (started);
}
void
XTRACE("task_manager_destroy");
/*
* Only one non-worker thread may ever call this routine.
* If a worker thread wants to initiate shutdown of the
* task manager, it should ask some non-worker thread to call
* task_manager_destroy(), e.g. by signalling a condition variable
* that the startup thread is sleeping on.
*/
/*
* Unlike elsewhere, we're going to hold this lock a long time.
* We need to do so, because otherwise the list of tasks could
* change while we were traversing it.
*
* This is also the only function where we will hold both the
* task manager lock and a task lock at the same time.
*/
/*
* Make sure we only get called once.
*/
/*
* Post the shutdown event to every task (if it hasn't already been
* posted).
*/
if (task->enqueue_allowed) {
}
}
}
/*
* Wake up any sleeping workers. This ensures we get work done if
* there's work left to do, and if there are already no tasks left
* it will cause the workers to see manager->exiting.
*/
/*
* Wait for all the worker threads to exit.
*
* XXX This will become a timed wait. If all the workers haven't
* died after we've waited the specified interval, we will
* kill the worker threads. Should we join with the worker
* threads after killing them or just leave them detached and
* hope they go away?
*/
}