Skip to content

Commit 6bf8d19

Browse files
committed
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.
1 parent 620b4d4 commit 6bf8d19

File tree

7 files changed

+71
-37
lines changed

7 files changed

+71
-37
lines changed

mk/rt.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ RUNTIME_CS_$(1) := \
5151
rt/rust_run_program.cpp \
5252
rt/rust_env.cpp \
5353
rt/rust_task_thread.cpp \
54+
rt/rust_sched_launcher.cpp \
5455
rt/rust_scheduler.cpp \
5556
rt/rust_task.cpp \
5657
rt/rust_stack.cpp \

src/rt/rust_sched_launcher.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "rust_sched_launcher.h"
2+
#include "rust_scheduler.h"
3+
4+
const size_t SCHED_STACK_SIZE = 1024*100;
5+
6+
rust_sched_launcher::rust_sched_launcher(rust_scheduler *sched,
7+
rust_srv *srv, int id)
8+
: rust_thread(SCHED_STACK_SIZE),
9+
kernel(sched->kernel),
10+
thread(sched, srv, id) {
11+
}
12+
13+
void
14+
rust_sched_launcher::run() {
15+
thread.start_main_loop();
16+
}

src/rt/rust_sched_launcher.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef RUST_SCHED_LAUNCHER_H
2+
#define RUST_SCHED_LAUNCHER_H
3+
4+
#include "rust_internal.h"
5+
#include "sync/rust_thread.h"
6+
7+
#ifndef _WIN32
8+
#include <pthread.h>
9+
#else
10+
#include <windows.h>
11+
#endif
12+
13+
class rust_sched_launcher
14+
: public kernel_owned<rust_sched_launcher>,
15+
public rust_thread {
16+
public:
17+
rust_kernel *kernel;
18+
19+
private:
20+
rust_task_thread thread;
21+
22+
public:
23+
rust_sched_launcher(rust_scheduler *sched, rust_srv *srv, int id);
24+
25+
virtual void run();
26+
rust_task_thread *get_loop() { return &thread; }
27+
};
28+
29+
#endif // RUST_SCHED_LAUNCHER_H

src/rt/rust_scheduler.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "rust_scheduler.h"
22
#include "rust_util.h"
3+
#include "rust_sched_launcher.h"
34

