OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_FUTEX_EMULATION_H_ | 5 #ifndef V8_FUTEX_EMULATION_H_ |
6 #define V8_FUTEX_EMULATION_H_ | 6 #define V8_FUTEX_EMULATION_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
11 #include "src/base/atomicops.h" | 11 #include "src/base/atomicops.h" |
12 #include "src/base/lazy-instance.h" | 12 #include "src/base/lazy-instance.h" |
13 #include "src/base/macros.h" | 13 #include "src/base/macros.h" |
14 #include "src/base/platform/condition-variable.h" | 14 #include "src/base/platform/condition-variable.h" |
15 #include "src/base/platform/mutex.h" | 15 #include "src/base/platform/mutex.h" |
16 #include "src/handles.h" | 16 #include "src/handles.h" |
17 | 17 |
18 // Support for emulating futexes, a low-level synchronization primitive. They | 18 // Support for emulating futexes, a low-level synchronization primitive. They |
19 // are natively supported by Linux, but must be emulated for other platforms. | 19 // are natively supported by Linux, but must be emulated for other platforms. |
20 // This library emulates them on all platforms using mutexes and condition | 20 // This library emulates them on all platforms using mutexes and condition |
21 // variables for consistency. | 21 // variables for consistency. |
22 // | 22 // |
23 // This is used by the Futex API defined in the SharedArrayBuffer draft spec, | 23 // This is used by the Futex API defined in the SharedArrayBuffer draft spec, |
24 // found here: https://github.com/lars-t-hansen/ecmascript_sharedmem | 24 // found here: https://github.com/tc39/ecmascript_sharedmem |
25 | 25 |
26 namespace v8 { | 26 namespace v8 { |
27 | 27 |
28 namespace base { | 28 namespace base { |
29 class TimeDelta; | 29 class TimeDelta; |
30 } // base | 30 } // base |
31 | 31 |
32 namespace internal { | 32 namespace internal { |
33 | 33 |
34 class Isolate; | 34 class Isolate; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 FutexWaitListNode* head_; | 75 FutexWaitListNode* head_; |
76 FutexWaitListNode* tail_; | 76 FutexWaitListNode* tail_; |
77 | 77 |
78 DISALLOW_COPY_AND_ASSIGN(FutexWaitList); | 78 DISALLOW_COPY_AND_ASSIGN(FutexWaitList); |
79 }; | 79 }; |
80 | 80 |
81 | 81 |
82 class FutexEmulation : public AllStatic { | 82 class FutexEmulation : public AllStatic { |
83 public: | 83 public: |
84 // These must match the values in src/harmony-atomics.js | 84 // Check that array_buffer[addr] == value, and return "not-equal" if not. If |
85 enum Result { | |
86 kOk = 0, | |
87 kNotEqual = -1, | |
88 kTimedOut = -2, | |
89 }; | |
90 | |
91 // Check that array_buffer[addr] == value, and return kNotEqual if not. If | |
92 // they are equal, block execution on |isolate|'s thread until woken via | 85 // they are equal, block execution on |isolate|'s thread until woken via |
93 // |Wake|, or when the time given in |rel_timeout_ms| elapses. Note that | 86 // |Wake|, or when the time given in |rel_timeout_ms| elapses. Note that |
94 // |rel_timeout_ms| can be Infinity. | 87 // |rel_timeout_ms| can be Infinity. |
95 // If woken, return kOk, otherwise return kTimedOut. The initial check and | 88 // If woken, return "ok", otherwise return "timed-out". The initial check and |
96 // the decision to wait happen atomically. | 89 // the decision to wait happen atomically. |
97 static Object* Wait(Isolate* isolate, Handle<JSArrayBuffer> array_buffer, | 90 static Object* Wait(Isolate* isolate, Handle<JSArrayBuffer> array_buffer, |
98 size_t addr, int32_t value, double rel_timeout_ms); | 91 size_t addr, int32_t value, double rel_timeout_ms); |
99 | 92 |
100 // Wake |num_waiters_to_wake| threads that are waiting on the given |addr|. | 93 // Wake |num_waiters_to_wake| threads that are waiting on the given |addr|. |
101 // The rest of the waiters will continue to wait. The return value is the | 94 // The rest of the waiters will continue to wait. The return value is the |
102 // number of woken waiters. | 95 // number of woken waiters. |
103 static Object* Wake(Isolate* isolate, Handle<JSArrayBuffer> array_buffer, | 96 static Object* Wake(Isolate* isolate, Handle<JSArrayBuffer> array_buffer, |
104 size_t addr, int num_waiters_to_wake); | 97 size_t addr, int num_waiters_to_wake); |
105 | 98 |
106 // Check that array_buffer[addr] == value, and return kNotEqual if not. If | |
107 // they are equal, wake |num_waiters_to_wake| threads that are waiting on the | |
108 // given |addr|. The rest of the waiters will continue to wait, but will now | |
109 // be waiting on |addr2| instead of |addr|. The return value is the number of | |
110 // woken waiters or kNotEqual as described above. | |
111 static Object* WakeOrRequeue(Isolate* isolate, | |
112 Handle<JSArrayBuffer> array_buffer, size_t addr, | |
113 int num_waiters_to_wake, int32_t value, | |
114 size_t addr2); | |
115 | |
116 // Return the number of threads waiting on |addr|. Should only be used for | 99 // Return the number of threads waiting on |addr|. Should only be used for |
117 // testing. | 100 // testing. |
118 static Object* NumWaitersForTesting(Isolate* isolate, | 101 static Object* NumWaitersForTesting(Isolate* isolate, |
119 Handle<JSArrayBuffer> array_buffer, | 102 Handle<JSArrayBuffer> array_buffer, |
120 size_t addr); | 103 size_t addr); |
121 | 104 |
122 private: | 105 private: |
123 friend class FutexWaitListNode; | 106 friend class FutexWaitListNode; |
124 | 107 |
125 static base::LazyMutex mutex_; | 108 static base::LazyMutex mutex_; |
126 static base::LazyInstance<FutexWaitList>::type wait_list_; | 109 static base::LazyInstance<FutexWaitList>::type wait_list_; |
127 }; | 110 }; |
128 } // namespace internal | 111 } // namespace internal |
129 } // namespace v8 | 112 } // namespace v8 |
130 | 113 |
131 #endif // V8_FUTEX_EMULATION_H_ | 114 #endif // V8_FUTEX_EMULATION_H_ |
OLD | NEW |