]>
git.saurik.com Git - apple/xnu.git/blob - tests/ulock.c
1 #include <darwintest.h>
11 #ifndef __TSD_MACH_THREAD_SELF
12 #define __TSD_MACH_THREAD_SELF 3
15 #pragma clang diagnostic push
16 #pragma clang diagnostic ignored "-Wbad-function-cast"
17 __inline
static mach_port_name_t
20 mach_port_name_t self
= (mach_port_name_t
)_os_tsd_get_direct(__TSD_MACH_THREAD_SELF
);
23 #pragma clang diagnostic pop
25 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
27 #pragma mark ulock_non_owner_wake
29 static _Atomic
uint32_t test_ulock
;
32 test_waiter(void *arg __unused
)
35 uint32_t test_ulock_owner
= atomic_load_explicit(&test_ulock
,
36 memory_order_relaxed
);
37 int rc
= __ulock_wait(UL_UNFAIR_LOCK
| ULF_NO_ERRNO
, &test_ulock
,
39 if (rc
== -EINTR
|| rc
== -EFAULT
) {
42 T_ASSERT_GE(rc
, 0, "__ulock_wait");
46 T_PASS("Waiter woke");
53 test_waker(void *arg __unused
)
56 int rc
= __ulock_wake(UL_UNFAIR_LOCK
| ULF_NO_ERRNO
| ULF_WAKE_ALLOW_NON_OWNER
,
61 T_ASSERT_EQ(rc
, 0, "__ulock_wake");
67 T_DECL(ulock_non_owner_wake
, "ulock_wake respects non-owner wakes",
68 T_META_CHECK_LEAKS(false))
70 pthread_t waiter
, waker
;
72 atomic_store_explicit(&test_ulock
, _os_get_self() & ~0x3u
, memory_order_relaxed
);
74 T_ASSERT_POSIX_ZERO(pthread_create(&waiter
, NULL
, test_waiter
, NULL
), "create waiter");
76 // wait for the waiter to reach the kernel
78 int kernel_ulocks
= __ulock_wake(UL_DEBUG_HASH_DUMP_PID
, NULL
, 0);
79 T_QUIET
; T_ASSERT_NE(kernel_ulocks
, -1, "UL_DEBUG_HASH_DUMP_PID");
81 if (kernel_ulocks
== 1) {
82 T_LOG("waiter is now waiting");
88 T_ASSERT_POSIX_ZERO(pthread_create(&waker
, NULL
, test_waker
, NULL
), "create waker");
90 // won't ever actually join
91 pthread_join(waiter
, NULL
);