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 * -------------------------------------------------------------------------*/
13 #include "OSThreads.h"
15 #include "Capability.h"
26 // Task lists and global counters.
27 // Locks required: sched_mutex.
28 Task *all_tasks = NULL;
29 static Task *task_free_list = NULL; // singly-linked
31 #define DEFAULT_MAX_WORKERS 64
32 static nat maxWorkers; // we won't create more workers than this
33 static nat tasksRunning;
34 static nat workerCount;
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 ThreadLocalKey currentTaskKey;
48 /* -----------------------------------------------------------------------------
49 * Rest of the Task API
50 * -------------------------------------------------------------------------- */
53 initTaskManager (void)
55 static int initialized = 0;
61 #if defined(THREADED_RTS)
62 maxWorkers = DEFAULT_MAX_WORKERS * RtsFlags.ParFlags.nNodes;
64 maxWorkers = DEFAULT_MAX_WORKERS;
67 #if defined(THREADED_RTS)
68 newThreadLocalKey(¤tTaskKey);
75 stopTaskManager (void)
77 debugTrace(DEBUG_sched,
78 "stopping task manager, %d tasks still running",
86 #if defined(THREADED_RTS)
87 Ticks currentElapsedTime, currentUserTime;
91 task = stgMallocBytes(sizeof(Task), "newTask");
94 task->stopped = rtsFalse;
95 task->suspended_tso = NULL;
97 task->stat = NoStatus;
100 #if defined(THREADED_RTS)
101 initCondition(&task->cond);
102 initMutex(&task->lock);
103 task->wakeup = rtsFalse;
106 #if defined(THREADED_RTS)
107 currentUserTime = getThreadCPUTime();
108 currentElapsedTime = getProcessElapsedTime();
113 task->muttimestart = currentUserTime;
114 task->elapsedtimestart = currentElapsedTime;
119 task->return_link = NULL;
121 task->all_link = all_tasks;
135 ASSERT_LOCK_HELD(&sched_mutex);
136 if (task_free_list == NULL) {
139 task = task_free_list;
140 task_free_list = task->next;
143 task->stopped = rtsFalse;
145 #if defined(THREADED_RTS)
146 task->id = osThreadId();
148 ASSERT(task->cap == NULL);
154 debugTrace(DEBUG_sched, "new task (taskCount: %d)", taskCount);
159 boundTaskExiting (Task *task)
161 task->stopped = rtsTrue;
164 #if defined(THREADED_RTS)
165 ASSERT(osThreadId() == task->id);
167 ASSERT(myTask() == task);
168 setMyTask(task->prev_stack);
172 // sadly, we need a lock around the free task list. Todo: eliminate.
173 ACQUIRE_LOCK(&sched_mutex);
174 task->next = task_free_list;
175 task_free_list = task;
176 RELEASE_LOCK(&sched_mutex);
178 debugTrace(DEBUG_sched, "task exiting");
182 #define TASK_ID(t) (t)->id
184 #define TASK_ID(t) (t)
188 discardTask (Task *task)
190 ASSERT_LOCK_HELD(&sched_mutex);
191 if (!task->stopped) {
192 debugTrace(DEBUG_sched, "discarding task %ld", TASK_ID(task));
195 task->stopped = rtsTrue;
197 task->next = task_free_list;
198 task_free_list = task;
203 taskTimeStamp (Task *task USED_IF_THREADS)
205 #if defined(THREADED_RTS)
206 Ticks currentElapsedTime, currentUserTime, elapsedGCTime;
208 currentUserTime = getThreadCPUTime();
209 currentElapsedTime = getProcessElapsedTime();
211 // XXX this is wrong; we want elapsed GC time since the
213 elapsedGCTime = stat_getElapsedGCTime();
216 currentUserTime - task->muttimestart - task->gc_time;
218 currentElapsedTime - task->elapsedtimestart - elapsedGCTime;
220 if (task->mut_time < 0) { task->mut_time = 0; }
221 if (task->mut_etime < 0) { task->mut_etime = 0; }
226 workerTaskStop (Task *task)
228 #if defined(THREADED_RTS)
231 ASSERT(task->id == id);
232 ASSERT(myTask() == task);
236 task->stopped = rtsTrue;
241 resetTaskManagerAfterFork (void)
247 #if defined(THREADED_RTS)
250 startWorkerTask (Capability *cap,
251 void OSThreadProcAttr (*taskStart)(Task *task))
257 if (workerCount >= maxWorkers) {
258 barf("too many workers; runaway worker creation?");
262 // A worker always gets a fresh Task structure.
267 // The lock here is to synchronise with taskStart(), to make sure
268 // that we have finished setting up the Task structure before the
269 // worker thread reads it.
270 ACQUIRE_LOCK(&task->lock);
274 // Give the capability directly to the worker; we can't let anyone
275 // else get in, because the new worker Task has nowhere to go to
276 // sleep so that it could be woken up again.
277 ASSERT_LOCK_HELD(&cap->lock);
278 cap->running_task = task;
280 r = createOSThread(&tid, (OSThreadProc *)taskStart, task);
282 barf("startTask: Can't create new task");
285 debugTrace(DEBUG_sched, "new worker task (taskCount: %d)", taskCount);
289 // ok, finished with the Task struct.
290 RELEASE_LOCK(&task->lock);
293 #endif /* THREADED_RTS */
297 static void *taskId(Task *task)
300 return (void *)task->id;
306 void printAllTasks(void);
312 for (task = all_tasks; task != NULL; task = task->all_link) {
313 debugBelch("task %p is %s, ", taskId(task), task->stopped ? "stopped" : "alive");
314 if (!task->stopped) {
316 debugBelch("on capability %d, ", task->cap->no);
319 debugBelch("bound to thread %d", task->tso->id);
321 debugBelch("worker");