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 maxWorkers = DEFAULT_MAX_WORKERS;
63 #if defined(THREADED_RTS)
64 newThreadLocalKey(¤tTaskKey);
71 stopTaskManager (void)
73 debugTrace(DEBUG_sched,
74 "stopping task manager, %d tasks still running",
82 #if defined(THREADED_RTS)
83 Ticks currentElapsedTime, currentUserTime;
87 task = stgMallocBytes(sizeof(Task), "newTask");
90 task->stopped = rtsFalse;
91 task->suspended_tso = NULL;
93 task->stat = NoStatus;
96 #if defined(THREADED_RTS)
97 initCondition(&task->cond);
98 initMutex(&task->lock);
99 task->wakeup = rtsFalse;
102 #if defined(THREADED_RTS)
103 currentUserTime = getThreadCPUTime();
104 currentElapsedTime = getProcessElapsedTime();
109 task->muttimestart = currentUserTime;
110 task->elapsedtimestart = currentElapsedTime;
115 task->return_link = NULL;
117 task->all_link = all_tasks;
131 ASSERT_LOCK_HELD(&sched_mutex);
132 if (task_free_list == NULL) {
135 task = task_free_list;
136 task_free_list = task->next;
139 task->stopped = rtsFalse;
141 #if defined(THREADED_RTS)
142 task->id = osThreadId();
144 ASSERT(task->cap == NULL);
150 debugTrace(DEBUG_sched, "new task (taskCount: %d)", taskCount);
155 boundTaskExiting (Task *task)
157 task->stopped = rtsTrue;
160 #if defined(THREADED_RTS)
161 ASSERT(osThreadId() == task->id);
163 ASSERT(myTask() == task);
164 setMyTask(task->prev_stack);
168 // sadly, we need a lock around the free task list. Todo: eliminate.
169 ACQUIRE_LOCK(&sched_mutex);
170 task->next = task_free_list;
171 task_free_list = task;
172 RELEASE_LOCK(&sched_mutex);
174 debugTrace(DEBUG_sched, "task exiting");
178 #define TASK_ID(t) (t)->id
180 #define TASK_ID(t) (t)
184 discardTask (Task *task)
186 ASSERT_LOCK_HELD(&sched_mutex);
187 if (!task->stopped) {
188 debugTrace(DEBUG_sched, "discarding task %p", TASK_ID(task));
191 task->stopped = rtsTrue;
193 task->next = task_free_list;
194 task_free_list = task;
199 taskTimeStamp (Task *task USED_IF_THREADS)
201 #if defined(THREADED_RTS)
202 Ticks currentElapsedTime, currentUserTime, elapsedGCTime;
204 currentUserTime = getThreadCPUTime();
205 currentElapsedTime = getProcessElapsedTime();
207 // XXX this is wrong; we want elapsed GC time since the
209 elapsedGCTime = stat_getElapsedGCTime();
212 currentUserTime - task->muttimestart - task->gc_time;
214 currentElapsedTime - task->elapsedtimestart - elapsedGCTime;
216 if (task->mut_time < 0) { task->mut_time = 0; }
217 if (task->mut_etime < 0) { task->mut_etime = 0; }
222 workerTaskStop (Task *task)
224 #if defined(THREADED_RTS)
227 ASSERT(task->id == id);
228 ASSERT(myTask() == task);
232 task->stopped = rtsTrue;
237 resetTaskManagerAfterFork (void)
243 #if defined(THREADED_RTS)
246 startWorkerTask (Capability *cap,
247 void OSThreadProcAttr (*taskStart)(Task *task))
253 if (workerCount >= maxWorkers) {
254 barf("too many workers; runaway worker creation?");
258 // A worker always gets a fresh Task structure.
263 // The lock here is to synchronise with taskStart(), to make sure
264 // that we have finished setting up the Task structure before the
265 // worker thread reads it.
266 ACQUIRE_LOCK(&task->lock);
270 // Give the capability directly to the worker; we can't let anyone
271 // else get in, because the new worker Task has nowhere to go to
272 // sleep so that it could be woken up again.
273 ASSERT_LOCK_HELD(&cap->lock);
274 cap->running_task = task;
276 r = createOSThread(&tid, (OSThreadProc *)taskStart, task);
278 barf("startTask: Can't create new task");
281 debugTrace(DEBUG_sched, "new worker task (taskCount: %d)", taskCount);
285 // ok, finished with the Task struct.
286 RELEASE_LOCK(&task->lock);
289 #endif /* THREADED_RTS */
293 static void *taskId(Task *task)
296 return (void *)task->id;
302 void printAllTasks(void);
308 for (task = all_tasks; task != NULL; task = task->all_link) {
309 debugBelch("task %p is %s, ", taskId(task), task->stopped ? "stopped" : "alive");
310 if (!task->stopped) {
312 debugBelch("on capability %d, ", task->cap->no);
315 debugBelch("bound to thread %d", task->tso->id);
317 debugBelch("worker");