5 #include "rust_internal.h"
9 pthread_key_t rust_scheduler::task_key;
11 DWORD rust_scheduler::task_key;
14 bool rust_scheduler::tls_initialized = false;
16 rust_scheduler::rust_scheduler(rust_kernel *kernel,
24 // TODO: calculate a per scheduler name.
26 newborn_tasks(this, "newborn"),
27 running_tasks(this, "running"),
28 blocked_tasks(this, "blocked"),
29 dead_tasks(this, "dead"),
33 min_stack_size(kernel->env->min_stack_size),
36 LOGPTR(this, "new dom", (uintptr_t)this);
37 isaac_init(this, &rctx);
39 pthread_attr_init(&attr);
40 pthread_attr_setstacksize(&attr, 1024 * 1024);
41 pthread_attr_setdetachstate(&attr, true);
48 rust_scheduler::~rust_scheduler() {
49 DLOG(this, dom, "~rust_scheduler %s @0x%" PRIxPTR, name, (uintptr_t)this);
51 newborn_tasks.delete_all();
52 running_tasks.delete_all();
53 blocked_tasks.delete_all();
54 dead_tasks.delete_all();
56 pthread_attr_destroy(&attr);
61 rust_scheduler::activate(rust_task *task) {
62 task->ctx.next = &c_context;
63 DLOG(this, task, "descheduling...");
65 task->ctx.swap(c_context);
67 DLOG(this, task, "task has returned");
71 rust_scheduler::log(rust_task* task, uint32_t level, char const *fmt, ...) {
75 vsnprintf(buf, sizeof(buf), fmt, args);
76 _log.trace_ln(task, level, buf);
81 rust_scheduler::fail() {
82 log(NULL, log_err, "domain %s @0x%" PRIxPTR " root task failed",
84 I(this, kernel->rval == 0);
85 kernel->rval = PROC_FAIL_CODE;
90 rust_scheduler::kill_all_tasks() {
91 I(this, !lock.lock_held_by_current_thread());
92 scoped_lock with(lock);
94 for (size_t i = 0; i < running_tasks.length(); i++) {
95 // We don't want the failure of these tasks to propagate back
96 // to the kernel again since we're already failing everything
97 running_tasks[i]->unsupervise();
98 running_tasks[i]->kill();
101 for (size_t i = 0; i < blocked_tasks.length(); i++) {
102 blocked_tasks[i]->unsupervise();
103 blocked_tasks[i]->kill();
108 rust_scheduler::number_of_live_tasks() {
109 return running_tasks.length() + blocked_tasks.length();
113 * Delete any dead tasks.
116 rust_scheduler::reap_dead_tasks(int id) {
117 I(this, lock.lock_held_by_current_thread());
118 if (dead_tasks.length() == 0) {
122 // First make a copy of the dead_task list with the lock held
123 size_t dead_tasks_len = dead_tasks.length();
124 rust_task **dead_tasks_copy = (rust_task**)
125 srv->malloc(sizeof(rust_task*) * dead_tasks_len);
126 for (size_t i = 0; i < dead_tasks_len; ++i) {
127 rust_task *task = dead_tasks[i];
128 dead_tasks_copy[i] = task;
131 // Now drop the lock and futz with the tasks. This avoids establishing
132 // a sched->lock then task->lock locking order, which would be devestating
136 for (size_t i = 0; i < dead_tasks_len; ++i) {
137 rust_task *task = dead_tasks_copy[i];
139 // Make sure this task isn't still running somewhere else...
140 if (task->can_schedule(id)) {
142 "deleting unreferenced dead task %s @0x%" PRIxPTR,
147 dead_tasks_copy[i] = NULL;
151 // Now grab the lock again and remove the tasks that were truly dead
154 for (size_t i = 0; i < dead_tasks_len; ++i) {
155 rust_task *task = dead_tasks_copy[i];
157 dead_tasks.remove(task);
161 // Now unlock again because we have to actually free the dead tasks,
162 // and that may end up wanting to lock the task and sched locks
163 // again (via target->send)
166 for (size_t i = 0; i < dead_tasks_len; ++i) {
167 rust_task *task = dead_tasks_copy[i];
170 sync::decrement(kernel->live_tasks);
171 kernel->wakeup_schedulers();
174 srv->free(dead_tasks_copy);
180 * Schedules a running task for execution. Only running tasks can be
181 * activated. Blocked tasks have to be unblocked before they can be
184 * Returns NULL if no tasks can be scheduled.
187 rust_scheduler::schedule_task(int id) {
189 // FIXME: in the face of failing tasks, this is not always right.
190 // I(this, n_live_tasks() > 0);
191 if (running_tasks.length() > 0) {
192 size_t k = isaac_rand(&rctx);
193 // Look around for a runnable task, starting at k.
194 for(size_t j = 0; j < running_tasks.length(); ++j) {
195 size_t i = (j + k) % running_tasks.length();
196 if (running_tasks[i]->can_schedule(id)) {
197 return (rust_task *)running_tasks[i];
205 rust_scheduler::log_state() {
206 if (log_rt_task < log_debug) return;
208 if (!running_tasks.is_empty()) {
209 log(NULL, log_debug, "running tasks:");
210 for (size_t i = 0; i < running_tasks.length(); i++) {
211 log(NULL, log_debug, "\t task: %s @0x%" PRIxPTR
212 " remaining: %" PRId64 " us",
213 running_tasks[i]->name,
215 running_tasks[i]->yield_timer.remaining_us());
219 if (!blocked_tasks.is_empty()) {
220 log(NULL, log_debug, "blocked tasks:");
221 for (size_t i = 0; i < blocked_tasks.length(); i++) {
222 log(NULL, log_debug, "\t task: %s @0x%" PRIxPTR ", blocked on: 0x%"
224 blocked_tasks[i]->name, blocked_tasks[i],
225 blocked_tasks[i]->cond, blocked_tasks[i]->cond_name);
229 if (!dead_tasks.is_empty()) {
230 log(NULL, log_debug, "dead tasks:");
231 for (size_t i = 0; i < dead_tasks.length(); i++) {
232 log(NULL, log_debug, "\t task: %s 0x%" PRIxPTR,
233 dead_tasks[i]->name, dead_tasks[i]);
238 * Starts the main scheduler loop which performs task scheduling for this
241 * Returns once no more tasks can be scheduled and all task ref_counts
245 rust_scheduler::start_main_loop() {
248 // Make sure someone is watching, to pull us out of infinite loops.
250 // FIXME: time-based interruption is not presently working; worked
251 // in rustboot and has been completely broken in rustc.
253 // rust_timer timer(this);
255 DLOG(this, dom, "started domain loop %d", id);
257 while (kernel->live_tasks > 0) {
258 A(this, kernel->is_deadlocked() == false, "deadlock");
260 DLOG(this, dom, "worker %d, number_of_live_tasks = %d, total = %d",
261 id, number_of_live_tasks(), kernel->live_tasks);
263 rust_task *scheduled_task = schedule_task(id);
265 if (scheduled_task == NULL) {
268 "all tasks are blocked, scheduler id %d yielding ...",
273 "scheduler %d resuming ...", id);
277 I(this, scheduled_task->running());
280 "activating task %s 0x%" PRIxPTR
283 scheduled_task->name,
284 (uintptr_t)scheduled_task,
285 scheduled_task->user.rust_sp,
286 scheduled_task->state->name);
288 place_task_in_tls(scheduled_task);
293 "Running task %p on worker %d",
295 scheduled_task->running_on = id;
296 activate(scheduled_task);
297 scheduled_task->running_on = -1;
300 "returned from task %s @0x%" PRIxPTR
301 " in state '%s', sp=0x%x, worker id=%d" PRIxPTR,
302 scheduled_task->name,
303 (uintptr_t)scheduled_task,
304 scheduled_task->state->name,
305 scheduled_task->user.rust_sp,
312 "terminated scheduler loop, reaping dead tasks ...");
314 while (dead_tasks.length() > 0) {
316 "waiting for %d dead tasks to become dereferenced, "
317 "scheduler yielding ...",
318 dead_tasks.length());
326 DLOG(this, dom, "finished main-loop %d", id);
332 rust_scheduler::get_cache() {
337 rust_scheduler::create_task(rust_task *spawner, const char *name) {
339 new (this->kernel, "rust_task")
340 rust_task (this, &newborn_tasks, spawner, name);
341 DLOG(this, task, "created task: " PTR ", spawner: %s, name: %s",
342 task, spawner ? spawner->name : "null", name);
344 task->pin(spawner->pinned_on);
348 scoped_lock with(lock);
349 newborn_tasks.append(task);
352 sync::increment(kernel->live_tasks);
357 void rust_scheduler::run() {
358 this->start_main_loop();
363 rust_scheduler::init_tls() {
364 int result = pthread_key_create(&task_key, NULL);
365 assert(!result && "Couldn't create the TLS key!");
366 tls_initialized = true;
370 rust_scheduler::place_task_in_tls(rust_task *task) {
371 int result = pthread_setspecific(task_key, task);
372 assert(!result && "Couldn't place the task in TLS!");
373 task->record_stack_limit();
377 rust_scheduler::get_task() {
378 if (!tls_initialized)
380 rust_task *task = reinterpret_cast<rust_task *>
381 (pthread_getspecific(task_key));
382 assert(task && "Couldn't get the task from TLS!");
387 rust_scheduler::init_tls() {
388 task_key = TlsAlloc();
389 assert(task_key != TLS_OUT_OF_INDEXES && "Couldn't create the TLS key!");
390 tls_initialized = true;
394 rust_scheduler::place_task_in_tls(rust_task *task) {
395 BOOL result = TlsSetValue(task_key, task);
396 assert(result && "Couldn't place the task in TLS!");
397 task->record_stack_limit();
401 rust_scheduler::get_task() {
402 if (!tls_initialized)
404 rust_task *task = reinterpret_cast<rust_task *>(TlsGetValue(task_key));
405 assert(task && "Couldn't get the task from TLS!");
414 // indent-tabs-mode: nil
416 // buffer-file-coding-system: utf-8-unix
417 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";