forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
park.cpp
69 lines (57 loc) · 1.54 KB
/
park.cpp
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
61
62
63
64
65
66
67
68
69
#include "prelude.hpp"
#include "park.hpp"
#include "builtin/thread.hpp"
namespace rubinius {
Object* Park::park(STATE, CallFrame* call_frame) {
utilities::thread::Mutex::LockGuard lg(mutex_);
if(!state->check_async(call_frame)) return NULL;
wake_ = false;
sleeping_ = true;
state->vm()->thread->sleep(state, cTrue);
Object* result = cNil;
while(!wake_) {
{
GCIndependent gc_guard(state, call_frame);
cond_.wait(mutex_);
}
mutex_.unlock();
if(!state->check_async(call_frame)) {
mutex_.lock();
result = NULL;
break;
}
mutex_.lock();
}
sleeping_ = false;
state->vm()->thread->sleep(state, cFalse);
return result;
}
Object* Park::park_timed(STATE, CallFrame* call_frame, struct timespec* ts) {
utilities::thread::Mutex::LockGuard lg(mutex_);
if(!state->check_async(call_frame)) return NULL;
wake_ = false;
sleeping_ = true;
state->vm()->thread->sleep(state, cTrue);
Object* timeout = cFalse;
while(!wake_) {
{
GCIndependent gc_guard(state, call_frame);
utilities::thread::Code status = cond_.wait_until(mutex_, ts);
if(status == utilities::thread::cTimedOut) {
timeout = cTrue;
break;
}
}
mutex_.unlock();
if(!state->check_async(call_frame)) {
mutex_.lock();
timeout = NULL;
break;
}
mutex_.lock();
}
sleeping_ = false;
state->vm()->thread->sleep(state, cFalse);
return timeout;
}
}