2012-12-10 17:44:02 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-04-02 22:18:01 -05:00
|
|
|
#include "rust_sched_loop.h"
|
2012-01-06 14:06:35 -06:00
|
|
|
#include "rust_util.h"
|
2012-02-03 17:12:18 -06:00
|
|
|
#include "rust_scheduler.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-09-27 19:14:18 -05:00
|
|
|
#ifndef _WIN32
|
2012-03-29 18:31:30 -05:00
|
|
|
pthread_key_t rust_sched_loop::task_key;
|
2011-09-27 19:14:18 -05:00
|
|
|
#else
|
2012-03-29 18:31:30 -05:00
|
|
|
DWORD rust_sched_loop::task_key;
|
2011-09-27 19:14:18 -05:00
|
|
|
#endif
|
|
|
|
|
2012-02-10 03:22:10 -06:00
|
|
|
const size_t C_STACK_SIZE = 1024*1024;
|
2012-02-09 00:53:58 -06:00
|
|
|
|
2012-03-29 18:31:30 -05:00
|
|
|
bool rust_sched_loop::tls_initialized = false;
|
2011-09-27 19:14:18 -05:00
|
|
|
|
2012-07-20 17:06:17 -05:00
|
|
|
rust_sched_loop::rust_sched_loop(rust_scheduler *sched, int id, bool killed) :
|
2012-04-02 03:11:58 -05:00
|
|
|
_log(this),
|
2012-03-01 23:41:11 -06:00
|
|
|
id(id),
|
|
|
|
should_exit(false),
|
|
|
|
cached_c_stack(NULL),
|
2012-03-17 20:33:48 -05:00
|
|
|
dead_task(NULL),
|
2012-07-20 17:06:17 -05:00
|
|
|
killed(killed),
|
2012-03-30 19:10:02 -05:00
|
|
|
pump_signal(NULL),
|
2012-03-17 20:12:15 -05:00
|
|
|
kernel(sched->kernel),
|
|
|
|
sched(sched),
|
2012-03-01 23:41:11 -06:00
|
|
|
log_lvl(log_debug),
|
2011-07-27 16:34:39 -05:00
|
|
|
min_stack_size(kernel->env->min_stack_size),
|
2012-04-02 03:11:58 -05:00
|
|
|
local_region(kernel->env, false),
|
2012-07-12 20:20:39 -05:00
|
|
|
// FIXME #2891: calculate a per-scheduler name.
|
2012-03-01 23:41:11 -06:00
|
|
|
name("main")
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-04-07 15:05:45 -05:00
|
|
|
LOGPTR(this, "new dom", (uintptr_t)this);
|
2012-05-20 08:06:54 -05:00
|
|
|
isaac_init(kernel, &rctx, NULL);
|
2011-09-27 19:14:18 -05:00
|
|
|
|
|
|
|
if (!tls_initialized)
|
|
|
|
init_tls();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::activate(rust_task *task) {
|
2012-03-30 20:46:32 -05:00
|
|
|
lock.must_have_lock();
|
2011-09-27 21:19:19 -05:00
|
|
|
task->ctx.next = &c_context;
|
2011-05-31 19:44:54 -05:00
|
|
|
DLOG(this, task, "descheduling...");
|
2011-07-23 21:03:02 -05:00
|
|
|
lock.unlock();
|
2012-02-14 13:58:27 -06:00
|
|
|
prepare_c_stack(task);
|
2011-09-27 21:19:19 -05:00
|
|
|
task->ctx.swap(c_context);
|
2012-03-21 02:31:40 -05:00
|
|
|
task->cleanup_after_turn();
|
2012-02-09 03:13:32 -06:00
|
|
|
unprepare_c_stack();
|
2011-07-23 21:03:02 -05:00
|
|
|
lock.lock();
|
2011-05-31 19:44:54 -05:00
|
|
|
DLOG(this, task, "task has returned");
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::fail() {
|
2012-06-03 19:53:24 -05:00
|
|
|
_log.log(NULL, log_err, "domain %s @0x%" PRIxPTR " root task failed",
|
2010-08-08 21:24:35 -05:00
|
|
|
name, this);
|
2011-08-10 14:57:53 -05:00
|
|
|
kernel->fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::kill_all_tasks() {
|
2012-03-03 22:50:11 -06:00
|
|
|
std::vector<rust_task*> all_tasks;
|
2011-08-10 14:57:53 -05:00
|
|
|
|
2012-03-03 22:50:11 -06:00
|
|
|
{
|
|
|
|
scoped_lock with(lock);
|
2012-07-20 17:06:17 -05:00
|
|
|
// Any task created after this will be killed. See transition, below.
|
|
|
|
killed = true;
|
2012-03-03 22:50:11 -06:00
|
|
|
|
|
|
|
for (size_t i = 0; i < running_tasks.length(); i++) {
|
2012-07-20 17:14:04 -05:00
|
|
|
rust_task *t = running_tasks[i];
|
|
|
|
t->ref();
|
|
|
|
all_tasks.push_back(t);
|
2012-03-03 22:50:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < blocked_tasks.length(); i++) {
|
2012-07-20 17:14:04 -05:00
|
|
|
rust_task *t = blocked_tasks[i];
|
|
|
|
t->ref();
|
|
|
|
all_tasks.push_back(t);
|
2012-03-03 22:50:11 -06:00
|
|
|
}
|
2011-08-10 14:57:53 -05:00
|
|
|
}
|
|
|
|
|
2012-03-03 22:50:11 -06:00
|
|
|
while (!all_tasks.empty()) {
|
|
|
|
rust_task *task = all_tasks.back();
|
|
|
|
all_tasks.pop_back();
|
|
|
|
task->kill();
|
2012-07-20 17:14:04 -05:00
|
|
|
task->deref();
|
2011-08-10 14:57:53 -05:00
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::number_of_live_tasks() {
|
2010-06-23 23:03:09 -05:00
|
|
|
return running_tasks.length() + blocked_tasks.length();
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Delete any dead tasks.
|
|
|
|
*/
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::reap_dead_tasks() {
|
2012-03-30 20:37:30 -05:00
|
|
|
lock.must_have_lock();
|
|
|
|
|
2012-03-17 20:33:48 -05:00
|
|
|
if (dead_task == NULL) {
|
2011-08-20 18:05:18 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-02 03:39:19 -06:00
|
|
|
// Dereferencing the task will probably cause it to be released
|
|
|
|
// from the scheduler, which may end up trying to take this lock
|
2011-08-20 18:05:18 -05:00
|
|
|
lock.unlock();
|
|
|
|
|
2012-03-02 03:39:19 -06:00
|
|
|
dead_task->delete_all_stacks();
|
|
|
|
// Deref the task, which may cause it to request us to release it
|
|
|
|
dead_task->deref();
|
2012-03-17 20:33:48 -05:00
|
|
|
dead_task = NULL;
|
2011-08-20 18:05:18 -05:00
|
|
|
|
|
|
|
lock.lock();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2012-02-07 01:38:22 -06:00
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::release_task(rust_task *task) {
|
2012-02-07 01:38:22 -06:00
|
|
|
// Nobody should have a ref to the task at this point
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(task->get_ref_count() == 0);
|
2012-02-07 01:38:22 -06:00
|
|
|
// Now delete the task, which will require using this thread's
|
|
|
|
// memory region.
|
|
|
|
delete task;
|
|
|
|
// Now release the task from the scheduler, which may trigger this
|
|
|
|
// thread to exit
|
|
|
|
sched->release_task();
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Schedules a running task for execution. Only running tasks can be
|
|
|
|
* activated. Blocked tasks have to be unblocked before they can be
|
|
|
|
* activated.
|
|
|
|
*
|
|
|
|
* Returns NULL if no tasks can be scheduled.
|
|
|
|
*/
|
2010-06-23 23:03:09 -05:00
|
|
|
rust_task *
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::schedule_task() {
|
2012-03-30 20:46:32 -05:00
|
|
|
lock.must_have_lock();
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(this);
|
2010-06-23 23:03:09 -05:00
|
|
|
if (running_tasks.length() > 0) {
|
2011-09-20 17:34:47 -05:00
|
|
|
size_t k = isaac_rand(&rctx);
|
2011-06-20 19:19:50 -05:00
|
|
|
// Look around for a runnable task, starting at k.
|
|
|
|
for(size_t j = 0; j < running_tasks.length(); ++j) {
|
|
|
|
size_t i = (j + k) % running_tasks.length();
|
2012-02-02 18:30:22 -06:00
|
|
|
return (rust_task *)running_tasks[i];
|
2010-08-11 23:23:34 -05:00
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-28 16:53:08 -05:00
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::log_state() {
|
2011-12-28 13:53:12 -06:00
|
|
|
if (log_rt_task < log_debug) return;
|
2011-04-19 05:21:57 -05:00
|
|
|
|
2010-07-28 16:53:08 -05:00
|
|
|
if (!running_tasks.is_empty()) {
|
2012-06-03 19:53:24 -05:00
|
|
|
_log.log(NULL, log_debug, "running tasks:");
|
2010-07-28 16:53:08 -05:00
|
|
|
for (size_t i = 0; i < running_tasks.length(); i++) {
|
2012-06-03 19:53:24 -05:00
|
|
|
_log.log(NULL, log_debug, "\t task: %s @0x%" PRIxPTR,
|
2010-08-11 23:23:34 -05:00
|
|
|
running_tasks[i]->name,
|
2012-02-02 18:24:32 -06:00
|
|
|
running_tasks[i]);
|
2010-07-28 16:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blocked_tasks.is_empty()) {
|
2012-06-03 19:53:24 -05:00
|
|
|
_log.log(NULL, log_debug, "blocked tasks:");
|
2010-07-28 16:53:08 -05:00
|
|
|
for (size_t i = 0; i < blocked_tasks.length(); i++) {
|
2012-06-03 19:53:24 -05:00
|
|
|
_log.log(NULL, log_debug, "\t task: %s @0x%" PRIxPTR
|
2012-04-01 01:12:06 -05:00
|
|
|
", blocked on: 0x%" PRIxPTR " '%s'",
|
2010-08-08 21:24:35 -05:00
|
|
|
blocked_tasks[i]->name, blocked_tasks[i],
|
2012-03-03 01:40:27 -06:00
|
|
|
blocked_tasks[i]->get_cond(),
|
|
|
|
blocked_tasks[i]->get_cond_name());
|
2010-07-28 16:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-29 19:07:50 -05:00
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
void
|
2012-03-30 19:10:02 -05:00
|
|
|
rust_sched_loop::on_pump_loop(rust_signal *signal) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(pump_signal == NULL);
|
|
|
|
assert(signal != NULL);
|
2012-03-30 19:10:02 -05:00
|
|
|
pump_signal = signal;
|
|
|
|
}
|
2012-03-29 19:07:50 -05:00
|
|
|
|
2012-03-30 19:10:02 -05:00
|
|
|
void
|
|
|
|
rust_sched_loop::pump_loop() {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(pump_signal != NULL);
|
2012-03-30 19:10:02 -05:00
|
|
|
pump_signal->signal();
|
2012-03-29 19:07:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rust_sched_loop_state
|
|
|
|
rust_sched_loop::run_single_turn() {
|
2012-03-30 19:10:02 -05:00
|
|
|
DLOG(this, task,
|
|
|
|
"scheduler %d resuming ...", id);
|
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
lock.lock();
|
2011-06-20 19:19:50 -05:00
|
|
|
|
2012-03-29 19:07:50 -05:00
|
|
|
if (!should_exit) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(dead_task == NULL && "Tasks should only die after running");
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-02-03 19:26:54 -06:00
|
|
|
DLOG(this, dom, "worker %d, number_of_live_tasks = %d",
|
|
|
|
id, number_of_live_tasks());
|
2011-06-20 19:19:50 -05:00
|
|
|
|
2012-02-02 20:17:33 -06:00
|
|
|
rust_task *scheduled_task = schedule_task();
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
if (scheduled_task == NULL) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log_state();
|
|
|
|
DLOG(this, task,
|
2011-06-20 19:19:50 -05:00
|
|
|
"all tasks are blocked, scheduler id %d yielding ...",
|
|
|
|
id);
|
2012-03-29 19:07:50 -05:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
return sched_loop_state_block;
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
2012-07-12 18:52:13 -05:00
|
|
|
scheduled_task->assert_is_running();
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task,
|
2011-07-27 16:51:25 -05:00
|
|
|
"activating task %s 0x%" PRIxPTR
|
|
|
|
", state: %s",
|
|
|
|
scheduled_task->name,
|
|
|
|
(uintptr_t)scheduled_task,
|
2012-03-17 20:19:49 -05:00
|
|
|
state_name(scheduled_task->get_state()));
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-09-27 19:14:18 -05:00
|
|
|
place_task_in_tls(scheduled_task);
|
|
|
|
|
2011-06-20 19:19:50 -05:00
|
|
|
DLOG(this, task,
|
|
|
|
"Running task %p on worker %d",
|
|
|
|
scheduled_task, id);
|
2010-07-19 16:05:18 -05:00
|
|
|
activate(scheduled_task);
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task,
|
2011-06-20 19:19:50 -05:00
|
|
|
"returned from task %s @0x%" PRIxPTR
|
2012-02-08 19:46:12 -06:00
|
|
|
" in state '%s', worker id=%d" PRIxPTR,
|
2011-06-20 19:19:50 -05:00
|
|
|
scheduled_task->name,
|
|
|
|
(uintptr_t)scheduled_task,
|
2012-03-17 20:19:49 -05:00
|
|
|
state_name(scheduled_task->get_state()),
|
2011-06-20 19:19:50 -05:00
|
|
|
id);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-02-02 20:17:33 -06:00
|
|
|
reap_dead_tasks();
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-03-29 19:07:50 -05:00
|
|
|
lock.unlock();
|
|
|
|
return sched_loop_state_keep_going;
|
|
|
|
} else {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(running_tasks.is_empty() && "Should have no running tasks");
|
|
|
|
assert(blocked_tasks.is_empty() && "Should have no blocked tasks");
|
|
|
|
assert(dead_task == NULL && "Should have no dead tasks");
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-03-29 19:07:50 -05:00
|
|
|
DLOG(this, dom, "finished main-loop %d", id);
|
2011-06-20 19:19:50 -05:00
|
|
|
|
2012-03-29 19:07:50 -05:00
|
|
|
lock.unlock();
|
2012-02-09 03:13:32 -06:00
|
|
|
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(!extra_c_stack);
|
2012-03-29 19:07:50 -05:00
|
|
|
if (cached_c_stack) {
|
|
|
|
destroy_stack(kernel->region(), cached_c_stack);
|
|
|
|
cached_c_stack = NULL;
|
|
|
|
}
|
2012-03-29 17:21:32 -05:00
|
|
|
|
2012-03-29 19:07:50 -05:00
|
|
|
sched->release_task_thread();
|
|
|
|
return sched_loop_state_exit;
|
2012-02-09 03:13:32 -06:00
|
|
|
}
|
2011-06-20 19:19:50 -05:00
|
|
|
}
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2012-03-14 22:22:34 -05:00
|
|
|
rust_task *
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::create_task(rust_task *spawner, const char *name) {
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task *task =
|
2011-07-18 14:02:26 -05:00
|
|
|
new (this->kernel, "rust_task")
|
2012-07-12 18:52:32 -05:00
|
|
|
rust_task(this, task_state_newborn,
|
2012-07-17 19:40:40 -05:00
|
|
|
name, kernel->env->min_stack_size);
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task, "created task: " PTR ", spawner: %s, name: %s",
|
2012-07-17 19:40:40 -05:00
|
|
|
task, spawner ? spawner->name : "(none)", name);
|
2011-08-01 13:01:59 -05:00
|
|
|
|
2012-03-14 22:55:57 -05:00
|
|
|
task->id = kernel->generate_task_id();
|
2012-03-14 22:22:34 -05:00
|
|
|
return task;
|
2010-09-10 03:21:29 -05:00
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-03-17 20:12:15 -05:00
|
|
|
rust_task_list *
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::state_list(rust_task_state state) {
|
2012-03-17 20:12:15 -05:00
|
|
|
switch (state) {
|
|
|
|
case task_state_running:
|
|
|
|
return &running_tasks;
|
|
|
|
case task_state_blocked:
|
|
|
|
return &blocked_tasks;
|
2012-03-17 20:29:10 -05:00
|
|
|
default:
|
|
|
|
return NULL;
|
2012-03-17 20:12:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:19:49 -05:00
|
|
|
const char *
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::state_name(rust_task_state state) {
|
2012-03-17 20:19:49 -05:00
|
|
|
switch (state) {
|
|
|
|
case task_state_newborn:
|
|
|
|
return "newborn";
|
|
|
|
case task_state_running:
|
|
|
|
return "running";
|
|
|
|
case task_state_blocked:
|
|
|
|
return "blocked";
|
|
|
|
case task_state_dead:
|
|
|
|
return "dead";
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 01:12:06 -05:00
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::transition(rust_task *task,
|
2012-03-17 20:12:15 -05:00
|
|
|
rust_task_state src, rust_task_state dst,
|
2012-03-03 04:36:53 -06:00
|
|
|
rust_cond *cond, const char* cond_name) {
|
2012-03-17 19:39:46 -05:00
|
|
|
scoped_lock with(lock);
|
2012-03-03 04:36:53 -06:00
|
|
|
DLOG(this, task,
|
|
|
|
"task %s " PTR " state change '%s' -> '%s' while in '%s'",
|
2012-03-17 20:19:49 -05:00
|
|
|
name, (uintptr_t)this, state_name(src), state_name(dst),
|
|
|
|
state_name(task->get_state()));
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(task->get_state() == src);
|
2012-03-17 20:29:10 -05:00
|
|
|
rust_task_list *src_list = state_list(src);
|
|
|
|
if (src_list) {
|
|
|
|
src_list->remove(task);
|
|
|
|
}
|
|
|
|
rust_task_list *dst_list = state_list(dst);
|
|
|
|
if (dst_list) {
|
|
|
|
dst_list->append(task);
|
|
|
|
}
|
2012-03-17 20:33:48 -05:00
|
|
|
if (dst == task_state_dead) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(dead_task == NULL);
|
2012-03-17 20:33:48 -05:00
|
|
|
dead_task = task;
|
|
|
|
}
|
2012-03-03 04:36:53 -06:00
|
|
|
task->set_state(dst, cond, cond_name);
|
|
|
|
|
2012-07-20 17:06:17 -05:00
|
|
|
// If the entire runtime is failing, newborn tasks must be doomed.
|
|
|
|
if (src == task_state_newborn && killed) {
|
|
|
|
task->kill_inner();
|
|
|
|
}
|
|
|
|
|
2012-03-30 19:10:02 -05:00
|
|
|
pump_loop();
|
2011-07-23 21:03:02 -05:00
|
|
|
}
|
|
|
|
|
2011-09-27 19:14:18 -05:00
|
|
|
#ifndef _WIN32
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::init_tls() {
|
2011-09-27 19:14:18 -05:00
|
|
|
int result = pthread_key_create(&task_key, NULL);
|
|
|
|
assert(!result && "Couldn't create the TLS key!");
|
|
|
|
tls_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::place_task_in_tls(rust_task *task) {
|
2011-09-27 19:14:18 -05:00
|
|
|
int result = pthread_setspecific(task_key, task);
|
|
|
|
assert(!result && "Couldn't place the task in TLS!");
|
2011-12-01 17:26:42 -06:00
|
|
|
task->record_stack_limit();
|
2011-09-27 19:14:18 -05:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::init_tls() {
|
2011-09-27 19:14:18 -05:00
|
|
|
task_key = TlsAlloc();
|
|
|
|
assert(task_key != TLS_OUT_OF_INDEXES && "Couldn't create the TLS key!");
|
|
|
|
tls_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::place_task_in_tls(rust_task *task) {
|
2011-09-27 19:14:18 -05:00
|
|
|
BOOL result = TlsSetValue(task_key, task);
|
|
|
|
assert(result && "Couldn't place the task in TLS!");
|
2011-12-08 18:47:33 -06:00
|
|
|
task->record_stack_limit();
|
2011-09-27 19:14:18 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-02 19:02:50 -06:00
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::exit() {
|
2012-02-02 19:02:50 -06:00
|
|
|
scoped_lock with(lock);
|
2012-03-29 19:07:50 -05:00
|
|
|
DLOG(this, dom, "Requesting exit for thread %d", id);
|
2012-02-02 19:02:50 -06:00
|
|
|
should_exit = true;
|
2012-03-30 19:10:02 -05:00
|
|
|
pump_loop();
|
2012-02-02 19:02:50 -06:00
|
|
|
}
|
|
|
|
|
2012-02-09 03:13:32 -06:00
|
|
|
// Before activating each task, make sure we have a C stack available.
|
|
|
|
// It needs to be allocated ahead of time (while we're on our own
|
|
|
|
// stack), because once we're on the Rust stack we won't have enough
|
|
|
|
// room to do the allocation
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::prepare_c_stack(rust_task *task) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(!extra_c_stack);
|
2012-02-14 13:58:27 -06:00
|
|
|
if (!cached_c_stack && !task->have_c_stack()) {
|
2012-02-27 17:42:22 -06:00
|
|
|
cached_c_stack = create_stack(kernel->region(), C_STACK_SIZE);
|
2012-02-09 03:13:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop::unprepare_c_stack() {
|
2012-02-09 03:13:32 -06:00
|
|
|
if (extra_c_stack) {
|
2012-02-27 17:42:22 -06:00
|
|
|
destroy_stack(kernel->region(), extra_c_stack);
|
2012-02-09 03:13:32 -06:00
|
|
|
extra_c_stack = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 70;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|