Rollup merge of #102773 - joboet:apple_parker, r=thomcc

Use semaphores for thread parking on Apple platforms

Currently we use a mutex-condvar pair for thread parking on Apple systems. Unfortunately, `pthread_cond_timedwait` uses the real-time clock for measuring time, which causes problems when the system time changes. The parking implementation in this PR uses a semaphore instead, which measures monotonic time by default, avoiding these issues. As a further benefit, this has the potential to improve performance a bit, since `unpark` does not need to wait for a lock to be released.

Since the Mach semaphores are poorly documented (I could not find availability or stability guarantees for instance), this uses a [dispatch semaphore](https://developer.apple.com/documentation/dispatch/dispatch_semaphore?language=objc) instead. While it adds a layer of indirection (it uses Mach semaphores internally), the overhead is probably negligible.

Tested on macOS 12.5.

r? ``````@thomcc``````
This commit is contained in:
Dylan DPC
2022-10-15 15:45:30 +05:30
committed by GitHub
3 changed files with 165 additions and 1 deletions

View File

@@ -244,6 +244,28 @@ fn test_try_panic_any_message_unit_struct() {
}
}
#[test]
fn test_park_unpark_before() {
for _ in 0..10 {
thread::current().unpark();
thread::park();
}
}
#[test]
fn test_park_unpark_called_other_thread() {
for _ in 0..10 {
let th = thread::current();
let _guard = thread::spawn(move || {
super::sleep(Duration::from_millis(50));
th.unpark();
});
thread::park();
}
}
#[test]
fn test_park_timeout_unpark_before() {
for _ in 0..10 {