rt: Extract rust_sched_launcher from rust_task_thread
rust_sched_launcher is actually responsible for setting up the thread and starting the loop. There will be other implementations that do not actually set up a new thread, in order to support scheduling tasks on the main OS thread.
This commit is contained in:
parent
620b4d4946
commit
6bf8d19712
7 changed files with 71 additions and 37 deletions
1
mk/rt.mk
1
mk/rt.mk
|
@ -51,6 +51,7 @@ RUNTIME_CS_$(1) := \
|
||||||
rt/rust_run_program.cpp \
|
rt/rust_run_program.cpp \
|
||||||
rt/rust_env.cpp \
|
rt/rust_env.cpp \
|
||||||
rt/rust_task_thread.cpp \
|
rt/rust_task_thread.cpp \
|
||||||
|
rt/rust_sched_launcher.cpp \
|
||||||
rt/rust_scheduler.cpp \
|
rt/rust_scheduler.cpp \
|
||||||
rt/rust_task.cpp \
|
rt/rust_task.cpp \
|
||||||
rt/rust_stack.cpp \
|
rt/rust_stack.cpp \
|
||||||
|
|
16
src/rt/rust_sched_launcher.cpp
Normal file
16
src/rt/rust_sched_launcher.cpp
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include "rust_sched_launcher.h"
|
||||||
|
#include "rust_scheduler.h"
|
||||||
|
|
||||||
|
const size_t SCHED_STACK_SIZE = 1024*100;
|
||||||
|
|
||||||
|
rust_sched_launcher::rust_sched_launcher(rust_scheduler *sched,
|
||||||
|
rust_srv *srv, int id)
|
||||||
|
: rust_thread(SCHED_STACK_SIZE),
|
||||||
|
kernel(sched->kernel),
|
||||||
|
thread(sched, srv, id) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rust_sched_launcher::run() {
|
||||||
|
thread.start_main_loop();
|
||||||
|
}
|
29
src/rt/rust_sched_launcher.h
Normal file
29
src/rt/rust_sched_launcher.h
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef RUST_SCHED_LAUNCHER_H
|
||||||
|
#define RUST_SCHED_LAUNCHER_H
|
||||||
|
|
||||||
|
#include "rust_internal.h"
|
||||||
|
#include "sync/rust_thread.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <pthread.h>
|
||||||
|
#else
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class rust_sched_launcher
|
||||||
|
: public kernel_owned<rust_sched_launcher>,
|
||||||
|
public rust_thread {
|
||||||
|
public:
|
||||||
|
rust_kernel *kernel;
|
||||||
|
|
||||||
|
private:
|
||||||
|
rust_task_thread thread;
|
||||||
|
|
||||||
|
public:
|
||||||
|
rust_sched_launcher(rust_scheduler *sched, rust_srv *srv, int id);
|
||||||
|
|
||||||
|
virtual void run();
|
||||||
|
rust_task_thread *get_loop() { return &thread; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // RUST_SCHED_LAUNCHER_H
|
|
@ -1,5 +1,6 @@
|
||||||
#include "rust_scheduler.h"
|
#include "rust_scheduler.h"
|
||||||
#include "rust_util.h"
|
#include "rust_util.h"
|
||||||
|
#include "rust_sched_launcher.h"
|
||||||
|
|
||||||
rust_scheduler::rust_scheduler(rust_kernel *kernel,
|
rust_scheduler::rust_scheduler(rust_kernel *kernel,
|
||||||
rust_srv *srv,
|
rust_srv *srv,
|
||||||
|
@ -21,21 +22,20 @@ rust_scheduler::~rust_scheduler() {
|
||||||
destroy_task_threads();
|
destroy_task_threads();
|
||||||
}
|
}
|
||||||
|
|
||||||
rust_task_thread *
|
rust_sched_launcher *
|
||||||
rust_scheduler::create_task_thread(int id) {
|
rust_scheduler::create_task_thread(int id) {
|
||||||
rust_srv *srv = this->srv->clone();
|
rust_srv *srv = this->srv->clone();
|
||||||
rust_task_thread *thread =
|
rust_sched_launcher *thread =
|
||||||
new (kernel, "rust_task_thread") rust_task_thread(this, srv, id);
|
new (kernel, "rust_sched_launcher") rust_sched_launcher(this, srv, id);
|
||||||
KLOG(kernel, kern, "created task thread: " PTR ", id: %d, index: %d",
|
KLOG(kernel, kern, "created task thread: " PTR ", id: %d",
|
||||||
thread, id, thread->list_index);
|
thread, id);
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
rust_scheduler::destroy_task_thread(rust_task_thread *thread) {
|
rust_scheduler::destroy_task_thread(rust_sched_launcher *thread) {
|
||||||
KLOG(kernel, kern, "deleting task thread: " PTR ", name: %s, index: %d",
|
KLOG(kernel, kern, "deleting task thread: " PTR, thread);
|
||||||
thread, thread->name, thread->list_index);
|
rust_srv *srv = thread->get_loop()->srv;
|
||||||
rust_srv *srv = thread->srv;
|
|
||||||
delete thread;
|
delete thread;
|
||||||
delete srv;
|
delete srv;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ void
|
||||||
rust_scheduler::start_task_threads()
|
rust_scheduler::start_task_threads()
|
||||||
{
|
{
|
||||||
for(size_t i = 0; i < num_threads; ++i) {
|
for(size_t i = 0; i < num_threads; ++i) {
|
||||||
rust_task_thread *thread = threads[i];
|
rust_sched_launcher *thread = threads[i];
|
||||||
thread->start();
|
thread->start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ void
|
||||||
rust_scheduler::join_task_threads()
|
rust_scheduler::join_task_threads()
|
||||||
{
|
{
|
||||||
for(size_t i = 0; i < num_threads; ++i) {
|
for(size_t i = 0; i < num_threads; ++i) {
|
||||||
rust_task_thread *thread = threads[i];
|
rust_sched_launcher *thread = threads[i];
|
||||||
thread->join();
|
thread->join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,8 +77,8 @@ rust_scheduler::join_task_threads()
|
||||||
void
|
void
|
||||||
rust_scheduler::kill_all_tasks() {
|
rust_scheduler::kill_all_tasks() {
|
||||||
for(size_t i = 0; i < num_threads; ++i) {
|
for(size_t i = 0; i < num_threads; ++i) {
|
||||||
rust_task_thread *thread = threads[i];
|
rust_sched_launcher *thread = threads[i];
|
||||||
thread->kill_all_tasks();
|
thread->get_loop()->kill_all_tasks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,8 +92,8 @@ rust_scheduler::create_task(rust_task *spawner, const char *name) {
|
||||||
if (cur_thread >= num_threads)
|
if (cur_thread >= num_threads)
|
||||||
cur_thread = 0;
|
cur_thread = 0;
|
||||||
}
|
}
|
||||||
rust_task_thread *thread = threads[thread_no];
|
rust_sched_launcher *thread = threads[thread_no];
|
||||||
return thread->create_task(spawner, name);
|
return thread->get_loop()->create_task(spawner, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -118,7 +118,7 @@ rust_scheduler::exit() {
|
||||||
// scheduler will get destroyed, and our fields will cease to exist.
|
// scheduler will get destroyed, and our fields will cease to exist.
|
||||||
size_t current_num_threads = num_threads;
|
size_t current_num_threads = num_threads;
|
||||||
for(size_t i = 0; i < current_num_threads; ++i) {
|
for(size_t i = 0; i < current_num_threads; ++i) {
|
||||||
threads[i]->exit();
|
threads[i]->get_loop()->exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include "rust_internal.h"
|
#include "rust_internal.h"
|
||||||
|
|
||||||
|
class rust_sched_launcher;
|
||||||
|
|
||||||
class rust_scheduler : public kernel_owned<rust_scheduler> {
|
class rust_scheduler : public kernel_owned<rust_scheduler> {
|
||||||
// FIXME: Make these private
|
// FIXME: Make these private
|
||||||
public:
|
public:
|
||||||
|
@ -17,7 +19,7 @@ private:
|
||||||
// When this hits zero we'll tell the threads to exit
|
// When this hits zero we'll tell the threads to exit
|
||||||
uintptr_t live_tasks;
|
uintptr_t live_tasks;
|
||||||
|
|
||||||
array_list<rust_task_thread *> threads;
|
array_list<rust_sched_launcher *> threads;
|
||||||
const size_t num_threads;
|
const size_t num_threads;
|
||||||
size_t cur_thread;
|
size_t cur_thread;
|
||||||
|
|
||||||
|
@ -26,8 +28,8 @@ private:
|
||||||
void create_task_threads();
|
void create_task_threads();
|
||||||
void destroy_task_threads();
|
void destroy_task_threads();
|
||||||
|
|
||||||
rust_task_thread *create_task_thread(int id);
|
rust_sched_launcher *create_task_thread(int id);
|
||||||
void destroy_task_thread(rust_task_thread *thread);
|
void destroy_task_thread(rust_sched_launcher *thread);
|
||||||
|
|
||||||
void exit();
|
void exit();
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ pthread_key_t rust_task_thread::task_key;
|
||||||
DWORD rust_task_thread::task_key;
|
DWORD rust_task_thread::task_key;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const size_t SCHED_STACK_SIZE = 1024*100;
|
|
||||||
const size_t C_STACK_SIZE = 1024*1024;
|
const size_t C_STACK_SIZE = 1024*1024;
|
||||||
|
|
||||||
bool rust_task_thread::tls_initialized = false;
|
bool rust_task_thread::tls_initialized = false;
|
||||||
|
@ -21,7 +20,6 @@ bool rust_task_thread::tls_initialized = false;
|
||||||
rust_task_thread::rust_task_thread(rust_scheduler *sched,
|
rust_task_thread::rust_task_thread(rust_scheduler *sched,
|
||||||
rust_srv *srv,
|
rust_srv *srv,
|
||||||
int id) :
|
int id) :
|
||||||
rust_thread(SCHED_STACK_SIZE),
|
|
||||||
_log(srv, this),
|
_log(srv, this),
|
||||||
id(id),
|
id(id),
|
||||||
should_exit(false),
|
should_exit(false),
|
||||||
|
@ -256,6 +254,8 @@ rust_task_thread::start_main_loop() {
|
||||||
destroy_stack(kernel->region(), cached_c_stack);
|
destroy_stack(kernel->region(), cached_c_stack);
|
||||||
cached_c_stack = NULL;
|
cached_c_stack = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sched->release_task_thread();
|
||||||
}
|
}
|
||||||
|
|
||||||
rust_task *
|
rust_task *
|
||||||
|
@ -327,11 +327,6 @@ rust_task_thread::transition(rust_task *task,
|
||||||
lock.signal();
|
lock.signal();
|
||||||
}
|
}
|
||||||
|
|
||||||
void rust_task_thread::run() {
|
|
||||||
this->start_main_loop();
|
|
||||||
sched->release_task_thread();
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
void
|
void
|
||||||
rust_task_thread::init_tls() {
|
rust_task_thread::init_tls() {
|
||||||
|
|
|
@ -2,16 +2,9 @@
|
||||||
#define RUST_TASK_THREAD_H
|
#define RUST_TASK_THREAD_H
|
||||||
|
|
||||||
#include "rust_internal.h"
|
#include "rust_internal.h"
|
||||||
#include "sync/rust_thread.h"
|
|
||||||
#include "rust_stack.h"
|
#include "rust_stack.h"
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
#include <pthread.h>
|
|
||||||
#else
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum rust_task_state {
|
enum rust_task_state {
|
||||||
task_state_newborn,
|
task_state_newborn,
|
||||||
task_state_running,
|
task_state_running,
|
||||||
|
@ -21,8 +14,7 @@ enum rust_task_state {
|
||||||
|
|
||||||
typedef indexed_list<rust_task> rust_task_list;
|
typedef indexed_list<rust_task> rust_task_list;
|
||||||
|
|
||||||
struct rust_task_thread : public kernel_owned<rust_task_thread>,
|
struct rust_task_thread
|
||||||
rust_thread
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -75,6 +67,7 @@ public:
|
||||||
|
|
||||||
randctx rctx;
|
randctx rctx;
|
||||||
|
|
||||||
|
// FIXME: Neither of these are used
|
||||||
int32_t list_index;
|
int32_t list_index;
|
||||||
const char *const name;
|
const char *const name;
|
||||||
|
|
||||||
|
@ -103,8 +96,6 @@ public:
|
||||||
rust_task_state src, rust_task_state dst,
|
rust_task_state src, rust_task_state dst,
|
||||||
rust_cond *cond, const char* cond_name);
|
rust_cond *cond, const char* cond_name);
|
||||||
|
|
||||||
virtual void run();
|
|
||||||
|
|
||||||
void init_tls();
|
void init_tls();
|
||||||
void place_task_in_tls(rust_task *task);
|
void place_task_in_tls(rust_task *task);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue