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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
mod os {
#[cold]
#[inline(never)]
fn unlikely<T>(result: T) -> T {
result
}
#[cfg(any(
all(unix, not(any(target_os = "macos", target_os = "ios"))),
target_os = "fuchsia"
))]
pub mod posix {
use super::unlikely;
use core::cell::UnsafeCell;
use core::mem;
use core::sync::atomic::{AtomicU8, Ordering};
use error_code::PosixError;
const UNINIT: u8 = 0;
const INITING: u8 = 0b01;
const INITED: u8 = 0b10;
///POSIX implementation of Semaphore
pub struct Sem {
handle: UnsafeCell<mem::MaybeUninit<libc::sem_t>>,
state: AtomicU8,
}
impl Sem {
///Creates new uninit instance.
///
///It is UB to use it until `init` is called.
pub const unsafe fn new_uninit() -> Self {
Self {
handle: UnsafeCell::new(mem::MaybeUninit::uninit()),
state: AtomicU8::new(UNINIT),
}
}
#[inline(always)]
///Returns whether semaphore is successfully initialized
pub fn is_init(&self) -> bool {
self.state.load(Ordering::Acquire) == INITED
}
#[cold]
#[inline(never)]
fn await_init(&self) {
//Wait for initialization to finish
while self.state.load(Ordering::Acquire) == INITING {
core::hint::spin_loop();
}
}
#[must_use]
///Initializes semaphore with provided `init` as initial value.
///
///Returns `true` on success.
///
///Returns `false` if semaphore is already initialized or initialization failed.
pub fn init(&self, init: u32) -> bool {
if let Ok(UNINIT) = self.state.compare_exchange(
UNINIT,
INITING,
Ordering::SeqCst,
Ordering::Acquire,
) {
let res = unsafe { libc::sem_init(self.handle.get() as _, 0, init as _) };
let res = match res {
0 => {
self.state.store(INITED, Ordering::Release);
true
}
_ => {
//TODO: assert against?
self.state.store(UNINIT, Ordering::Release);
false
}
};
unlikely(res)
} else {
//Similarly to `Once` we give priority to already-init path
//although we do need to make sure it is finished
if self.state.load(Ordering::Acquire) != INITED {
self.await_init();
}
false
}
}
///Creates new instance, initializing it with `init`
pub fn new(init: u32) -> Option<Self> {
let result = unsafe { Self::new_uninit() };
if result.init(init) {
Some(result)
} else {
unlikely(None)
}
}
///Decrements self, returning immediately if it was signaled.
///
///Otherwise awaits for signal.
pub fn wait(&self) {
loop {
let res = unsafe { libc::sem_wait(mem::transmute(self.handle.get())) };
if res == -1 {
let errno = PosixError::last();
debug_assert_eq!(errno.raw_code(), libc::EINTR, "Unexpected error");
continue;
}
break;
}
}
#[inline]
///Attempts to decrement self, returning whether self was signaled or not.
///
///Returns `true` if self was signaled.
///
///Returns `false` otherwise.
pub fn try_wait(&self) -> bool {
loop {
let res = unsafe { libc::sem_trywait(mem::transmute(self.handle.get())) };
if res == -1 {
let errno = PosixError::last();
if errno.is_would_block() {
break false;
}
debug_assert_eq!(errno.raw_code(), libc::EINTR, "Unexpected error");
continue;
}
break true;
}
}
///Attempts to decrement self within provided time, returning whether self was signaled or not.
///
///Returns `true` if self was signaled within specified timeout
///
///Returns `false` otherwise
pub fn wait_timeout(&self, duration: core::time::Duration) -> bool {
let mut timeout = mem::MaybeUninit::uninit();
if unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, timeout.as_mut_ptr()) } == -1
{
panic!("Failed to get current time");
}
let mut timeout = unsafe { timeout.assume_init() };
timeout.tv_sec = timeout.tv_sec.saturating_add(duration.as_secs() as _);
timeout.tv_nsec = timeout.tv_nsec.saturating_add(duration.subsec_nanos() as _);
if timeout.tv_nsec > 999999999 {
timeout.tv_nsec = 0;
timeout.tv_sec = timeout.tv_sec.saturating_add(1);
}
loop {
let res =
unsafe { libc::sem_timedwait(mem::transmute(self.handle.get()), &timeout) };
if res == -1 {
let errno = PosixError::last();
if errno.is_would_block() || errno.raw_code() == libc::ETIMEDOUT {
break false;
}
if errno.raw_code() != libc::EINTR {
panic!("Unexpected error: {}", errno);
}
continue;
}
break true;
}
}
///Increments self, waking any awaiting thread as result.
pub fn signal(&self, count: usize) {
for _ in 0..count {
let res = unsafe { libc::sem_post(mem::transmute(self.handle.get())) };
debug_assert_eq!(res, 0);
}
}
///Performs deinitialization.
///
///Using `Sem` after `close` is undefined behaviour, unless `init` is called
pub unsafe fn close(&self) {
let handle = self.handle.get();
if let Ok(INITED) =
self.state
.compare_exchange(INITED, UNINIT, Ordering::SeqCst, Ordering::Acquire)
{
libc::sem_destroy(mem::transmute(handle));
}
}
}
impl Drop for Sem {
fn drop(&mut self) {
unsafe {
self.close();
}
}
}
unsafe impl Send for Sem {}
unsafe impl Sync for Sem {}
}
#[cfg(any(
all(unix, not(any(target_os = "macos", target_os = "ios"))),
target_os = "fuchsia"
))]
pub use posix::*;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
))]
pub mod mach {
use super::unlikely;
use core::ffi::c_void;
use core::sync::atomic::{AtomicPtr, Ordering};
use core::{mem, ptr};
#[repr(C)]
struct TimeSpec {
tv_sec: libc::c_uint,
tv_nsec: libc::c_int,
}
impl Into<TimeSpec> for core::time::Duration {
fn into(self) -> TimeSpec {
use core::convert::TryFrom;
TimeSpec {
tv_sec: libc::c_uint::try_from(self.as_secs())
.unwrap_or(libc::c_uint::max_value()),
tv_nsec: libc::c_int::try_from(self.subsec_nanos())
.unwrap_or(libc::c_int::max_value()),
}
}
}
const KERN_OPERATION_TIMED_OUT: libc::c_int = 49;
const SYNC_POLICY_FIFO: libc::c_int = 0;
extern "C" {
static mach_task_self_: libc::c_uint;
//typedef struct semaphore *semaphore_t;
//Function takes semaphore_t*
fn semaphore_create(
task: libc::c_uint,
semaphore: *mut *mut c_void,
policy: libc::c_int,
value: libc::c_int,
) -> libc::c_int;
fn semaphore_signal(semaphore: *mut c_void) -> libc::c_int;
fn semaphore_wait(semaphore: *mut c_void) -> libc::c_int;
fn semaphore_timedwait(semaphore: *mut c_void, timeout: TimeSpec) -> libc::c_int;
fn semaphore_destroy(task: libc::c_uint, semaphore: *mut c_void) -> libc::c_int;
}
///MacOS semaphore based on mach API
pub struct Sem {
handle: AtomicPtr<c_void>,
}
impl Sem {
///Creates new uninit instance.
///
///It is UB to use it until `init` is called.
pub const unsafe fn new_uninit() -> Self {
Self {
handle: AtomicPtr::new(ptr::null_mut()),
}
}
#[inline(always)]
///Returns whether semaphore is successfully initialized
pub fn is_init(&self) -> bool {
!self.handle.load(Ordering::Acquire).is_null()
}
#[must_use]
///Initializes semaphore with provided `init` as initial value.
///
///Returns `true` on success.
///
///Returns `false` if semaphore is already initialized or initialization failed.
pub fn init(&self, init: u32) -> bool {
if !self.handle.load(Ordering::Acquire).is_null() {
//Similarly to `Once` we give priority to already-init path
return false;
} else {
let mut handle = mem::MaybeUninit::uninit();
let res = unsafe {
semaphore_create(
mach_task_self_,
handle.as_mut_ptr(),
SYNC_POLICY_FIFO,
init as libc::c_int,
)
};
let res = match res {
0 => unsafe {
let handle = handle.assume_init();
match self.handle.compare_exchange(
ptr::null_mut(),
handle,
Ordering::SeqCst,
Ordering::Acquire,
) {
Ok(_) => true,
Err(_) => {
semaphore_destroy(mach_task_self_, handle);
false
}
}
},
_ => false,
};
unlikely(res)
}
}
///Creates new instance, initializing it with `init`
pub fn new(init: u32) -> Option<Self> {
let result = unsafe { Self::new_uninit() };
if result.init(init) {
Some(result)
} else {
unlikely(None)
}
}
///Decrements self, returning immediately if it was signaled.
///
///Otherwise awaits for signal.
pub fn wait(&self) {
loop {
let result = unsafe { semaphore_wait(self.handle.load(Ordering::Acquire)) };
if result != libc::KERN_ABORTED {
assert_eq!(result, libc::KERN_SUCCESS, "Failed to wait on semaphore");
break;
}
}
}
#[inline]
///Attempts to decrement self, returning whether self was signaled or not.
///
///Returns `true` if self was signaled.
///
///Returns `false` otherwise.
pub fn try_wait(&self) -> bool {
self.wait_timeout(core::time::Duration::from_secs(0))
}
///Attempts to decrement self within provided time, returning whether self was signaled or not.
///
///Returns `true` if self was signaled within specified timeout
///
///Returns `false` otherwise
pub fn wait_timeout(&self, timeout: core::time::Duration) -> bool {
let result = unsafe {
semaphore_timedwait(self.handle.load(Ordering::Acquire), timeout.into())
};
debug_assert!(
result == 0 || result == KERN_OPERATION_TIMED_OUT,
"semaphore_timedwait() failed"
);
result == 0
}
///Increments self, waking any awaiting thread as result.
pub fn signal(&self, count: usize) {
for _ in 0..count {
let res = unsafe { semaphore_signal(self.handle.load(Ordering::Acquire)) };
debug_assert_eq!(res, 0, "semaphore_signal() failed");
}
}
///Performs deinitialization.
///
///Using `Sem` after `close` is undefined behaviour, unless `init` is called
pub unsafe fn close(&self) {
let handle = self.handle.swap(ptr::null_mut(), Ordering::AcqRel);
if !handle.is_null() {
semaphore_destroy(mach_task_self_, handle);
}
}
}
impl Drop for Sem {
fn drop(&mut self) {
unsafe {
self.close();
}
}
}
unsafe impl Send for Sem {}
unsafe impl Sync for Sem {}
}
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
))]
pub use mach::*;
#[cfg(windows)]
pub mod windows {
use core::ffi::c_void;
use core::ptr;
use core::sync::atomic::{AtomicPtr, Ordering};
use super::unlikely;
const WAIT_OBJECT_0: u32 = 0;
const WAIT_TIMEOUT: u32 = 0x00000102;
const INFINITE: u32 = 0xFFFFFFFF;
extern "system" {
fn CloseHandle(handle: *mut c_void) -> i32;
fn CreateSemaphoreW(
attrs: *mut c_void,
initial: i32,
max: i32,
name: *const u16,
) -> *mut c_void;
fn WaitForSingleObject(handle: *mut c_void, timeout_ms: u32) -> u32;
fn ReleaseSemaphore(
handle: *mut c_void,
increment: i32,
previous_increment: *mut i32,
) -> i32;
}
///Windows implementation of Semaphore
pub struct Sem {
handle: AtomicPtr<c_void>,
}
impl Sem {
///Creates new uninit instance.
///
///It is UB to use it until `init` is called.
pub const unsafe fn new_uninit() -> Self {
Self {
handle: AtomicPtr::new(ptr::null_mut()),
}
}
#[inline(always)]
///Returns whether semaphore is successfully initialized
pub fn is_init(&self) -> bool {
!self.handle.load(Ordering::Acquire).is_null()
}
#[must_use]
///Initializes semaphore with provided `init` as initial value.
///
///Returns `true` on success.
///
///Returns `false` if semaphore is already initialized or initialization failed.
pub fn init(&self, init: u32) -> bool {
if !self.handle.load(Ordering::Acquire).is_null() {
//Similarly to `Once` we give priority to already-init path
return false;
} else {
let handle = unsafe {
CreateSemaphoreW(
ptr::null_mut(),
init as i32,
i32::max_value(),
ptr::null(),
)
};
let res = match self.handle.compare_exchange(
ptr::null_mut(),
handle,
Ordering::SeqCst,
Ordering::Acquire,
) {
Ok(_) => !handle.is_null(),
Err(_) => {
unsafe {
CloseHandle(handle);
}
unlikely(false)
}
};
unlikely(res)
}
}
///Creates new instance, initializing it with `init`
pub fn new(init: u32) -> Option<Self> {
let result = unsafe { Self::new_uninit() };
if result.init(init) {
Some(result)
} else {
unlikely(None)
}
}
///Decrements self, returning immediately if it was signaled.
///
///Otherwise awaits for signal.
pub fn wait(&self) {
let result =
unsafe { WaitForSingleObject(self.handle.load(Ordering::Acquire), INFINITE) };
match result {
WAIT_OBJECT_0 => (),
//We cannot really timeout when there is no timeout
other => panic!("Unexpected result: {}", other),
}
}
#[inline]
///Attempts to decrement self, returning whether self was signaled or not.
///
///Returns `true` if self was signaled.
///
///Returns `false` otherwise.
pub fn try_wait(&self) -> bool {
self.wait_timeout(core::time::Duration::from_secs(0))
}
///Attempts to decrement self within provided time, returning whether self was signaled or not.
///
///Returns `true` if self was signaled within specified timeout
///
///Returns `false` otherwise
pub fn wait_timeout(&self, timeout: core::time::Duration) -> bool {
use core::convert::TryInto;
let result = unsafe {
WaitForSingleObject(
self.handle.load(Ordering::Acquire),
timeout.as_millis().try_into().unwrap_or(u32::max_value()),
)
};
match result {
WAIT_OBJECT_0 => true,
WAIT_TIMEOUT => false,
other => panic!("Unexpected result: {}", other),
}
}
///Increments self, waking any awaiting thread as result.
pub fn signal(&self, count: usize) {
let res = unsafe {
ReleaseSemaphore(self.handle.load(Ordering::Acquire), count as _, ptr::null_mut())
};
debug_assert_ne!(res, 0);
}
///Performs deinitialization.
///
///Using `Sem` after `close` is undefined behaviour, unless `init` is called
pub unsafe fn close(&self) {
let handle = self.handle.swap(ptr::null_mut(), Ordering::AcqRel);
if !handle.is_null() {
CloseHandle(handle);
}
}
}
impl Drop for Sem {
fn drop(&mut self) {
unsafe {
self.close();
}
}
}
unsafe impl Send for Sem {}
unsafe impl Sync for Sem {}
}
#[cfg(windows)]
pub use windows::*;
}
pub use os::Sem;
use crate::{heap::thread::{Thread, }, thread::safepoint_scope};
impl Sem {
#[inline]
pub fn wait_with_safepoint_check(&self) {
// Prepare to block and allow safepoints while blocked
safepoint_scope(|| {
self.wait()
});
}
}