Skip to content

Commit 421c8db

Browse files
committed
rt: Move rust_thread to its own files
1 parent 8ad9cf8 commit 421c8db

File tree

6 files changed

+72
-63
lines changed

6 files changed

+72
-63
lines changed

mk/rt.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ RUNTIME_CS_$(1) := \
3737
rt/sync/timer.cpp \
3838
rt/sync/sync.cpp \
3939
rt/sync/lock_and_signal.cpp \
40+
rt/sync/rust_thread.cpp \
4041
rt/rust.cpp \
4142
rt/rust_builtin.cpp \
4243
rt/rust_run_program.cpp \
@@ -97,6 +98,7 @@ RUNTIME_HDR_$(1) := rt/globals.h \
9798
rt/sync/timer.h \
9899
rt/sync/lock_and_signal.h \
99100
rt/sync/lock_free_queue.h \
101+
rt/sync/rust_thread.h \
100102
rt/rust_srv.h \
101103
rt/rust_kernel.h \
102104
rt/memory_region.h \

src/rt/rust_task_thread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef RUST_TASK_THREAD_H
22
#define RUST_TASK_THREAD_H
33

4+
#include "sync/rust_thread.h"
45
#include "rust_stack.h"
56
#include "context.h"
67

src/rt/sync/rust_thread.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "globals.h"
2+
#include "rust_thread.h"
3+
4+
rust_thread::rust_thread() : thread(0) {
5+
}
6+
7+
#if defined(__WIN32__)
8+
static DWORD WINAPI
9+
#elif defined(__GNUC__)
10+
static void *
11+
#else
12+
#error "Platform not supported"
13+
#endif
14+
rust_thread_start(void *ptr) {
15+
rust_thread *thread = (rust_thread *) ptr;
16+
thread->run();
17+
return 0;
18+
}
19+
20+
void
21+
rust_thread::start() {
22+
#if defined(__WIN32__)
23+
thread = CreateThread(NULL, 0, rust_thread_start, this, 0, NULL);
24+
#else
25+
pthread_attr_t attr;
26+
pthread_attr_init(&attr);
27+
pthread_attr_setstacksize(&attr, 1024 * 1024);
28+
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
29+
pthread_create(&thread, &attr, rust_thread_start, (void *) this);
30+
#endif
31+
}
32+
33+
void
34+
rust_thread::join() {
35+
#if defined(__WIN32__)
36+
if (thread)
37+
WaitForSingleObject(thread, INFINITE);
38+
#else
39+
if (thread)
40+
pthread_join(thread, NULL);
41+
#endif
42+
thread = 0;
43+
}

src/rt/sync/rust_thread.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef RUST_THREAD_H
2+
#define RUST_THREAD_H
3+
4+
/**
5+
* Thread utility class. Derive and implement your own run() method.
6+
*/
7+
class rust_thread {
8+
public:
9+
#if defined(__WIN32__)
10+
HANDLE thread;
11+
#else
12+
pthread_t thread;
13+
#endif
14+
rust_thread();
15+
void start();
16+
17+
virtual void run() {
18+
return;
19+
}
20+
21+
void join();
22+
23+
virtual ~rust_thread() {} // quiet the compiler
24+
};
25+
26+
#endif /* RUST_THREAD_H */

src/rt/sync/sync.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,3 @@ void sync::sleep(size_t timeout_in_ms) {
1818
usleep(timeout_in_ms * 1000);
1919
#endif
2020
}
21-
22-
rust_thread::rust_thread() : thread(0) {
23-
}
24-
25-
#if defined(__WIN32__)
26-
static DWORD WINAPI
27-
#elif defined(__GNUC__)
28-
static void *
29-
#else
30-
#error "Platform not supported"
31-
#endif
32-
rust_thread_start(void *ptr) {
33-
rust_thread *thread = (rust_thread *) ptr;
34-
thread->run();
35-
return 0;
36-
}
37-
38-
void
39-
rust_thread::start() {
40-
#if defined(__WIN32__)
41-
thread = CreateThread(NULL, 0, rust_thread_start, this, 0, NULL);
42-
#else
43-
pthread_attr_t attr;
44-
pthread_attr_init(&attr);
45-
pthread_attr_setstacksize(&attr, 1024 * 1024);
46-
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
47-
pthread_create(&thread, &attr, rust_thread_start, (void *) this);
48-
#endif
49-
}
50-
51-
void
52-
rust_thread::join() {
53-
#if defined(__WIN32__)
54-
if (thread)
55-
WaitForSingleObject(thread, INFINITE);
56-
#else
57-
if (thread)
58-
pthread_join(thread, NULL);
59-
#endif
60-
thread = 0;
61-
}

src/rt/sync/sync.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,4 @@ class sync {
3333
}
3434
};
3535

36-
/**
37-
* Thread utility class. Derive and implement your own run() method.
38-
*/
39-
class rust_thread {
40-
public:
41-
#if defined(__WIN32__)
42-
HANDLE thread;
43-
#else
44-
pthread_t thread;
45-
#endif
46-
rust_thread();
47-
void start();
48-
49-
virtual void run() {
50-
return;
51-
}
52-
53-
void join();
54-
55-
virtual ~rust_thread() {} // quiet the compiler
56-
};
57-
5836
#endif /* SYNC_H */

0 commit comments

Comments
 (0)