2 * Copyright (c) 2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #define OS_UNFAIR_LOCK_INLINE 1
22 #include "lock_internal.h"
23 #include "os/internal.h"
25 #include "libkern/OSAtomic.h"
27 #include "os/lock_private.h"
28 #include "os/once_private.h"
30 #include <mach/mach_init.h>
31 #include <mach/mach_traps.h>
32 #include <mach/thread_switch.h>
33 #include <mach/mach_time.h>
37 #pragma mark _os_lock_base_t
39 OS_NOINLINE OS_NORETURN OS_COLD
40 void _os_lock_corruption_abort(void *lock_ptr OS_UNUSED
, uintptr_t lock_value
);
43 OS_LOCK_STRUCT_DECL_INTERNAL(base
);
44 OS_USED
static OS_LOCK_TYPE_STRUCT_DECL(base
);
47 os_lock_lock(os_lock_t l
)
49 return l
._osl_base
->osl_type
->osl_lock(l
);
53 os_lock_trylock(os_lock_t l
)
55 return l
._osl_base
->osl_type
->osl_trylock(l
);
59 os_lock_unlock(os_lock_t l
)
61 return l
._osl_base
->osl_type
->osl_unlock(l
);
64 OS_NOINLINE OS_NORETURN OS_COLD
66 _os_lock_corruption_abort(void *lock_ptr OS_UNUSED
, uintptr_t lock_value
)
68 __LIBPLATFORM_CLIENT_CRASH__(lock_value
, "os_lock is corrupt");
73 #pragma mark OSSpinLock
75 OS_NOEXPORT OS_NOINLINE
void _OSSpinLockLockSlow(volatile OSSpinLock
*l
);
77 OS_ATOMIC_EXPORT
void OSSpinLockLock(volatile OSSpinLock
*l
);
78 OS_ATOMIC_EXPORT
bool OSSpinLockTry(volatile OSSpinLock
*l
);
79 OS_ATOMIC_EXPORT
int spin_lock_try(volatile OSSpinLock
*l
);
80 OS_ATOMIC_EXPORT
void OSSpinLockUnlock(volatile OSSpinLock
*l
);
82 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
83 static const OSSpinLock _OSSpinLockLocked
= 1;
85 static const OSSpinLock _OSSpinLockLocked
= -1;
93 #define OS_LOCK_SPIN_SPIN_TRIES 1000
94 #define OS_LOCK_SPIN_PAUSE() os_hardware_pause()
99 _os_lock_yield_deadline(mach_msg_timeout_t timeout
)
101 uint64_t abstime
= timeout
* NSEC_PER_MSEC
;
102 #if !(defined(__i386__) || defined(__x86_64__))
103 mach_timebase_info_data_t tbi
;
104 kern_return_t kr
= mach_timebase_info(&tbi
);
105 if (kr
) return UINT64_MAX
;
106 abstime
*= tbi
.denom
;
107 abstime
/= tbi
.numer
;
109 return mach_absolute_time() + abstime
;
114 _os_lock_yield_until(uint64_t deadline
)
116 return mach_absolute_time() < deadline
;
121 _OSSpinLockLockYield(volatile OSSpinLock
*l
)
123 int option
= SWITCH_OPTION_DEPRESS
;
124 mach_msg_timeout_t timeout
= 1;
125 uint64_t deadline
= _os_lock_yield_deadline(timeout
);
127 while (unlikely(lock
= *l
)) {
129 if (unlikely(lock
!= _OSSpinLockLocked
)) {
130 _os_lock_corruption_abort((void *)l
, (uintptr_t)lock
);
132 thread_switch(MACH_PORT_NULL
, option
, timeout
);
133 if (option
== SWITCH_OPTION_WAIT
) {
135 } else if (!_os_lock_yield_until(deadline
)) {
136 option
= SWITCH_OPTION_WAIT
;
139 bool r
= os_atomic_cmpxchgv(l
, 0, _OSSpinLockLocked
, &lock
, acquire
);
140 if (likely(r
)) return;
146 _OSSpinLockLockSlow(volatile OSSpinLock
*l
)
148 return _OSSpinLockLockYield(l
); // Don't spin on UP
150 #else // !OS_ATOMIC_UP
152 _OSSpinLockLockSlow(volatile OSSpinLock
*l
)
154 uint32_t tries
= OS_LOCK_SPIN_SPIN_TRIES
;
156 while (unlikely(lock
= *l
)) {
158 if (unlikely(lock
!= _OSSpinLockLocked
)) {
159 return _os_lock_corruption_abort((void *)l
, (uintptr_t)lock
);
161 if (unlikely(!tries
--)) return _OSSpinLockLockYield(l
);
162 OS_LOCK_SPIN_PAUSE();
164 bool r
= os_atomic_cmpxchgv(l
, 0, _OSSpinLockLocked
, &lock
, acquire
);
165 if (likely(r
)) return;
168 #endif // !OS_ATOMIC_UP
172 #if OS_LOCK_OSSPINLOCK_IS_NOSPINLOCK && !TARGET_OS_SIMULATOR
174 typedef struct _os_nospin_lock_s
*_os_nospin_lock_t
;
176 OS_ATOMIC_EXPORT
void _os_nospin_lock_lock(_os_nospin_lock_t lock
);
177 OS_ATOMIC_EXPORT
bool _os_nospin_lock_trylock(_os_nospin_lock_t lock
);
178 OS_ATOMIC_EXPORT
void _os_nospin_lock_unlock(_os_nospin_lock_t lock
);
181 OSSpinLockLock(volatile OSSpinLock
*l
)
183 OS_ATOMIC_ALIAS(spin_lock
, OSSpinLockLock
);
184 OS_ATOMIC_ALIAS(_spin_lock
, OSSpinLockLock
);
185 return _os_nospin_lock_lock((_os_nospin_lock_t
)l
);
189 OSSpinLockTry(volatile OSSpinLock
*l
)
191 return _os_nospin_lock_trylock((_os_nospin_lock_t
)l
);
195 spin_lock_try(volatile OSSpinLock
*l
)
197 OS_ATOMIC_ALIAS(_spin_lock_try
, spin_lock_try
);
198 return _os_nospin_lock_trylock((_os_nospin_lock_t
)l
);
202 OSSpinLockUnlock(volatile OSSpinLock
*l
)
204 OS_ATOMIC_ALIAS(spin_unlock
, OSSpinLockUnlock
);
205 OS_ATOMIC_ALIAS(_spin_unlock
, OSSpinLockUnlock
);
206 return _os_nospin_lock_unlock((_os_nospin_lock_t
)l
);
209 #undef OS_ATOMIC_ALIAS
210 #define OS_ATOMIC_ALIAS(n, o)
211 static void _OSSpinLockLock(volatile OSSpinLock
*l
);
212 #undef OSSpinLockLock
213 #define OSSpinLockLock _OSSpinLockLock
214 static bool _OSSpinLockTry(volatile OSSpinLock
*l
);
216 #define OSSpinLockTry _OSSpinLockTry
217 static __unused
int __spin_lock_try(volatile OSSpinLock
*l
);
219 #define spin_lock_try __spin_lock_try
220 static void _OSSpinLockUnlock(volatile OSSpinLock
*l
);
221 #undef OSSpinLockUnlock
222 #define OSSpinLockUnlock _OSSpinLockUnlock
224 #endif // OS_LOCK_OSSPINLOCK_IS_NOSPINLOCK
227 OSSpinLockLock(volatile OSSpinLock
*l
)
229 OS_ATOMIC_ALIAS(spin_lock
, OSSpinLockLock
);
230 OS_ATOMIC_ALIAS(_spin_lock
, OSSpinLockLock
);
231 bool r
= os_atomic_cmpxchg(l
, 0, _OSSpinLockLocked
, acquire
);
232 if (likely(r
)) return;
233 return _OSSpinLockLockSlow(l
);
237 OSSpinLockTry(volatile OSSpinLock
*l
)
239 bool r
= os_atomic_cmpxchg(l
, 0, _OSSpinLockLocked
, acquire
);
244 spin_lock_try(volatile OSSpinLock
*l
) // <rdar://problem/13316060>
246 OS_ATOMIC_ALIAS(_spin_lock_try
, spin_lock_try
);
247 return OSSpinLockTry(l
);
251 OSSpinLockUnlock(volatile OSSpinLock
*l
)
253 OS_ATOMIC_ALIAS(spin_unlock
, OSSpinLockUnlock
);
254 OS_ATOMIC_ALIAS(_spin_unlock
, OSSpinLockUnlock
);
255 os_atomic_store(l
, 0, release
);
260 #pragma mark os_lock_spin_t
262 OS_LOCK_STRUCT_DECL_INTERNAL(spin
,
263 OSSpinLock
volatile osl_spinlock
;
265 OS_LOCK_METHODS_DECL(spin
);
266 OS_LOCK_TYPE_INSTANCE(spin
);
269 _os_lock_spin_lock(_os_lock_spin_t l
)
271 return OSSpinLockLock(&l
->osl_spinlock
);
275 _os_lock_spin_trylock(_os_lock_spin_t l
)
277 return OSSpinLockTry(&l
->osl_spinlock
);
281 _os_lock_spin_unlock(_os_lock_spin_t l
)
283 return OSSpinLockUnlock(&l
->osl_spinlock
);
288 #pragma mark os_lock_owner_t
290 #ifndef __TSD_MACH_THREAD_SELF
291 #define __TSD_MACH_THREAD_SELF 3
294 typedef mach_port_name_t os_lock_owner_t
;
295 #define OS_LOCK_NO_OWNER MACH_PORT_NULL
298 OS_ALWAYS_INLINE OS_CONST
299 static inline os_lock_owner_t
300 _os_lock_owner_get_self(void)
302 os_lock_owner_t self
;
303 self
= (os_lock_owner_t
)_os_tsd_get_direct(__TSD_MACH_THREAD_SELF
);
308 OS_NOINLINE OS_NORETURN OS_COLD
310 _os_lock_recursive_abort(os_lock_owner_t owner
)
312 __LIBPLATFORM_CLIENT_CRASH__(owner
, "Trying to recursively lock an "
318 #pragma mark os_lock_handoff_t
320 OS_LOCK_STRUCT_DECL_INTERNAL(handoff
,
321 os_lock_owner_t
volatile osl_owner
;
323 OS_LOCK_METHODS_DECL(handoff
);
324 OS_LOCK_TYPE_INSTANCE(handoff
);
326 #define OS_LOCK_HANDOFF_YIELD_TRIES 100
330 _os_lock_handoff_lock_slow(_os_lock_handoff_t l
)
332 int option
= SWITCH_OPTION_OSLOCK_DEPRESS
;
333 mach_msg_timeout_t timeout
= 1;
334 uint32_t tries
= OS_LOCK_HANDOFF_YIELD_TRIES
;
335 os_lock_owner_t self
= _os_lock_owner_get_self(), owner
;
336 while (unlikely(owner
= l
->osl_owner
)) {
338 if (unlikely(owner
== self
)) return _os_lock_recursive_abort(self
);
339 // Yield until tries first hits zero, then permanently switch to wait
340 if (unlikely(!tries
--)) option
= SWITCH_OPTION_OSLOCK_WAIT
;
341 thread_switch(owner
, option
, timeout
);
342 // Redrive the handoff every 1ms until switching to wait
343 if (option
== SWITCH_OPTION_OSLOCK_WAIT
) timeout
++;
345 bool r
= os_atomic_cmpxchgv2o(l
, osl_owner
, MACH_PORT_NULL
, self
, &owner
,
347 if (likely(r
)) return;
352 _os_lock_handoff_lock(_os_lock_handoff_t l
)
354 os_lock_owner_t self
= _os_lock_owner_get_self();
355 bool r
= os_atomic_cmpxchg2o(l
, osl_owner
, MACH_PORT_NULL
, self
, acquire
);
356 if (likely(r
)) return;
357 return _os_lock_handoff_lock_slow(l
);
361 _os_lock_handoff_trylock(_os_lock_handoff_t l
)
363 os_lock_owner_t self
= _os_lock_owner_get_self();
364 bool r
= os_atomic_cmpxchg2o(l
, osl_owner
, MACH_PORT_NULL
, self
, acquire
);
369 _os_lock_handoff_unlock(_os_lock_handoff_t l
)
371 os_atomic_store2o(l
, osl_owner
, MACH_PORT_NULL
, release
);
376 #pragma mark os_ulock_value_t
378 #include <sys/errno.h>
379 #include <sys/ulock.h>
381 typedef os_lock_owner_t os_ulock_value_t
;
383 // This assumes that all thread mach port values always have the low bit set!
384 // Clearing this bit is used to communicate the existence of waiters to unlock.
385 #define OS_ULOCK_NOWAITERS_BIT ((os_ulock_value_t)1u)
386 #define OS_ULOCK_OWNER(value) ((value) | OS_ULOCK_NOWAITERS_BIT)
388 #define OS_ULOCK_ANONYMOUS_OWNER MACH_PORT_DEAD
389 #define OS_ULOCK_IS_OWNER(value, self, allow_anonymous_owner) ({ \
390 os_lock_owner_t _owner = OS_ULOCK_OWNER(value); (_owner == (self)) && \
391 (!(allow_anonymous_owner) || _owner != OS_ULOCK_ANONYMOUS_OWNER); })
392 #define OS_ULOCK_IS_NOT_OWNER(value, self, allow_anonymous_owner) ({ \
393 os_lock_owner_t _owner = OS_ULOCK_OWNER(value); (_owner != (self)) && \
394 (!(allow_anonymous_owner) || _owner != OS_ULOCK_ANONYMOUS_OWNER); })
397 #pragma mark os_unfair_lock
399 typedef struct _os_unfair_lock_s
{
400 os_ulock_value_t oul_value
;
401 } *_os_unfair_lock_t
;
403 _Static_assert(sizeof(struct os_unfair_lock_s
) ==
404 sizeof(struct _os_unfair_lock_s
), "os_unfair_lock size mismatch");
406 OS_ATOMIC_EXPORT
void os_unfair_lock_lock(os_unfair_lock_t lock
);
407 OS_ATOMIC_EXPORT
void os_unfair_lock_lock_with_options(os_unfair_lock_t lock
,
408 os_unfair_lock_options_t options
);
409 OS_ATOMIC_EXPORT
bool os_unfair_lock_trylock(os_unfair_lock_t lock
);
410 OS_ATOMIC_EXPORT
void os_unfair_lock_unlock(os_unfair_lock_t lock
);
412 OS_ATOMIC_EXPORT
void os_unfair_lock_lock_no_tsd_4libpthread(
413 os_unfair_lock_t lock
);
414 OS_ATOMIC_EXPORT
void os_unfair_lock_unlock_no_tsd_4libpthread(
415 os_unfair_lock_t lock
);
416 OS_ATOMIC_EXPORT
void os_unfair_lock_lock_with_options_4Libc(
417 os_unfair_lock_t lock
, os_unfair_lock_options_t options
);
418 OS_ATOMIC_EXPORT
void os_unfair_lock_unlock_4Libc(os_unfair_lock_t lock
);
420 OS_NOINLINE OS_NORETURN OS_COLD
421 void _os_unfair_lock_recursive_abort(os_lock_owner_t owner
);
422 OS_NOINLINE OS_NORETURN OS_COLD
423 void _os_unfair_lock_unowned_abort(os_lock_owner_t owner
);
424 OS_NOINLINE OS_NORETURN OS_COLD
425 void _os_unfair_lock_corruption_abort(os_ulock_value_t current
);
427 _Static_assert(OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION
==
428 ULF_WAIT_WORKQ_DATA_CONTENTION
,
429 "check value for OS_UNFAIR_LOCK_OPTIONS_MASK");
430 #define OS_UNFAIR_LOCK_OPTIONS_MASK \
431 (os_unfair_lock_options_t)(OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION)
432 #define OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER 0x01000000u
435 OS_NOINLINE OS_NORETURN OS_COLD
437 _os_unfair_lock_recursive_abort(os_lock_owner_t owner
)
439 __LIBPLATFORM_CLIENT_CRASH__(owner
, "Trying to recursively lock an "
443 OS_NOINLINE OS_NORETURN OS_COLD
445 _os_unfair_lock_unowned_abort(os_lock_owner_t owner
)
447 __LIBPLATFORM_CLIENT_CRASH__(owner
, "Unlock of an os_unfair_lock not "
448 "owned by current thread");
451 OS_NOINLINE OS_NORETURN OS_COLD
453 _os_unfair_lock_corruption_abort(os_ulock_value_t current
)
455 __LIBPLATFORM_CLIENT_CRASH__(current
, "os_unfair_lock is corrupt");
461 _os_unfair_lock_lock_slow(_os_unfair_lock_t l
, os_lock_owner_t self
,
462 os_unfair_lock_options_t options
)
464 os_unfair_lock_options_t allow_anonymous_owner
=
465 options
& OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER
;
466 options
&= ~OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER
;
467 if (unlikely(options
& ~OS_UNFAIR_LOCK_OPTIONS_MASK
)) {
468 __LIBPLATFORM_CLIENT_CRASH__(options
, "Invalid options");
470 os_ulock_value_t current
, new, waiters_mask
= 0;
471 while (unlikely((current
= os_atomic_load2o(l
, oul_value
, relaxed
)) !=
474 if (unlikely(OS_ULOCK_IS_OWNER(current
, self
, allow_anonymous_owner
))) {
475 return _os_unfair_lock_recursive_abort(self
);
477 new = current
& ~OS_ULOCK_NOWAITERS_BIT
;
478 if (current
!= new) {
479 // Clear nowaiters bit in lock value before waiting
480 if (!os_atomic_cmpxchgv2o(l
, oul_value
, current
, new, ¤t
,
486 int ret
= __ulock_wait(UL_UNFAIR_LOCK
| ULF_NO_ERRNO
| options
,
488 if (unlikely(ret
< 0)) {
494 _os_unfair_lock_corruption_abort(current
);
497 __LIBPLATFORM_INTERNAL_CRASH__(-ret
, "ulock_wait failure");
501 // If there are more waiters, unset nowaiters bit when acquiring lock
502 waiters_mask
= OS_ULOCK_NOWAITERS_BIT
;
505 new = self
& ~waiters_mask
;
506 bool r
= os_atomic_cmpxchgv2o(l
, oul_value
, OS_LOCK_NO_OWNER
, new,
508 if (unlikely(!r
)) goto _retry
;
513 _os_unfair_lock_unlock_slow(_os_unfair_lock_t l
, os_ulock_value_t current
,
514 os_lock_owner_t self
, os_unfair_lock_options_t options
)
516 os_unfair_lock_options_t allow_anonymous_owner
=
517 options
& OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER
;
518 options
&= ~OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER
;
519 if (unlikely(OS_ULOCK_IS_NOT_OWNER(current
, self
, allow_anonymous_owner
))) {
520 return _os_unfair_lock_unowned_abort(OS_ULOCK_OWNER(current
));
522 if (current
& OS_ULOCK_NOWAITERS_BIT
) {
523 __LIBPLATFORM_INTERNAL_CRASH__(current
, "unlock_slow with no waiters");
526 int ret
= __ulock_wake(UL_UNFAIR_LOCK
| ULF_NO_ERRNO
, l
, 0);
527 if (unlikely(ret
< 0)) {
534 __LIBPLATFORM_INTERNAL_CRASH__(-ret
, "ulock_wake failure");
542 os_unfair_lock_lock(os_unfair_lock_t lock
)
544 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
545 os_lock_owner_t self
= _os_lock_owner_get_self();
546 bool r
= os_atomic_cmpxchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, self
, acquire
);
547 if (likely(r
)) return;
548 return _os_unfair_lock_lock_slow(l
, self
, OS_UNFAIR_LOCK_NONE
);
552 os_unfair_lock_lock_with_options(os_unfair_lock_t lock
,
553 os_unfair_lock_options_t options
)
555 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
556 os_lock_owner_t self
= _os_lock_owner_get_self();
557 bool r
= os_atomic_cmpxchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, self
, acquire
);
558 if (likely(r
)) return;
559 return _os_unfair_lock_lock_slow(l
, self
, options
);
563 os_unfair_lock_trylock(os_unfair_lock_t lock
)
565 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
566 os_lock_owner_t self
= _os_lock_owner_get_self();
567 bool r
= os_atomic_cmpxchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, self
, acquire
);
572 os_unfair_lock_unlock(os_unfair_lock_t lock
)
574 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
575 os_lock_owner_t self
= _os_lock_owner_get_self();
576 os_ulock_value_t current
;
577 current
= os_atomic_xchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, release
);
578 if (likely(current
== self
)) return;
579 return _os_unfair_lock_unlock_slow(l
, current
, self
, 0);
583 os_unfair_lock_lock_no_tsd_4libpthread(os_unfair_lock_t lock
)
585 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
586 os_lock_owner_t self
= OS_ULOCK_ANONYMOUS_OWNER
;
587 bool r
= os_atomic_cmpxchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, self
, acquire
);
588 if (likely(r
)) return;
589 return _os_unfair_lock_lock_slow(l
, self
,
590 OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION
|
591 OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER
);
595 os_unfair_lock_unlock_no_tsd_4libpthread(os_unfair_lock_t lock
)
597 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
598 os_lock_owner_t self
= OS_ULOCK_ANONYMOUS_OWNER
;
599 os_ulock_value_t current
;
600 current
= os_atomic_xchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, release
);
601 if (likely(current
== self
)) return;
602 return _os_unfair_lock_unlock_slow(l
, current
, self
,
603 OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER
);
608 os_unfair_lock_assert_owner(os_unfair_lock_t lock
)
610 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
611 os_lock_owner_t self
= _os_lock_owner_get_self();
612 os_ulock_value_t current
= os_atomic_load2o(l
, oul_value
, relaxed
);
613 if (unlikely(OS_ULOCK_IS_NOT_OWNER(current
, self
, 0))) {
614 __LIBPLATFORM_CLIENT_CRASH__(current
, "Assertion failed: "
615 "Lock unexpectedly not owned by current thread");
620 os_unfair_lock_assert_not_owner(os_unfair_lock_t lock
)
622 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
623 os_lock_owner_t self
= _os_lock_owner_get_self();
624 os_ulock_value_t current
= os_atomic_load2o(l
, oul_value
, relaxed
);
625 if (unlikely(OS_ULOCK_IS_OWNER(current
, self
, 0))) {
626 __LIBPLATFORM_CLIENT_CRASH__(current
, "Assertion failed: "
627 "Lock unexpectedly owned by current thread");
633 #pragma mark os_unfair_recursive_lock
636 void os_unfair_recursive_lock_lock_with_options(os_unfair_recursive_lock_t lock
,
637 os_unfair_lock_options_t options
);
640 bool os_unfair_recursive_lock_trylock(os_unfair_recursive_lock_t lock
);
643 void os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock
);
646 bool os_unfair_recursive_lock_tryunlock4objc(os_unfair_recursive_lock_t lock
);
649 static inline os_lock_owner_t
650 _os_unfair_lock_owner(os_unfair_lock_t lock
)
652 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
653 return OS_ULOCK_OWNER(os_atomic_load(&l
->oul_value
, relaxed
));
657 os_unfair_recursive_lock_lock_with_options(os_unfair_recursive_lock_t lock
,
658 os_unfair_lock_options_t options
)
660 os_lock_owner_t cur
, self
= _os_lock_owner_get_self();
661 _os_unfair_lock_t l
= (_os_unfair_lock_t
)&lock
->ourl_lock
;
663 if (likely(os_atomic_cmpxchgv2o(l
, oul_value
,
664 OS_LOCK_NO_OWNER
, self
, &cur
, acquire
))) {
668 if (OS_ULOCK_OWNER(cur
) == self
) {
673 return _os_unfair_lock_lock_slow(l
, self
, options
);
677 os_unfair_recursive_lock_trylock(os_unfair_recursive_lock_t lock
)
679 os_lock_owner_t cur
, self
= _os_lock_owner_get_self();
680 _os_unfair_lock_t l
= (_os_unfair_lock_t
)&lock
->ourl_lock
;
682 if (likely(os_atomic_cmpxchgv2o(l
, oul_value
,
683 OS_LOCK_NO_OWNER
, self
, &cur
, acquire
))) {
687 if (likely(OS_ULOCK_OWNER(cur
) == self
)) {
698 _os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock
,
699 os_lock_owner_t self
)
701 if (unlikely(lock
->ourl_count
)) {
702 os_lock_owner_t cur
= _os_unfair_lock_owner(&lock
->ourl_lock
);
703 if (unlikely(cur
!= self
)) {
704 _os_unfair_lock_unowned_abort(cur
);
710 _os_unfair_lock_t l
= (_os_unfair_lock_t
)lock
;
711 os_ulock_value_t current
;
712 current
= os_atomic_xchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, release
);
713 if (likely(current
== self
)) return;
714 return _os_unfair_lock_unlock_slow(l
, current
, self
, 0);
718 os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock
)
720 os_lock_owner_t self
= _os_lock_owner_get_self();
721 _os_unfair_recursive_lock_unlock(lock
, self
);
725 os_unfair_recursive_lock_tryunlock4objc(os_unfair_recursive_lock_t lock
)
727 os_lock_owner_t cur
= _os_unfair_lock_owner(&lock
->ourl_lock
);
728 os_lock_owner_t self
= _os_lock_owner_get_self();
729 if (likely(cur
== self
)) {
730 _os_unfair_recursive_lock_unlock(lock
, self
);
738 #pragma mark _os_lock_unfair_t
740 OS_LOCK_STRUCT_DECL_INTERNAL(unfair
,
741 os_unfair_lock osl_unfair_lock
;
743 OS_LOCK_METHODS_DECL(unfair
);
744 OS_LOCK_TYPE_INSTANCE(unfair
);
747 _os_lock_unfair_lock(_os_lock_unfair_t l
)
749 return os_unfair_lock_lock(&l
->osl_unfair_lock
);
753 _os_lock_unfair_trylock(_os_lock_unfair_t l
)
755 return os_unfair_lock_trylock(&l
->osl_unfair_lock
);
759 _os_lock_unfair_unlock(_os_lock_unfair_t l
)
761 return os_unfair_lock_unlock(&l
->osl_unfair_lock
);
766 #pragma mark _os_nospin_lock
768 typedef struct _os_nospin_lock_s
{
769 os_ulock_value_t oul_value
;
770 } _os_nospin_lock
, *_os_nospin_lock_t
;
772 _Static_assert(sizeof(OSSpinLock
) ==
773 sizeof(struct _os_nospin_lock_s
), "os_nospin_lock size mismatch");
775 OS_ATOMIC_EXPORT
void _os_nospin_lock_lock(_os_nospin_lock_t lock
);
776 OS_ATOMIC_EXPORT
bool _os_nospin_lock_trylock(_os_nospin_lock_t lock
);
777 OS_ATOMIC_EXPORT
void _os_nospin_lock_unlock(_os_nospin_lock_t lock
);
782 _os_nospin_lock_lock_slow(_os_nospin_lock_t l
)
784 os_lock_owner_t self
= _os_lock_owner_get_self();
785 os_ulock_value_t current
, new, waiters_mask
= 0;
786 uint32_t timeout
= 1;
787 while (unlikely((current
= os_atomic_load2o(l
, oul_value
, relaxed
)) !=
790 new = current
& ~OS_ULOCK_NOWAITERS_BIT
;
791 // For safer compatibility with OSSpinLock where _OSSpinLockLocked may
792 // be 1, check that new didn't become 0 (unlocked) by clearing this bit
793 if (current
!= new && new) {
794 // Clear nowaiters bit in lock value before waiting
795 if (!os_atomic_cmpxchgv2o(l
, oul_value
, current
, new, ¤t
,
801 int ret
= __ulock_wait(UL_COMPARE_AND_WAIT
| ULF_NO_ERRNO
, l
, current
,
803 if (unlikely(ret
< 0)) {
812 __LIBPLATFORM_INTERNAL_CRASH__(-ret
, "ulock_wait failure");
816 // If there are more waiters, unset nowaiters bit when acquiring lock
817 waiters_mask
= OS_ULOCK_NOWAITERS_BIT
;
820 new = self
& ~waiters_mask
;
821 bool r
= os_atomic_cmpxchgv2o(l
, oul_value
, OS_LOCK_NO_OWNER
, new,
823 if (unlikely(!r
)) goto _retry
;
828 _os_nospin_lock_unlock_slow(_os_nospin_lock_t l
, os_ulock_value_t current
)
830 os_lock_owner_t self
= _os_lock_owner_get_self();
831 if (unlikely(OS_ULOCK_OWNER(current
) != self
)) {
832 return; // no unowned_abort for drop-in compatibility with OSSpinLock
834 if (current
& OS_ULOCK_NOWAITERS_BIT
) {
835 __LIBPLATFORM_INTERNAL_CRASH__(current
, "unlock_slow with no waiters");
838 int ret
= __ulock_wake(UL_COMPARE_AND_WAIT
| ULF_NO_ERRNO
, l
, 0);
839 if (unlikely(ret
< 0)) {
846 __LIBPLATFORM_INTERNAL_CRASH__(-ret
, "ulock_wake failure");
854 _os_nospin_lock_lock(_os_nospin_lock_t l
)
856 os_lock_owner_t self
= _os_lock_owner_get_self();
857 bool r
= os_atomic_cmpxchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, self
, acquire
);
858 if (likely(r
)) return;
859 return _os_nospin_lock_lock_slow(l
);
863 _os_nospin_lock_trylock(_os_nospin_lock_t l
)
865 os_lock_owner_t self
= _os_lock_owner_get_self();
866 bool r
= os_atomic_cmpxchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, self
, acquire
);
871 _os_nospin_lock_unlock(_os_nospin_lock_t l
)
873 os_lock_owner_t self
= _os_lock_owner_get_self();
874 os_ulock_value_t current
;
875 current
= os_atomic_xchg2o(l
, oul_value
, OS_LOCK_NO_OWNER
, release
);
876 if (likely(current
== self
)) return;
877 return _os_nospin_lock_unlock_slow(l
, current
);
882 #pragma mark _os_lock_nospin_t
884 OS_LOCK_STRUCT_DECL_INTERNAL(nospin
,
885 _os_nospin_lock osl_nospin_lock
;
887 OS_LOCK_METHODS_DECL(nospin
);
888 OS_LOCK_TYPE_INSTANCE(nospin
);
891 _os_lock_nospin_lock(_os_lock_nospin_t l
)
893 return _os_nospin_lock_lock(&l
->osl_nospin_lock
);
897 _os_lock_nospin_trylock(_os_lock_nospin_t l
)
899 return _os_nospin_lock_trylock(&l
->osl_nospin_lock
);
903 _os_lock_nospin_unlock(_os_lock_nospin_t l
)
905 return _os_nospin_lock_unlock(&l
->osl_nospin_lock
);
910 #pragma mark os_once_t
912 typedef struct os_once_gate_s
{
914 os_ulock_value_t ogo_lock
;
917 } os_once_gate_s
, *os_once_gate_t
;
919 #define OS_ONCE_INIT ((uintptr_t)0l)
920 #define OS_ONCE_DONE (~(uintptr_t)0l)
922 #if defined(__i386__) || defined(__x86_64__)
923 #define OS_ONCE_USE_QUIESCENT_COUNTER 0
925 #define OS_ONCE_USE_QUIESCENT_COUNTER 1
928 OS_ATOMIC_EXPORT
void _os_once(os_once_t
*val
, void *ctxt
, os_function_t func
);
929 OS_ATOMIC_EXPORT
void __os_once_reset(os_once_t
*val
);
931 OS_NOINLINE OS_NORETURN OS_COLD
932 void _os_once_gate_recursive_abort(os_lock_owner_t owner
);
933 OS_NOINLINE OS_NORETURN OS_COLD
934 void _os_once_gate_unowned_abort(os_lock_owner_t owner
);
935 OS_NOINLINE OS_NORETURN OS_COLD
936 void _os_once_gate_corruption_abort(os_ulock_value_t current
);
939 OS_NOINLINE OS_NORETURN OS_COLD
941 _os_once_gate_recursive_abort(os_lock_owner_t owner
)
943 __LIBPLATFORM_CLIENT_CRASH__(owner
, "Trying to recursively lock an "
947 OS_NOINLINE OS_NORETURN OS_COLD
949 _os_once_gate_unowned_abort(os_lock_owner_t owner
)
951 __LIBPLATFORM_CLIENT_CRASH__(owner
, "Unlock of an os_once_t not "
952 "owned by current thread");
955 OS_NOINLINE OS_NORETURN OS_COLD
957 _os_once_gate_corruption_abort(os_ulock_value_t current
)
959 __LIBPLATFORM_CLIENT_CRASH__(current
, "os_once_t is corrupt");
963 #if OS_ONCE_USE_QUIESCENT_COUNTER
964 #define OS_ONCE_MAKE_GEN(gen) (((gen) << 2) + OS_ULOCK_NOWAITERS_BIT)
965 #define OS_ONCE_IS_GEN(gen) (((gen) & 3) == OS_ULOCK_NOWAITERS_BIT)
967 // the _COMM_PAGE_CPU_QUIESCENT_COUNTER value is incremented every time
968 // all CPUs have performed a context switch.
970 // To make sure all CPUs context switched at least once since `gen`,
971 // we need to observe 4 increments, see libdispatch/src/shims/lock.h
972 #define OS_ONCE_GEN_SAFE_DELTA (4 << 2)
975 static inline uintptr_t
976 _os_once_generation(void)
978 uintptr_t value
= *(volatile uintptr_t *)_COMM_PAGE_CPU_QUIESCENT_COUNTER
;
979 return OS_ONCE_MAKE_GEN(value
);
983 static inline uintptr_t
984 _os_once_mark_quiescing(os_once_gate_t og
)
986 return os_atomic_xchg(&og
->ogo_once
, _os_once_generation(), release
);
991 _os_once_mark_done_if_quiesced(os_once_gate_t og
, uintptr_t gen
)
993 if (_os_once_generation() - gen
>= OS_ONCE_GEN_SAFE_DELTA
) {
994 os_atomic_store(&og
->ogo_once
, OS_ONCE_DONE
, relaxed
);
999 static inline uintptr_t
1000 _os_once_mark_done(os_once_gate_t og
)
1002 return os_atomic_xchg(&og
->ogo_once
, OS_ONCE_DONE
, release
);
1008 _os_once_gate_broadcast(os_once_gate_t og
, os_ulock_value_t current
,
1009 os_lock_owner_t self
)
1011 if (unlikely(OS_ULOCK_IS_NOT_OWNER(current
, self
, 0))) {
1012 return _os_once_gate_unowned_abort(OS_ULOCK_OWNER(current
));
1014 if (current
& OS_ULOCK_NOWAITERS_BIT
) {
1015 __LIBPLATFORM_INTERNAL_CRASH__(current
, "unlock_slow with no waiters");
1018 int ret
= __ulock_wake(UL_UNFAIR_LOCK
| ULF_NO_ERRNO
| ULF_WAKE_ALL
,
1020 if (unlikely(ret
< 0)) {
1027 __LIBPLATFORM_INTERNAL_CRASH__(-ret
, "ulock_wake failure");
1036 _os_once_callout(os_once_gate_t og
, void *ctxt
, os_function_t func
,
1037 os_lock_owner_t self
)
1043 #if OS_ONCE_USE_QUIESCENT_COUNTER
1044 v
= _os_once_mark_quiescing(og
);
1046 v
= _os_once_mark_done(og
);
1048 if (likely((os_ulock_value_t
)v
== self
)) return;
1049 _os_once_gate_broadcast(og
, (os_ulock_value_t
)v
, self
);
1054 _os_once_gate_wait(os_once_gate_t og
, void *ctxt
, os_function_t func
,
1055 os_lock_owner_t self
)
1060 os_atomic_rmw_loop(&og
->ogo_once
, old
, new, relaxed
, {
1061 if (old
== OS_ONCE_DONE
) {
1062 os_atomic_rmw_loop_give_up(return);
1063 #if OS_ONCE_USE_QUIESCENT_COUNTER
1064 } else if (OS_ONCE_IS_GEN(old
)) {
1065 os_atomic_rmw_loop_give_up({
1066 os_atomic_thread_fence(acquire
);
1067 return _os_once_mark_done_if_quiesced(og
, old
);
1070 } else if (old
== OS_ONCE_INIT
) {
1071 // __os_once_reset was used, try to become the new initializer
1072 new = (uintptr_t)self
;
1074 new = old
& ~(uintptr_t)OS_ULOCK_NOWAITERS_BIT
;
1075 if (new == old
) os_atomic_rmw_loop_give_up(break);
1078 if (old
== OS_ONCE_INIT
) {
1079 // see comment in _os_once, pairs with the release barrier
1080 // in __os_once_reset()
1081 os_atomic_thread_fence(acquire
);
1082 return _os_once_callout(og
, ctxt
, func
, self
);
1084 if (unlikely(OS_ULOCK_IS_OWNER((os_lock_owner_t
)old
, self
, 0))) {
1085 return _os_once_gate_recursive_abort(self
);
1087 int ret
= __ulock_wait(UL_UNFAIR_LOCK
| ULF_NO_ERRNO
,
1088 &og
->ogo_lock
, (os_ulock_value_t
)new, 0);
1089 if (unlikely(ret
< 0)) {
1095 _os_once_gate_corruption_abort((os_lock_owner_t
)old
);
1098 __LIBPLATFORM_INTERNAL_CRASH__(-ret
, "ulock_wait failure");
1104 // Atomically resets the once value to zero and then signals all
1105 // pending waiters to return from their __ulock_wait()
1107 __os_once_reset(os_once_t
*val
)
1109 os_once_gate_t og
= (os_once_gate_t
)val
;
1110 os_lock_owner_t self
= _os_lock_owner_get_self();
1113 v
= os_atomic_xchg(&og
->ogo_once
, OS_ONCE_INIT
, release
);
1114 if (likely((os_ulock_value_t
)v
== self
)) return;
1115 return _os_once_gate_broadcast(og
, (os_ulock_value_t
)v
, self
);
1119 _os_once(os_once_t
*val
, void *ctxt
, os_function_t func
)
1121 os_once_gate_t og
= (os_once_gate_t
)val
;
1122 os_lock_owner_t self
;
1125 #if OS_ONCE_USE_QUIESCENT_COUNTER
1126 v
= os_atomic_load(&og
->ogo_once
, acquire
);
1127 if (likely(OS_ONCE_IS_GEN(v
))) {
1128 return _os_once_mark_done_if_quiesced(og
, v
);
1132 self
= _os_lock_owner_get_self();
1133 v
= (uintptr_t)self
;
1135 // The acquire barrier pairs with the release in __os_once_reset()
1136 // for cases when a previous initializer failed.
1137 if (likely(os_atomic_cmpxchg(&og
->ogo_once
, OS_ONCE_INIT
, v
, acquire
))) {
1138 return _os_once_callout(og
, ctxt
, func
, self
);
1140 return _os_once_gate_wait(og
, ctxt
, func
, self
);
1145 #pragma mark os_lock_eliding_t
1147 #if !TARGET_OS_IPHONE
1149 #define _os_lock_eliding_t _os_lock_spin_t
1150 #define _os_lock_eliding_lock _os_lock_spin_lock
1151 #define _os_lock_eliding_trylock _os_lock_spin_trylock
1152 #define _os_lock_eliding_unlock _os_lock_spin_unlock
1153 OS_LOCK_METHODS_DECL(eliding
);
1154 OS_LOCK_TYPE_INSTANCE(eliding
);
1157 #pragma mark os_lock_transactional_t
1159 OS_LOCK_STRUCT_DECL_INTERNAL(transactional
,
1160 uintptr_t volatile osl_lock
;
1163 #define _os_lock_transactional_t _os_lock_eliding_t
1164 #define _os_lock_transactional_lock _os_lock_eliding_lock
1165 #define _os_lock_transactional_trylock _os_lock_eliding_trylock
1166 #define _os_lock_transactional_unlock _os_lock_eliding_unlock
1167 OS_LOCK_METHODS_DECL(transactional
);
1168 OS_LOCK_TYPE_INSTANCE(transactional
);
1170 #endif // !TARGET_OS_IPHONE