45
rust_scheduler::rust_scheduler(rust_kernel *kernel,
56
rust_srv *srv,
@@ -21,21 +22,20 @@ rust_scheduler::~rust_scheduler() {
2122
destroy_task_threads();
2223
}
2324

24-
rust_task_thread *
25+
rust_sched_launcher *
2526
rust_scheduler::create_task_thread(int id) {
2627
rust_srv *srv = this->srv->clone();
27-
rust_task_thread *thread =
28-
new (kernel, "rust_task_thread") rust_task_thread(this, srv, id);
29-
KLOG(kernel, kern, "created task thread: " PTR ", id: %d, index: %d",
30-
thread, id, thread->list_index);
28+
rust_sched_launcher *thread =
29+
new (kernel, "rust_sched_launcher") rust_sched_launcher(this, srv, id);
30+
KLOG(kernel, kern, "created task thread: " PTR ", id: %d",
31+
thread, id);
3132
return thread;
3233
}
3334

3435
void
35-
rust_scheduler::destroy_task_thread(rust_task_thread *thread) {
36-
KLOG(kernel, kern, "deleting task thread: " PTR ", name: %s, index: %d",
37-
thread, thread->name, thread->list_index);
38-
rust_srv *srv = thread->srv;
36+
rust_scheduler::destroy_task_thread(rust_sched_launcher *thread) {
37+
KLOG(kernel, kern, "deleting task thread: " PTR, thread);
38+
rust_srv *srv = thread->get_loop()->srv;
3939
delete thread;
4040
delete srv;
4141
}
@@ -60,7 +60,7 @@ void
6060
rust_scheduler::start_task_threads()
6161
{
6262
for(size_t i = 0; i < num_threads; ++i) {
63-
rust_task_thread *thread = threads[i];
63+
rust_sched_launcher *thread = threads[i];
6464
thread->start();
6565
}
6666
}
@@ -69,16 +69,16 @@ void
6969
rust_scheduler::join_task_threads()
7070
{
7171
for(size_t i = 0; i < num_threads; ++i) {
72-
rust_task_thread *thread = threads[i];
72+
rust_sched_launcher *thread = threads[i];
7373
thread->join();
7474
}
7575
}
7676

7777
void
7878
rust_scheduler::kill_all_tasks() {
7979
for(size_t i = 0; i < num_threads; ++i) {
80-
rust_task_thread *thread = threads[i];
81-
thread->kill_all_tasks();
80+
rust_sched_launcher *thread = threads[i];
81+
thread->get_loop()->kill_all_tasks();
8282
}
8383
}
8484

@@ -92,8 +92,8 @@ rust_scheduler::create_task(rust_task *spawner, const char *name) {
9292
if (cur_thread >= num_threads)
9393
cur_thread = 0;
9494
}
95-
rust_task_thread *thread = threads[thread_no];
96-
return thread->create_task(spawner, name);
95+
rust_sched_launcher *thread = threads[thread_no];
96+
return thread->get_loop()->create_task(spawner, name);
9797
}
9898

9999
void
@@ -118,7 +118,7 @@ rust_scheduler::exit() {
118118
// scheduler will get destroyed, and our fields will cease to exist.
119119
size_t current_num_threads = num_threads;
120120
for(size_t i = 0; i < current_num_threads; ++i) {
121-
threads[i]->exit();
121+
threads[i]->get_loop()->exit();
122122
}
123123
}
124124

src/rt/rust_scheduler.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "rust_internal.h"
55

6+
class rust_sched_launcher;
7+
68
class rust_scheduler : public kernel_owned<rust_scheduler> {
79
// FIXME: Make these private
810
public:
@@ -17,7 +19,7 @@ class rust_scheduler : public kernel_owned<rust_scheduler> {
1719
// When this hits zero we'll tell the threads to exit
1820
uintptr_t live_tasks;
1921

20-
array_list<rust_task_thread *> threads;
22+
array_list<rust_sched_launcher *> threads;
2123
const size_t num_threads;
2224
size_t cur_thread;
2325

@@ -26,8 +28,8 @@ class rust_scheduler : public kernel_owned<rust_scheduler> {
2628
void create_task_threads();
2729
void destroy_task_threads();
2830

29-
rust_task_thread *create_task_thread(int id);
30-
void destroy_task_thread(rust_task_thread *thread);
31+
rust_sched_launcher *create_task_thread(int id);
32+
void destroy_task_thread(rust_sched_launcher *thread);
3133

3234
void exit();
3335

src/rt/rust_task_thread.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ pthread_key_t rust_task_thread::task_key;
1313
DWORD rust_task_thread::task_key;
1414
#endif
1515

16-
const size_t SCHED_STACK_SIZE = 1024*100;
1716
const size_t C_STACK_SIZE = 1024*1024;
1817

1918
bool rust_task_thread::tls_initialized = false;
2019

2120
rust_task_thread::rust_task_thread(rust_scheduler *sched,
2221
rust_srv *srv,
2322
int id) :
24-
rust_thread(SCHED_STACK_SIZE),
2523
_log(srv, this),
2624
id(id),
2725
should_exit(false),
@@ -256,6 +254,8 @@ rust_task_thread::start_main_loop() {
256254
destroy_stack(kernel->region(), cached_c_stack);
257255
cached_c_stack = NULL;
258256
}
257+
258+
sched->release_task_thread();
259259
}
260260

261261
rust_task *
@@ -327,11 +327,6 @@ rust_task_thread::transition(rust_task *task,
327327
lock.signal();
328328
}
329329

330-
void rust_task_thread::run() {
331-
this->start_main_loop();
332-
sched->release_task_thread();
333-
}
334-
335330
#ifndef _WIN32
336331
void
337332
rust_task_thread::init_tls() {

src/rt/rust_task_thread.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
#define RUST_TASK_THREAD_H
33

44
#include "rust_internal.h"
5-
#include "sync/rust_thread.h"
65
#include "rust_stack.h"
76
#include "context.h"
87

9-
#ifndef _WIN32
10-
#include <pthread.h>
11-
#else
12-
#include <windows.h>
13-
#endif
14-
158
enum rust_task_state {
169
task_state_newborn,
1710
task_state_running,
@@ -21,8 +14,7 @@ enum rust_task_state {
2114

2215
typedef indexed_list<rust_task> rust_task_list;
2316

24-
struct rust_task_thread : public kernel_owned<rust_task_thread>,
25-
rust_thread
17+
struct rust_task_thread
2618
{
2719
private:
2820

@@ -75,6 +67,7 @@ struct rust_task_thread : public kernel_owned<rust_task_thread>,
7567

7668
randctx rctx;
7769

70+
// FIXME: Neither of these are used
7871
int32_t list_index;
7972
const char *const name;
8073

@@ -103,8 +96,6 @@ struct rust_task_thread : public kernel_owned<rust_task_thread>,
10396
rust_task_state src, rust_task_state dst,
10497
rust_cond *cond, const char* cond_name);
10598

106-
virtual void run();
107-
10899
void init_tls();
109100
void place_task_in_tls(rust_task *task);
110101

0 commit comments

Comments
 (0)