forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_pthread_stub.js
329 lines (280 loc) · 11.9 KB
/
library_pthread_stub.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* @license
* Copyright 2015 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
var LibraryPThreadStub = {
// ===================================================================================
// Stub implementation for pthread.h when not compiling with pthreads support enabled.
// ===================================================================================
emscripten_is_main_browser_thread: function() {
#if MINIMAL_RUNTIME
return typeof importScripts === 'undefined';
#else
return !ENVIRONMENT_IS_WORKER;
#endif
},
pthread_mutexattr_init: function() {},
pthread_mutexattr_setschedparam: function() {},
pthread_mutexattr_setprotocol: function() {},
pthread_mutexattr_settype: function() {},
pthread_mutexattr_destroy: function() {},
pthread_mutexattr_setpshared: function(attr, pshared) {
// XXX implement if/when getpshared is required
return 0;
},
pthread_cond_init: function() { return 0; },
pthread_cond_destroy: function() { return 0; },
pthread_cond_timedwait: function() { return 0; },
pthread_condattr_init: function() { return 0; },
pthread_condattr_destroy: function() { return 0; },
pthread_condattr_setclock: function() { return 0; },
pthread_condattr_setpshared: function() { return 0; },
pthread_condattr_getclock: function() { return 0; },
pthread_condattr_getpshared: function() { return 0; },
pthread_attr_init: function(attr) {
/* int pthread_attr_init(pthread_attr_t *attr); */
//FIXME: should allocate a pthread_attr_t
return 0;
},
pthread_getattr_np: function(thread, attr) {
/* int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr); */
//FIXME: should fill in attributes of the given thread in pthread_attr_t
return 0;
},
pthread_attr_destroy: function(attr) {
/* int pthread_attr_destroy(pthread_attr_t *attr); */
//FIXME: should destroy the pthread_attr_t struct
return 0;
},
pthread_attr_getstack: function(attr, stackaddr, stacksize) {
/* int pthread_attr_getstack(const pthread_attr_t *restrict attr,
void **restrict stackaddr, size_t *restrict stacksize); */
/*FIXME: assumes that there is only one thread, and that attr is the
current thread*/
{{{ makeSetValue('stackaddr', '0', '_emscripten_stack_get_base()', 'i8*') }}};
{{{ makeSetValue('stacksize', '0', TOTAL_STACK, 'i32') }}};
return 0;
},
pthread_attr_getdetachstate: function(attr, detachstate) {
/* int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate); */
return 0;
},
pthread_setcancelstate: function() { return 0; },
pthread_setcanceltype: function() { return 0; },
pthread_cleanup_push__sig: 'vii',
pthread_cleanup_push: function(routine, arg) {
__ATEXIT__.push({ func: routine, arg: arg });
_pthread_cleanup_push.level = __ATEXIT__.length;
},
pthread_cleanup_pop__sig: 'vi',
pthread_cleanup_pop: function(execute) {
assert(_pthread_cleanup_push.level == __ATEXIT__.length, 'cannot pop if something else added meanwhile!');
callback = __ATEXIT__.pop();
if (execute) {
{{{ makeDynCall('vi', 'callback.func') }}}(callback.arg)
}
_pthread_cleanup_push.level = __ATEXIT__.length;
},
// pthread_sigmask - examine and change mask of blocked signals
pthread_sigmask: function(how, set, oldset) {
err('pthread_sigmask() is not supported: this is a no-op.');
return 0;
},
pthread_rwlock_init: function() { return 0; },
pthread_rwlock_destroy: function() { return 0; },
pthread_rwlock_rdlock: function() { return 0; },
pthread_rwlock_tryrdlock: function() { return 0; },
pthread_rwlock_timedrdlock: function() { return 0; },
pthread_rwlock_wrlock: function() { return 0; },
pthread_rwlock_trywrlock: function() { return 0; },
pthread_rwlock_timedwrlock: function() { return 0; },
pthread_rwlock_unlock: function() { return 0; },
pthread_rwlockattr_init: function() { return 0; },
pthread_rwlockattr_destroy: function() { return 0; },
pthread_rwlockattr_setpshared: function() { return 0; },
pthread_rwlockattr_getpshared: function() { return 0; },
pthread_spin_init: function() { return 0; },
pthread_spin_destroy: function() { return 0; },
pthread_spin_lock: function() { return 0; },
pthread_spin_trylock: function() { return 0; },
pthread_spin_unlock: function() { return 0; },
pthread_attr_setdetachstate: function() {},
pthread_attr_setschedparam: function() {},
pthread_attr_setstacksize: function() {},
{{{ USE_LSAN || USE_ASAN ? 'emscripten_builtin_' : '' }}}pthread_create: function() {
return {{{ cDefine('EAGAIN') }}};
},
pthread_cancel: function() {},
pthread_exit__deps: ['exit'],
pthread_exit: function(status) {
_exit(status);
},
pthread_equal: function(x, y) { return x == y },
{{{ USE_LSAN ? 'emscripten_builtin_' : '' }}}pthread_join: function() {},
pthread_detach: function() {},
sem_init: function() {},
sem_post: function() {},
sem_wait: function() {},
sem_trywait: function() {},
sem_destroy: function() {},
emscripten_main_browser_thread_id__deps: ['pthread_self'],
emscripten_main_browser_thread_id: function() { return _pthread_self(); },
// When pthreads is not enabled, we can't use the Atomics futex api to do proper sleeps, so simulate a busy spin wait loop instead.
usleep__deps: ['emscripten_get_now'],
usleep: function(useconds) {
// int usleep(useconds_t useconds);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/usleep.html
// We're single-threaded, so use a busy loop. Super-ugly.
var start = _emscripten_get_now();
while (_emscripten_get_now() - start < useconds / 1000) {
// Do nothing.
}
},
nanosleep__deps: ['usleep', '$setErrNo'],
nanosleep: function(rqtp, rmtp) {
// int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
if (rqtp === 0) {
setErrNo({{{ cDefine('EINVAL') }}});
return -1;
}
var seconds = {{{ makeGetValue('rqtp', C_STRUCTS.timespec.tv_sec, 'i32') }}};
var nanoseconds = {{{ makeGetValue('rqtp', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
if (nanoseconds < 0 || nanoseconds > 999999999 || seconds < 0) {
setErrNo({{{ cDefine('EINVAL') }}});
return -1;
}
if (rmtp !== 0) {
{{{ makeSetValue('rmtp', C_STRUCTS.timespec.tv_sec, '0', 'i32') }}};
{{{ makeSetValue('rmtp', C_STRUCTS.timespec.tv_nsec, '0', 'i32') }}};
}
return _usleep((seconds * 1e6) + (nanoseconds / 1000));
},
llvm_memory_barrier: function(){},
llvm_atomic_load_add_i32_p0i32: function(ptr, delta) {
var ret = {{{ makeGetValue('ptr', '0', 'i32') }}};
{{{ makeSetValue('ptr', '0', 'ret+delta', 'i32') }}};
return ret;
},
// TODO(sbc): Remove these helpers and move them and thier callers into
// native code instream.
i64Add__asm: true,
i64Add__sig: 'iiiii',
i64Add: function(a, b, c, d) {
/*
x = a + b*2^32
y = c + d*2^32
result = l + h*2^32
*/
a = a|0; b = b|0; c = c|0; d = d|0;
var l = 0, h = 0;
l = (a + c)>>>0;
h = (b + d + (((l>>>0) < (a>>>0))|0))>>>0; // Add carry from low word to high word on overflow.
{{{ makeStructuralReturn(['l|0', 'h']) }}};
},
i64Subtract__asm: true,
i64Subtract__sig: 'iiiii',
i64Subtract: function(a, b, c, d) {
a = a|0; b = b|0; c = c|0; d = d|0;
var l = 0, h = 0;
l = (a - c)>>>0;
h = (b - d)>>>0;
h = (b - d - (((c>>>0) > (a>>>0))|0))>>>0; // Borrow one from high word to low word on underflow.
{{{ makeStructuralReturn(['l|0', 'h']) }}};
},
// gnu atomics
__atomic_is_lock_free: function(size, ptr) {
return size <= 4 && (size & (size-1)) == 0 && (ptr&(size-1)) == 0;
},
__atomic_load_8: function(ptr, memmodel) {
{{{ makeStructuralReturn([makeGetValue('ptr', 0, 'i32'), makeGetValue('ptr', 4, 'i32')]) }}};
},
__atomic_store_8: function(ptr, vall, valh, memmodel) {
{{{ makeSetValue('ptr', 0, 'vall', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'valh', 'i32') }}};
},
__atomic_exchange_8: function(ptr, vall, valh, memmodel) {
var l = {{{ makeGetValue('ptr', 0, 'i32') }}};
var h = {{{ makeGetValue('ptr', 4, 'i32') }}};
{{{ makeSetValue('ptr', 0, 'vall', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'valh', 'i32') }}};
{{{ makeStructuralReturn(['l', 'h']) }}};
},
__atomic_compare_exchange_8: function(ptr, expected, desiredl, desiredh, weak, success_memmodel, failure_memmodel) {
var pl = {{{ makeGetValue('ptr', 0, 'i32') }}};
var ph = {{{ makeGetValue('ptr', 4, 'i32') }}};
var el = {{{ makeGetValue('expected', 0, 'i32') }}};
var eh = {{{ makeGetValue('expected', 4, 'i32') }}};
if (pl === el && ph === eh) {
{{{ makeSetValue('ptr', 0, 'desiredl', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'desiredh', 'i32') }}};
return 1;
} else {
{{{ makeSetValue('expected', 0, 'pl', 'i32') }}};
{{{ makeSetValue('expected', 4, 'ph', 'i32') }}};
return 0;
}
},
__atomic_fetch_add_8__deps: ['i64Add'],
__atomic_fetch_add_8: function(ptr, vall, valh, memmodel) {
var l = {{{ makeGetValue('ptr', 0, 'i32') }}};
var h = {{{ makeGetValue('ptr', 4, 'i32') }}};
{{{ makeSetValue('ptr', 0, '_i64Add(l, h, vall, valh)', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'getTempRet0()', 'i32') }}};
{{{ makeStructuralReturn(['l', 'h']) }}};
},
__atomic_fetch_sub_8__deps: ['i64Subtract'],
__atomic_fetch_sub_8: function(ptr, vall, valh, memmodel) {
var l = {{{ makeGetValue('ptr', 0, 'i32') }}};
var h = {{{ makeGetValue('ptr', 4, 'i32') }}};
{{{ makeSetValue('ptr', 0, '_i64Subtract(l, h, vall, valh)', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'getTempRet0()', 'i32') }}};
{{{ makeStructuralReturn(['l', 'h']) }}};
},
__atomic_fetch_and_8__deps: ['i64Subtract'],
__atomic_fetch_and_8: function(ptr, vall, valh, memmodel) {
var l = {{{ makeGetValue('ptr', 0, 'i32') }}};
var h = {{{ makeGetValue('ptr', 4, 'i32') }}};
{{{ makeSetValue('ptr', 0, 'l&vall', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'h&valh', 'i32') }}};
{{{ makeStructuralReturn(['l', 'h']) }}};
},
__atomic_fetch_or_8: function(ptr, vall, valh, memmodel) {
var l = {{{ makeGetValue('ptr', 0, 'i32') }}};
var h = {{{ makeGetValue('ptr', 4, 'i32') }}};
{{{ makeSetValue('ptr', 0, 'l|vall', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'h|valh', 'i32') }}};
{{{ makeStructuralReturn(['l', 'h']) }}};
},
__atomic_fetch_xor_8: function(ptr, vall, valh, memmodel) {
var l = {{{ makeGetValue('ptr', 0, 'i32') }}};
var h = {{{ makeGetValue('ptr', 4, 'i32') }}};
{{{ makeSetValue('ptr', 0, 'l^vall', 'i32') }}};
{{{ makeSetValue('ptr', 4, 'h^valh', 'i32') }}};
{{{ makeStructuralReturn(['l', 'h']) }}};
},
emscripten_atomic_add_u32__sig: 'iii',
emscripten_atomic_add_u32: 'llvm_atomic_load_add_i32_p0i32',
emscripten_atomic_load_u64__sig: 'iii',
emscripten_atomic_load_u64: '__atomic_load_8',
emscripten_atomic_store_u64__sig: 'viiii',
emscripten_atomic_store_u64: '__atomic_store_8',
emscripten_atomic_cas_u64__sig: 'iiiiiiii',
emscripten_atomic_cas_u64: '__atomic_compare_exchange_8',
emscripten_atomic_exchange_u64__sig: 'iiiii',
emscripten_atomic_exchange_u64: '__atomic_exchange_8',
_emscripten_atomic_fetch_and_add_u64__sig: 'iiiii',
_emscripten_atomic_fetch_and_add_u64: '__atomic_fetch_add_8',
_emscripten_atomic_fetch_and_add_u64__sig: 'iiiii',
_emscripten_atomic_fetch_and_add_u64: '__atomic_fetch_add_8',
_emscripten_atomic_fetch_and_sub_u64__sig: 'iiiii',
_emscripten_atomic_fetch_and_sub_u64: '__atomic_fetch_sub_8',
_emscripten_atomic_fetch_and_and_u64__sig: 'iiiii',
_emscripten_atomic_fetch_and_and_u64: '__atomic_fetch_and_8',
_emscripten_atomic_fetch_and_or_u64__sig: 'iiiii',
_emscripten_atomic_fetch_and_or_u64: '__atomic_fetch_or_8',
_emscripten_atomic_fetch_and_xor_u64__sig: 'iiiii',
_emscripten_atomic_fetch_and_xor_u64: '__atomic_fetch_xor_8',
__wait: function() {},
};
mergeInto(LibraryManager.library, LibraryPThreadStub);