rt: Move rust_thread to its own files
This commit is contained in:
2
mk/rt.mk
2
mk/rt.mk
@@ -37,6 +37,7 @@ RUNTIME_CS_$(1) := \
|
|||||||
rt/sync/timer.cpp \
|
rt/sync/timer.cpp \
|
||||||
rt/sync/sync.cpp \
|
rt/sync/sync.cpp \
|
||||||
rt/sync/lock_and_signal.cpp \
|
rt/sync/lock_and_signal.cpp \
|
||||||
|
rt/sync/rust_thread.cpp \
|
||||||
rt/rust.cpp \
|
rt/rust.cpp \
|
||||||
rt/rust_builtin.cpp \
|
rt/rust_builtin.cpp \
|
||||||
rt/rust_run_program.cpp \
|
rt/rust_run_program.cpp \
|
||||||
@@ -97,6 +98,7 @@ RUNTIME_HDR_$(1) := rt/globals.h \
|
|||||||
rt/sync/timer.h \
|
rt/sync/timer.h \
|
||||||
rt/sync/lock_and_signal.h \
|
rt/sync/lock_and_signal.h \
|
||||||
rt/sync/lock_free_queue.h \
|
rt/sync/lock_free_queue.h \
|
||||||
|
rt/sync/rust_thread.h \
|
||||||
rt/rust_srv.h \
|
rt/rust_srv.h \
|
||||||
rt/rust_kernel.h \
|
rt/rust_kernel.h \
|
||||||
rt/memory_region.h \
|
rt/memory_region.h \
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef RUST_TASK_THREAD_H
|
#ifndef RUST_TASK_THREAD_H
|
||||||
#define RUST_TASK_THREAD_H
|
#define RUST_TASK_THREAD_H
|
||||||
|
|
||||||
|
#include "sync/rust_thread.h"
|
||||||
#include "rust_stack.h"
|
#include "rust_stack.h"
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
|
|
||||||
|
|||||||
43
src/rt/sync/rust_thread.cpp
Normal file
43
src/rt/sync/rust_thread.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#include "globals.h"
|
||||||
|
#include "rust_thread.h"
|
||||||
|
|
||||||
|
rust_thread::rust_thread() : thread(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__WIN32__)
|
||||||
|
static DWORD WINAPI
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
static void *
|
||||||
|
#else
|
||||||
|
#error "Platform not supported"
|
||||||
|
#endif
|
||||||
|
rust_thread_start(void *ptr) {
|
||||||
|
rust_thread *thread = (rust_thread *) ptr;
|
||||||
|
thread->run();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rust_thread::start() {
|
||||||
|
#if defined(__WIN32__)
|
||||||
|
thread = CreateThread(NULL, 0, rust_thread_start, this, 0, NULL);
|
||||||
|
#else
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setstacksize(&attr, 1024 * 1024);
|
||||||
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||||
|
pthread_create(&thread, &attr, rust_thread_start, (void *) this);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rust_thread::join() {
|
||||||
|
#if defined(__WIN32__)
|
||||||
|
if (thread)
|
||||||
|
WaitForSingleObject(thread, INFINITE);
|
||||||
|
#else
|
||||||
|
if (thread)
|
||||||
|
pthread_join(thread, NULL);
|
||||||
|
#endif
|
||||||
|
thread = 0;
|
||||||
|
}
|
||||||
26
src/rt/sync/rust_thread.h
Normal file
26
src/rt/sync/rust_thread.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef RUST_THREAD_H
|
||||||
|
#define RUST_THREAD_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread utility class. Derive and implement your own run() method.
|
||||||
|
*/
|
||||||
|
class rust_thread {
|
||||||
|
public:
|
||||||
|
#if defined(__WIN32__)
|
||||||
|
HANDLE thread;
|
||||||
|
#else
|
||||||
|
pthread_t thread;
|
||||||
|
#endif
|
||||||
|
rust_thread();
|
||||||
|
void start();
|
||||||
|
|
||||||
|
virtual void run() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void join();
|
||||||
|
|
||||||
|
virtual ~rust_thread() {} // quiet the compiler
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* RUST_THREAD_H */
|
||||||
@@ -18,44 +18,3 @@ void sync::sleep(size_t timeout_in_ms) {
|
|||||||
usleep(timeout_in_ms * 1000);
|
usleep(timeout_in_ms * 1000);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
rust_thread::rust_thread() : thread(0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__WIN32__)
|
|
||||||
static DWORD WINAPI
|
|
||||||
#elif defined(__GNUC__)
|
|
||||||
static void *
|
|
||||||
#else
|
|
||||||
#error "Platform not supported"
|
|
||||||
#endif
|
|
||||||
rust_thread_start(void *ptr) {
|
|
||||||
rust_thread *thread = (rust_thread *) ptr;
|
|
||||||
thread->run();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
rust_thread::start() {
|
|
||||||
#if defined(__WIN32__)
|
|
||||||
thread = CreateThread(NULL, 0, rust_thread_start, this, 0, NULL);
|
|
||||||
#else
|
|
||||||
pthread_attr_t attr;
|
|
||||||
pthread_attr_init(&attr);
|
|
||||||
pthread_attr_setstacksize(&attr, 1024 * 1024);
|
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|
||||||
pthread_create(&thread, &attr, rust_thread_start, (void *) this);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
rust_thread::join() {
|
|
||||||
#if defined(__WIN32__)
|
|
||||||
if (thread)
|
|
||||||
WaitForSingleObject(thread, INFINITE);
|
|
||||||
#else
|
|
||||||
if (thread)
|
|
||||||
pthread_join(thread, NULL);
|
|
||||||
#endif
|
|
||||||
thread = 0;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -33,26 +33,4 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Thread utility class. Derive and implement your own run() method.
|
|
||||||
*/
|
|
||||||
class rust_thread {
|
|
||||||
public:
|
|
||||||
#if defined(__WIN32__)
|
|
||||||
HANDLE thread;
|
|
||||||
#else
|
|
||||||
pthread_t thread;
|
|
||||||
#endif
|
|
||||||
rust_thread();
|
|
||||||
void start();
|
|
||||||
|
|
||||||
virtual void run() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void join();
|
|
||||||
|
|
||||||
virtual ~rust_thread() {} // quiet the compiler
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif /* SYNC_H */
|
#endif /* SYNC_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user