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"
25 // Task lists and global counters.
26 // Locks required: sched_mutex.
27 Task *all_tasks = NULL;
28 static Task *task_free_list = NULL; // singly-linked
30 #define DEFAULT_MAX_WORKERS 64
31 static nat maxWorkers; // we won't create more workers than this
32 static nat tasksRunning;
33 static nat workerCount;
35 /* -----------------------------------------------------------------------------
36 * Remembering the current thread's Task
37 * -------------------------------------------------------------------------- */
39 // A thread-local-storage key that we can use to get access to the
40 // current thread's Task structure.
41 #if defined(THREADED_RTS)
42 ThreadLocalKey currentTaskKey;
47 /* -----------------------------------------------------------------------------
48 * Rest of the Task API
49 * -------------------------------------------------------------------------- */
52 initTaskManager (void)
54 static int initialized = 0;
60 maxWorkers = DEFAULT_MAX_WORKERS;
62 #if defined(THREADED_RTS)
63 newThreadLocalKey(¤tTaskKey);
70 stopTaskManager (void)
72 IF_DEBUG(scheduler, sched_belch("stopping task manager, %d tasks still running", tasksRunning));
79 #if defined(THREADED_RTS)
80 long currentElapsedTime, currentUserTime, elapsedGCTime;
84 task = stgMallocBytes(sizeof(Task), "newTask");
87 task->stopped = rtsFalse;
88 task->suspended_tso = NULL;
90 task->stat = NoStatus;
93 #if defined(THREADED_RTS)
94 initCondition(&task->cond);
95 initMutex(&task->lock);
96 task->wakeup = rtsFalse;
99 #if defined(THREADED_RTS)
100 stat_getTimes(¤tElapsedTime, ¤tUserTime, &elapsedGCTime);
101 task->mut_time = 0.0;
102 task->mut_etime = 0.0;
104 task->gc_etime = 0.0;
105 task->muttimestart = currentUserTime;
106 task->elapsedtimestart = currentElapsedTime;
111 task->return_link = NULL;
113 task->all_link = all_tasks;
127 ASSERT_LOCK_HELD(&sched_mutex);
128 if (task_free_list == NULL) {
131 task = task_free_list;
132 task_free_list = task->next;
135 task->stopped = rtsFalse;
137 #if defined(THREADED_RTS)
138 task->id = osThreadId();
140 ASSERT(task->cap == NULL);
146 IF_DEBUG(scheduler,sched_belch("new task (taskCount: %d)", taskCount););
151 boundTaskExiting (Task *task)
153 task->stopped = rtsTrue;
156 #if defined(THREADED_RTS)
157 ASSERT(osThreadId() == task->id);
159 ASSERT(myTask() == task);
160 setMyTask(task->prev_stack);
164 // sadly, we need a lock around the free task list. Todo: eliminate.
165 ACQUIRE_LOCK(&sched_mutex);
166 task->next = task_free_list;
167 task_free_list = task;
168 RELEASE_LOCK(&sched_mutex);
170 IF_DEBUG(scheduler,sched_belch("task exiting"));
174 discardTask (Task *task)
176 ASSERT_LOCK_HELD(&sched_mutex);
177 #if defined(THREADED_RTS)
178 closeCondition(&task->cond);
180 task->stopped = rtsTrue;
182 task->next = task_free_list;
183 task_free_list = task;
187 taskStop (Task *task)
189 #if defined(THREADED_RTS)
191 long currentElapsedTime, currentUserTime, elapsedGCTime;
194 ASSERT(task->id == id);
195 ASSERT(myTask() == task);
197 stat_getTimes(¤tElapsedTime, ¤tUserTime, &elapsedGCTime);
200 currentUserTime - task->muttimestart - task->gc_time;
202 currentElapsedTime - task->elapsedtimestart - elapsedGCTime;
204 if (task->mut_time < 0.0) { task->mut_time = 0.0; }
205 if (task->mut_etime < 0.0) { task->mut_etime = 0.0; }
208 task->stopped = rtsTrue;
213 resetTaskManagerAfterFork (void)
219 #if defined(THREADED_RTS)
222 startWorkerTask (Capability *cap,
223 void OSThreadProcAttr (*taskStart)(Task *task))
229 if (workerCount >= maxWorkers) {
230 barf("too many workers; runaway worker creation?");
234 // A worker always gets a fresh Task structure.
239 // The lock here is to synchronise with taskStart(), to make sure
240 // that we have finished setting up the Task structure before the
241 // worker thread reads it.
242 ACQUIRE_LOCK(&task->lock);
246 // Give the capability directly to the worker; we can't let anyone
247 // else get in, because the new worker Task has nowhere to go to
248 // sleep so that it could be woken up again.
249 ASSERT_LOCK_HELD(&cap->lock);
250 cap->running_task = task;
252 r = createOSThread(&tid, (OSThreadProc *)taskStart, task);
254 barf("startTask: Can't create new task");
257 IF_DEBUG(scheduler,sched_belch("new worker task (taskCount: %d)", taskCount););
261 // ok, finished with the Task struct.
262 RELEASE_LOCK(&task->lock);
265 #endif /* THREADED_RTS */
269 static void *taskId(Task *task)
272 return (void *)task->id;
278 void printAllTasks(void);
284 for (task = all_tasks; task != NULL; task = task->all_link) {
285 debugBelch("task %p is %s, ", taskId(task), task->stopped ? "stopped" : "alive");
286 if (!task->stopped) {
288 debugBelch("on capability %d, ", task->cap->no);
291 debugBelch("bound to thread %d", task->tso->id);
293 debugBelch("worker");