about summary refs log tree commit diff
path: root/src/rt/sync/sync.cpp
blob: fdfc065249dd50ceb00154a9a8f7973e90e390d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "../globals.h"
#include "sync.h"

void sync::yield() {
#ifdef __APPLE__
    pthread_yield_np();
#elif __WIN32__
    Sleep(1);
#else
    pthread_yield();
#endif
}

rust_thread::rust_thread() : _is_running(false) {
    // Nop.
}

#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();
    thread->thread = 0;
    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
   _is_running = true;
}

void
rust_thread::join() {
#if defined(__WIN32__)
   WaitForSingleObject(thread, INFINITE);
#else
   pthread_join(thread, NULL);
#endif
   thread = 0;
   _is_running = false;
}

bool
rust_thread::is_running() {
    return _is_running;
}