1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2001-2005
5 * The task manager subsystem. Tasks execute STG code, with this
6 * module providing the API which the Scheduler uses to control their
7 * creation and destruction.
9 * -------------------------------------------------------------------------*/
11 #include "PosixSource.h"
16 #include "Capability.h"
26 // Task lists and global counters.
27 // Locks required: sched_mutex.
28 Task *all_tasks = NULL;
30 static int tasksInitialized = 0;
32 static void freeTask (Task *task);
33 static Task * allocTask (void);
34 static Task * newTask (rtsBool);
36 /* -----------------------------------------------------------------------------
37 * Remembering the current thread's Task
38 * -------------------------------------------------------------------------- */
40 // A thread-local-storage key that we can use to get access to the
41 // current thread's Task structure.
42 #if defined(THREADED_RTS)
43 # if defined(MYTASK_USE_TLV)
44 __thread Task *my_task;
46 ThreadLocalKey currentTaskKey;
52 /* -----------------------------------------------------------------------------
53 * Rest of the Task API
54 * -------------------------------------------------------------------------- */
57 initTaskManager (void)
59 if (!tasksInitialized) {
62 #if defined(THREADED_RTS) && !defined(MYTASK_USE_TLV)
63 newThreadLocalKey(¤tTaskKey);
69 freeTaskManager (void)
74 ASSERT_LOCK_HELD(&sched_mutex);
76 for (task = all_tasks; task != NULL; task = next) {
77 next = task->all_link;
85 debugTrace(DEBUG_sched, "freeing task manager, %d tasks still running",
89 #if defined(THREADED_RTS) && !defined(MYTASK_USE_TLV)
90 freeThreadLocalKey(¤tTaskKey);
107 task = newTask(rtsFalse);
108 #if defined(THREADED_RTS)
109 task->id = osThreadId();
117 freeTask (Task *task)
119 InCall *incall, *next;
121 // We only free resources if the Task is not in use. A
122 // Task may still be in use if we have a Haskell thread in
123 // a foreign call while we are attempting to shut down the
124 // RTS (see conc059).
125 #if defined(THREADED_RTS)
126 closeCondition(&task->cond);
127 closeMutex(&task->lock);
130 for (incall = task->incall; incall != NULL; incall = next) {
131 next = incall->prev_stack;
134 for (incall = task->spare_incalls; incall != NULL; incall = next) {
143 newTask (rtsBool worker)
145 #if defined(THREADED_RTS)
146 Ticks currentElapsedTime, currentUserTime;
150 #define ROUND_TO_CACHE_LINE(x) ((((x)+63) / 64) * 64)
151 task = stgMallocBytes(ROUND_TO_CACHE_LINE(sizeof(Task)), "newTask");
154 task->worker = worker;
155 task->stopped = rtsFalse;
156 task->stat = NoStatus;
158 task->n_spare_incalls = 0;
159 task->spare_incalls = NULL;
162 #if defined(THREADED_RTS)
163 initCondition(&task->cond);
164 initMutex(&task->lock);
165 task->wakeup = rtsFalse;
168 #if defined(THREADED_RTS)
169 currentUserTime = getThreadCPUTime();
170 currentElapsedTime = getProcessElapsedTime();
175 task->muttimestart = currentUserTime;
176 task->elapsedtimestart = currentElapsedTime;
181 ACQUIRE_LOCK(&sched_mutex);
183 task->all_link = all_tasks;
188 RELEASE_LOCK(&sched_mutex);
193 // avoid the spare_incalls list growing unboundedly
194 #define MAX_SPARE_INCALLS 8
197 newInCall (Task *task)
201 if (task->spare_incalls != NULL) {
202 incall = task->spare_incalls;
203 task->spare_incalls = incall->next;
204 task->n_spare_incalls--;
206 incall = stgMallocBytes((sizeof(InCall)), "newBoundTask");
211 incall->suspended_tso = NULL;
212 incall->suspended_cap = NULL;
215 incall->prev_stack = task->incall;
216 task->incall = incall;
220 endInCall (Task *task)
224 incall = task->incall;
226 task->incall = task->incall->prev_stack;
228 if (task->n_spare_incalls >= MAX_SPARE_INCALLS) {
231 incall->next = task->spare_incalls;
232 task->spare_incalls = incall;
233 task->n_spare_incalls++;
243 if (!tasksInitialized) {
244 errorBelch("newBoundTask: RTS is not initialised; call hs_init() first");
245 stg_exit(EXIT_FAILURE);
250 task->stopped = rtsFalse;
254 debugTrace(DEBUG_sched, "new task (taskCount: %d)", taskCount);
259 boundTaskExiting (Task *task)
261 task->stopped = rtsTrue;
263 #if defined(THREADED_RTS)
264 ASSERT(osThreadId() == task->id);
266 ASSERT(myTask() == task);
270 debugTrace(DEBUG_sched, "task exiting");
275 #define TASK_ID(t) (t)->id
277 #define TASK_ID(t) (t)
281 discardTasksExcept (Task *keep)
285 // Wipe the task list, except the current Task.
286 ACQUIRE_LOCK(&sched_mutex);
287 for (task = all_tasks; task != NULL; task=next) {
288 next = task->all_link;
290 debugTrace(DEBUG_sched, "discarding task %ld", (long)TASK_ID(task));
295 RELEASE_LOCK(&sched_mutex);
299 taskTimeStamp (Task *task USED_IF_THREADS)
301 #if defined(THREADED_RTS)
302 Ticks currentElapsedTime, currentUserTime, elapsedGCTime;
304 currentUserTime = getThreadCPUTime();
305 currentElapsedTime = getProcessElapsedTime();
307 // XXX this is wrong; we want elapsed GC time since the
309 elapsedGCTime = stat_getElapsedGCTime();
312 currentUserTime - task->muttimestart - task->gc_time;
314 currentElapsedTime - task->elapsedtimestart - elapsedGCTime;
316 if (task->mut_time < 0) { task->mut_time = 0; }
317 if (task->mut_etime < 0) { task->mut_etime = 0; }
321 #if defined(THREADED_RTS)
324 workerTaskStop (Task *task)
328 ASSERT(task->id == id);
329 ASSERT(myTask() == task);
333 task->stopped = rtsTrue;
338 #if defined(THREADED_RTS)
340 static void OSThreadProcAttr
341 workerStart(Task *task)
345 // See startWorkerTask().
346 ACQUIRE_LOCK(&task->lock);
348 RELEASE_LOCK(&task->lock);
350 if (RtsFlags.ParFlags.setAffinity) {
351 setThreadAffinity(cap->no, n_capabilities);
354 // set the thread-local pointer to the Task:
359 scheduleWorker(cap,task);
363 startWorkerTask (Capability *cap)
369 // A worker always gets a fresh Task structure.
370 task = newTask(rtsTrue);
372 // The lock here is to synchronise with taskStart(), to make sure
373 // that we have finished setting up the Task structure before the
374 // worker thread reads it.
375 ACQUIRE_LOCK(&task->lock);
379 // Give the capability directly to the worker; we can't let anyone
380 // else get in, because the new worker Task has nowhere to go to
381 // sleep so that it could be woken up again.
382 ASSERT_LOCK_HELD(&cap->lock);
383 cap->running_task = task;
385 r = createOSThread(&tid, (OSThreadProc*)workerStart, task);
387 sysErrorBelch("failed to create OS thread");
388 stg_exit(EXIT_FAILURE);
391 debugTrace(DEBUG_sched, "new worker task (taskCount: %d)", taskCount);
395 // ok, finished with the Task struct.
396 RELEASE_LOCK(&task->lock);
399 #endif /* THREADED_RTS */
403 static void *taskId(Task *task)
406 return (void *)task->id;
412 void printAllTasks(void);
418 for (task = all_tasks; task != NULL; task = task->all_link) {
419 debugBelch("task %p is %s, ", taskId(task), task->stopped ? "stopped" : "alive");
420 if (!task->stopped) {
422 debugBelch("on capability %d, ", task->cap->no);
424 if (task->incall->tso) {
425 debugBelch("bound to thread %lu",
426 (unsigned long)task->incall->tso->id);
428 debugBelch("worker");