-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlock.c
1507 lines (1295 loc) · 39.5 KB
/
lock.c
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
#define OS_UNFAIR_LOCK_INLINE 1
#include "lock_internal.h"
#include "os/internal.h"
#include "resolver.h"
#include "libkern/OSAtomic.h"
#include "os/lock.h"
#include "os/lock_private.h"
#include "os/once_private.h"
#include <mach/mach_init.h>
#include <mach/mach_traps.h>
#include <mach/thread_switch.h>
#include <mach/mach_time.h>
#include <os/tsd.h>
#include <os/os_sync_wait_on_address.h>
#pragma mark -
#pragma mark _os_lock_base_t
OS_NOINLINE OS_NORETURN OS_COLD
void _os_lock_corruption_abort(void *lock_ptr OS_UNUSED, uintptr_t lock_value);
OS_LOCK_STRUCT_DECL_INTERNAL(base);
OS_USED static OS_LOCK_TYPE_STRUCT_DECL(base);
void
os_lock_lock(os_lock_t l)
{
return l._osl_base->osl_type->osl_lock(l);
}
bool
os_lock_trylock(os_lock_t l)
{
return l._osl_base->osl_type->osl_trylock(l);
}
void
os_lock_unlock(os_lock_t l)
{
return l._osl_base->osl_type->osl_unlock(l);
}
OS_NOINLINE OS_NORETURN OS_COLD
void
_os_lock_corruption_abort(void *lock_ptr OS_UNUSED, uintptr_t lock_value)
{
__LIBPLATFORM_CLIENT_CRASH__(lock_value, "os_lock is corrupt");
}
#pragma mark -
#pragma mark OSSpinLock
OS_ATOMIC_EXPORT OS_NOINLINE void _OSSpinLockLockSlow(volatile OSSpinLock *l);
OS_ATOMIC_EXPORT void OSSpinLockLock(volatile OSSpinLock *l);
OS_ATOMIC_EXPORT bool OSSpinLockTry(volatile OSSpinLock *l);
OS_ATOMIC_EXPORT int spin_lock_try(volatile OSSpinLock *l);
OS_ATOMIC_EXPORT void OSSpinLockUnlock(volatile OSSpinLock *l);
#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
static const OSSpinLock _OSSpinLockLocked = 1;
#else
static const OSSpinLock _OSSpinLockLocked = -1;
#endif
#if OS_ATOMIC_UP
// Don't spin on UP
#elif defined(__arm__) || defined(__arm64__)
#define OS_LOCK_SPIN_SPIN_TRIES 100
#define OS_LOCK_SPIN_PAUSE() os_hardware_wfe()
#else
#define OS_LOCK_SPIN_SPIN_TRIES 1000
#define OS_LOCK_SPIN_PAUSE() os_hardware_pause()
#endif
OS_ALWAYS_INLINE
static uint64_t
_os_lock_yield_deadline(mach_msg_timeout_t timeout)
{
uint64_t abstime = timeout;
#if defined(__arm__)
// some armv7 targets do not have div, like the armv7 arch
// so hardcode the most typical clock resolution it has
// as we don't really need accuracy here anyway
abstime *= NSEC_PER_MSEC * 128 / 3;
#elif defined(__i386__) || defined(__x86_64__)
// abstime is in nanoseconds
#else
mach_timebase_info_data_t tbi;
kern_return_t kr = mach_timebase_info(&tbi);
if (kr) return UINT64_MAX;
abstime *= (NSEC_PER_MSEC * tbi.denom / tbi.numer);
#endif
return mach_absolute_time() + abstime;
}
OS_ALWAYS_INLINE
static bool
_os_lock_yield_until(uint64_t deadline)
{
return mach_absolute_time() < deadline;
}
OS_NOINLINE
static void
_OSSpinLockLockYield(volatile OSSpinLock *l)
{
int option = SWITCH_OPTION_DEPRESS;
mach_msg_timeout_t timeout = 1;
uint64_t deadline = _os_lock_yield_deadline(timeout);
OSSpinLock lock;
while (unlikely(lock = *l)) {
_yield:
if (unlikely(lock != _OSSpinLockLocked)) {
_os_lock_corruption_abort((void *)l, (uintptr_t)lock);
}
thread_switch(MACH_PORT_NULL, option, timeout);
if (option == SWITCH_OPTION_WAIT) {
timeout++;
} else if (!_os_lock_yield_until(deadline)) {
option = SWITCH_OPTION_WAIT;
}
}
bool r = os_atomic_cmpxchgv(l, 0, _OSSpinLockLocked, &lock, acquire);
if (likely(r)) return;
goto _yield;
}
#if OS_ATOMIC_UP
void
_OSSpinLockLockSlow(volatile OSSpinLock *l)
{
return _OSSpinLockLockYield(l); // Don't spin on UP
}
#elif defined(__arm64__)
// Exclusive monitor must be held during WFE <rdar://problem/22300054>
#if defined(__ARM_ARCH_8_2__)
void
_OSSpinLockLockSlow(volatile OSSpinLock *l)
{
uint32_t tries = OS_LOCK_SPIN_SPIN_TRIES;
OSSpinLock lock;
_spin:
while (unlikely(lock = os_atomic_load_exclusive(l, relaxed))) {
if (unlikely(lock != _OSSpinLockLocked)) {
os_atomic_clear_exclusive();
return _os_lock_corruption_abort((void *)l, (uintptr_t)lock);
}
if (unlikely(!tries--)) {
os_atomic_clear_exclusive();
return _OSSpinLockLockYield(l);
}
OS_LOCK_SPIN_PAUSE();
}
os_atomic_clear_exclusive();
bool r = os_atomic_cmpxchg(l, 0, _OSSpinLockLocked, acquire);
if (likely(r)) return;
goto _spin;
}
#else // !__ARM_ARCH_8_2__
void
_OSSpinLockLockSlow(volatile OSSpinLock *l)
{
uint32_t tries = OS_LOCK_SPIN_SPIN_TRIES;
OSSpinLock lock;
os_atomic_rmw_loop(l, lock, _OSSpinLockLocked, acquire, if (unlikely(lock)){
if (unlikely(lock != _OSSpinLockLocked)) {
os_atomic_rmw_loop_give_up(return
_os_lock_corruption_abort((void *)l, (uintptr_t)lock));
}
if (unlikely(!tries--)) {
os_atomic_rmw_loop_give_up(return _OSSpinLockLockYield(l));
}
OS_LOCK_SPIN_PAUSE();
continue;
});
}
#endif // !__ARM_ARCH_8_2__
#else // !OS_ATOMIC_UP
void
_OSSpinLockLockSlow(volatile OSSpinLock *l)
{
uint32_t tries = OS_LOCK_SPIN_SPIN_TRIES;
OSSpinLock lock;
while (unlikely(lock = *l)) {
_spin:
if (unlikely(lock != _OSSpinLockLocked)) {
return _os_lock_corruption_abort((void *)l, (uintptr_t)lock);
}
if (unlikely(!tries--)) return _OSSpinLockLockYield(l);
OS_LOCK_SPIN_PAUSE();
}
bool r = os_atomic_cmpxchgv(l, 0, _OSSpinLockLocked, &lock, acquire);
if (likely(r)) return;
goto _spin;
}
#endif // !OS_ATOMIC_UP
#if OS_LOCK_OSSPINLOCK_IS_NOSPINLOCK && !TARGET_OS_SIMULATOR
typedef struct _os_nospin_lock_s *_os_nospin_lock_t;
OS_ATOMIC_EXPORT void _os_nospin_lock_lock(_os_nospin_lock_t lock);
OS_ATOMIC_EXPORT bool _os_nospin_lock_trylock(_os_nospin_lock_t lock);
OS_ATOMIC_EXPORT void _os_nospin_lock_unlock(_os_nospin_lock_t lock);
void
OSSpinLockLock(volatile OSSpinLock *l)
{
OS_ATOMIC_ALIAS(spin_lock, OSSpinLockLock);
OS_ATOMIC_ALIAS(_spin_lock, OSSpinLockLock);
return _os_nospin_lock_lock((_os_nospin_lock_t)l);
}
bool
OSSpinLockTry(volatile OSSpinLock *l)
{
return _os_nospin_lock_trylock((_os_nospin_lock_t)l);
}
int
spin_lock_try(volatile OSSpinLock *l)
{
OS_ATOMIC_ALIAS(_spin_lock_try, spin_lock_try);
return _os_nospin_lock_trylock((_os_nospin_lock_t)l);
}
void
OSSpinLockUnlock(volatile OSSpinLock *l)
{
OS_ATOMIC_ALIAS(spin_unlock, OSSpinLockUnlock);
OS_ATOMIC_ALIAS(_spin_unlock, OSSpinLockUnlock);
return _os_nospin_lock_unlock((_os_nospin_lock_t)l);
}
#undef OS_ATOMIC_ALIAS
#define OS_ATOMIC_ALIAS(n, o)
static void _OSSpinLockLock(volatile OSSpinLock *l);
#undef OSSpinLockLock
#define OSSpinLockLock _OSSpinLockLock
static bool _OSSpinLockTry(volatile OSSpinLock *l);
#undef OSSpinLockTry
#define OSSpinLockTry _OSSpinLockTry
static __unused int __spin_lock_try(volatile OSSpinLock *l);
#undef spin_lock_try
#define spin_lock_try __spin_lock_try
static void _OSSpinLockUnlock(volatile OSSpinLock *l);
#undef OSSpinLockUnlock
#define OSSpinLockUnlock _OSSpinLockUnlock
#endif // OS_LOCK_OSSPINLOCK_IS_NOSPINLOCK
void
OSSpinLockLock(volatile OSSpinLock *l)
{
OS_ATOMIC_ALIAS(spin_lock, OSSpinLockLock);
OS_ATOMIC_ALIAS(_spin_lock, OSSpinLockLock);
bool r = os_atomic_cmpxchg(l, 0, _OSSpinLockLocked, acquire);
if (likely(r)) return;
return _OSSpinLockLockSlow(l);
}
bool
OSSpinLockTry(volatile OSSpinLock *l)
{
bool r = os_atomic_cmpxchg(l, 0, _OSSpinLockLocked, acquire);
return r;
}
int
spin_lock_try(volatile OSSpinLock *l) // <rdar://problem/13316060>
{
OS_ATOMIC_ALIAS(_spin_lock_try, spin_lock_try);
return OSSpinLockTry(l);
}
void
OSSpinLockUnlock(volatile OSSpinLock *l)
{
OS_ATOMIC_ALIAS(spin_unlock, OSSpinLockUnlock);
OS_ATOMIC_ALIAS(_spin_unlock, OSSpinLockUnlock);
os_atomic_store(l, 0, release);
}
#pragma mark -
#pragma mark os_lock_spin_t
OS_LOCK_STRUCT_DECL_INTERNAL(spin,
OSSpinLock volatile osl_spinlock;
);
OS_LOCK_METHODS_DECL(spin);
OS_LOCK_TYPE_INSTANCE(spin);
void
_os_lock_spin_lock(_os_lock_spin_t l)
{
return OSSpinLockLock(&l->osl_spinlock);
}
bool
_os_lock_spin_trylock(_os_lock_spin_t l)
{
return OSSpinLockTry(&l->osl_spinlock);
}
void
_os_lock_spin_unlock(_os_lock_spin_t l)
{
return OSSpinLockUnlock(&l->osl_spinlock);
}
#pragma mark -
#pragma mark os_lock_owner_t
#ifndef __TSD_MACH_THREAD_SELF
#define __TSD_MACH_THREAD_SELF 3
#endif
typedef mach_port_name_t os_lock_owner_t;
#define OS_LOCK_NO_OWNER MACH_PORT_NULL
OS_ALWAYS_INLINE OS_CONST
static inline os_lock_owner_t
_os_lock_owner_get_self(void)
{
os_lock_owner_t self;
self = (os_lock_owner_t)_os_tsd_get_direct(__TSD_MACH_THREAD_SELF);
return self;
}
OS_NOINLINE OS_NORETURN OS_COLD
static void
_os_lock_recursive_abort(os_lock_owner_t owner)
{
__LIBPLATFORM_CLIENT_CRASH__(owner, "Trying to recursively lock an "
"os_lock");
}
#pragma mark -
#pragma mark os_lock_handoff_t
OS_LOCK_STRUCT_DECL_INTERNAL(handoff,
os_lock_owner_t volatile osl_owner;
);
OS_LOCK_METHODS_DECL(handoff);
OS_LOCK_TYPE_INSTANCE(handoff);
#define OS_LOCK_HANDOFF_YIELD_TRIES 100
OS_NOINLINE
static void
_os_lock_handoff_lock_slow(_os_lock_handoff_t l)
{
int option = SWITCH_OPTION_OSLOCK_DEPRESS;
mach_msg_timeout_t timeout = 1;
uint32_t tries = OS_LOCK_HANDOFF_YIELD_TRIES;
os_lock_owner_t self = _os_lock_owner_get_self(), owner;
while (unlikely(owner = l->osl_owner)) {
_handoff:
if (unlikely(owner == self)) return _os_lock_recursive_abort(self);
// Yield until tries first hits zero, then permanently switch to wait
if (unlikely(!tries--)) option = SWITCH_OPTION_OSLOCK_WAIT;
thread_switch(owner, option, timeout);
// Redrive the handoff every 1ms until switching to wait
if (option == SWITCH_OPTION_OSLOCK_WAIT) timeout++;
}
bool r = os_atomic_cmpxchgv(&l->osl_owner, MACH_PORT_NULL, self, &owner,
acquire);
if (likely(r)) return;
goto _handoff;
}
void
_os_lock_handoff_lock(_os_lock_handoff_t l)
{
os_lock_owner_t self = _os_lock_owner_get_self();
bool r = os_atomic_cmpxchg(&l->osl_owner, MACH_PORT_NULL, self, acquire);
if (likely(r)) return;
return _os_lock_handoff_lock_slow(l);
}
bool
_os_lock_handoff_trylock(_os_lock_handoff_t l)
{
os_lock_owner_t self = _os_lock_owner_get_self();
bool r = os_atomic_cmpxchg(&l->osl_owner, MACH_PORT_NULL, self, acquire);
return r;
}
void
_os_lock_handoff_unlock(_os_lock_handoff_t l)
{
os_atomic_store(&l->osl_owner, MACH_PORT_NULL, release);
}
#pragma mark -
#pragma mark os_ulock_value_t
#include <sys/errno.h>
#include <sys/ulock.h>
typedef os_lock_owner_t os_ulock_value_t;
// This assumes that all thread mach port values always have the low bit set!
// Clearing this bit is used to communicate the existence of waiters to unlock.
#define OS_ULOCK_NOWAITERS_BIT ((os_ulock_value_t)1u)
#define OS_ULOCK_OWNER(value) ((value) | OS_ULOCK_NOWAITERS_BIT)
#define OS_ULOCK_ANONYMOUS_OWNER MACH_PORT_DEAD
#define OS_ULOCK_IS_OWNER(value, self, allow_anonymous_owner) ({ \
os_lock_owner_t _owner = OS_ULOCK_OWNER(value); (_owner == (self)) && \
(!(allow_anonymous_owner) || _owner != OS_ULOCK_ANONYMOUS_OWNER); })
#define OS_ULOCK_IS_NOT_OWNER(value, self, allow_anonymous_owner) ({ \
os_lock_owner_t _owner = OS_ULOCK_OWNER(value); (_owner != (self)) && \
(!(allow_anonymous_owner) || _owner != OS_ULOCK_ANONYMOUS_OWNER); })
#pragma mark -
#pragma mark os_unfair_lock
typedef struct _os_unfair_lock_s {
os_ulock_value_t oul_value;
} *_os_unfair_lock_t;
_Static_assert(sizeof(struct os_unfair_lock_s) ==
sizeof(struct _os_unfair_lock_s), "os_unfair_lock size mismatch");
OS_ATOMIC_EXPORT void os_unfair_lock_lock(os_unfair_lock_t lock);
OS_ATOMIC_EXPORT void os_unfair_lock_lock_with_options(os_unfair_lock_t lock,
os_unfair_lock_options_t options);
OS_ATOMIC_EXPORT bool os_unfair_lock_trylock(os_unfair_lock_t lock);
OS_ATOMIC_EXPORT void os_unfair_lock_unlock(os_unfair_lock_t lock);
OS_ATOMIC_EXPORT void os_unfair_lock_lock_no_tsd(os_unfair_lock_t lock,
os_unfair_lock_options_t options, mach_port_t mts);
OS_ATOMIC_EXPORT void os_unfair_lock_unlock_no_tsd(os_unfair_lock_t lock,
mach_port_t mts);
OS_NOINLINE OS_NORETURN OS_COLD
void _os_unfair_lock_recursive_abort(os_lock_owner_t owner);
OS_NOINLINE OS_NORETURN OS_COLD
void _os_unfair_lock_unowned_abort(os_lock_owner_t owner);
OS_NOINLINE OS_NORETURN OS_COLD
void _os_unfair_lock_corruption_abort(os_ulock_value_t current);
_Static_assert(OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION ==
ULF_WAIT_WORKQ_DATA_CONTENTION,
"check value for OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION");
_Static_assert(OS_UNFAIR_LOCK_ADAPTIVE_SPIN ==
ULF_WAIT_ADAPTIVE_SPIN,
"check value for OS_UNFAIR_LOCK_ADAPTIVE_SPIN");
_Static_assert(OS_UNFAIR_LOCK_DEADLINE == ULF_DEADLINE,
"check value for OS_UNFAIR_LOCK_DEADLINE");
#define OS_UNFAIR_LOCK_OPTIONS_MASK \
(os_unfair_lock_options_t)(OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION | \
OS_UNFAIR_LOCK_ADAPTIVE_SPIN | OS_UNFAIR_LOCK_DEADLINE)
#define OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER 0x01000000u
OS_NOINLINE OS_NORETURN OS_COLD
void
_os_unfair_lock_recursive_abort(os_lock_owner_t owner)
{
__LIBPLATFORM_CLIENT_CRASH__(owner, "Trying to recursively lock an "
"os_unfair_lock");
}
OS_NOINLINE OS_NORETURN OS_COLD
void
_os_unfair_lock_unowned_abort(os_lock_owner_t owner)
{
__LIBPLATFORM_CLIENT_CRASH__(owner, "Unlock of an os_unfair_lock not "
"owned by current thread");
}
OS_NOINLINE OS_NORETURN OS_COLD
void
_os_unfair_lock_corruption_abort(os_ulock_value_t current)
{
if (current >= 0x200 && current <= 0x40000) {
/*
* 0x103 tends to be mach_task_self()
* and up to 0x400 ports (512) is pretty common.
* so it might be a thread leaving without unlocking
*/
__LIBPLATFORM_CLIENT_CRASH__(current,
"os_unfair_lock is corrupt, "
"or owner thread exited without unlocking");
} else {
__LIBPLATFORM_CLIENT_CRASH__(current, "os_unfair_lock is corrupt");
}
}
#if defined(__i386__) || defined(__x86_64__)
#define _os_lock_deadline_to_duration(deadline_abs) deadline_abs
#define _os_lock_duration_to_deadline(timeout_ns) timeout_ns
#elif defined(__arm__)
#define _os_lock_deadline_to_duration(deadline_abs) ({ \
uint64_t _delta_abs = deadline_abs - mach_absolute_time(); \
_delta_abs * 128 / 3; \
})
#define _os_lock_duration_to_deadline(timeout_ns) ({ \
mach_absolute_time() + (timeout_ns * 3 / 128);
})
#else
#define _os_lock_deadline_to_duration(deadline_abs) ({ \
mach_timebase_info_data_t tbi; \
mach_timebase_info(&tbi); \
uint64_t _delta_abs = deadline_abs - mach_absolute_time(); \
_delta_abs * tbi.numer / tbi.denom; \
})
#define _os_lock_duration_to_deadline(timeout_ns) ({ \
mach_timebase_info_data_t tbi; \
mach_timebase_info(&tbi); \
uint64_t abstime = timeout_ns * tbi.denom / tbi.numer; \
mach_absolute_time() + abstime; \
})
#endif
OS_NOINLINE
static bool
_os_unfair_lock_lock_slow(_os_unfair_lock_t l,
os_unfair_lock_options_t options, uint64_t duration, os_lock_owner_t self)
{
os_unfair_lock_options_t allow_anonymous_owner =
options & OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER;
options &= ~OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER;
if (unlikely(options & ~OS_UNFAIR_LOCK_OPTIONS_MASK)) {
__LIBPLATFORM_CLIENT_CRASH__(options, "Invalid options");
}
os_ulock_value_t current, new, waiters_mask = 0;
while (unlikely((current = os_atomic_load(&l->oul_value, relaxed)) !=
OS_LOCK_NO_OWNER)) {
_retry:
if (unlikely(OS_ULOCK_IS_OWNER(current, self, allow_anonymous_owner))) {
_os_unfair_lock_recursive_abort(self);
}
new = current & ~OS_ULOCK_NOWAITERS_BIT;
if (current != new) {
// Clear nowaiters bit in lock value before waiting
if (!os_atomic_cmpxchgv(&l->oul_value, current, new, ¤t,
relaxed)){
continue;
}
current = new;
}
int ret = __ulock_wait2(UL_UNFAIR_LOCK | ULF_NO_ERRNO | options,
l, current, duration, 0);
if (unlikely(ret < 0)) {
switch (-ret) {
case EINTR:
case EFAULT:
continue;
case EOWNERDEAD:
_os_unfair_lock_corruption_abort(current);
break;
case ETIMEDOUT:
return false;
default:
__LIBPLATFORM_INTERNAL_CRASH__(-ret, "ulock_wait2 failure");
}
}
if (ret > 0) {
// If there are more waiters, unset nowaiters bit when acquiring lock
waiters_mask = OS_ULOCK_NOWAITERS_BIT;
}
}
new = self & ~waiters_mask;
bool r = os_atomic_cmpxchgv(&l->oul_value, OS_LOCK_NO_OWNER, new,
¤t, acquire);
if (unlikely(!r)) goto _retry;
return true; // Successfully acquired
}
OS_NOINLINE
static void
_os_unfair_lock_unlock_slow(_os_unfair_lock_t l, os_lock_owner_t self,
os_ulock_value_t current, os_unfair_lock_options_t options)
{
os_unfair_lock_options_t allow_anonymous_owner =
options & OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER;
options &= ~OS_UNFAIR_LOCK_ALLOW_ANONYMOUS_OWNER;
if (unlikely(OS_ULOCK_IS_NOT_OWNER(current, self, allow_anonymous_owner))) {
return _os_unfair_lock_unowned_abort(OS_ULOCK_OWNER(current));
}
if (current & OS_ULOCK_NOWAITERS_BIT) {
__LIBPLATFORM_INTERNAL_CRASH__(current, "unlock_slow with no waiters");
}
for (;;) {
int ret = __ulock_wake(UL_UNFAIR_LOCK | ULF_NO_ERRNO, l, 0);
if (unlikely(ret < 0)) {
switch (-ret) {
case EINTR:
continue;
case ENOENT:
break;
default:
__LIBPLATFORM_INTERNAL_CRASH__(-ret, "ulock_wake failure");
}
}
break;
}
}
void
os_unfair_lock_lock(os_unfair_lock_t lock)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
os_lock_owner_t self = _os_lock_owner_get_self();
bool r = os_atomic_cmpxchg(&l->oul_value, OS_LOCK_NO_OWNER, self, acquire);
if (likely(r)) return;
return (void) _os_unfair_lock_lock_slow(l, OS_UNFAIR_LOCK_NONE, 0, self);
}
void
os_unfair_lock_lock_with_options(os_unfair_lock_t lock,
os_unfair_lock_options_t options)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
os_lock_owner_t self = _os_lock_owner_get_self();
bool r = os_atomic_cmpxchg(&l->oul_value, OS_LOCK_NO_OWNER, self, acquire);
if (likely(r)) return;
return (void) _os_unfair_lock_lock_slow(l, options, 0, self);
}
bool
os_unfair_lock_trylock(os_unfair_lock_t lock)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
os_lock_owner_t self = _os_lock_owner_get_self();
bool r = os_atomic_cmpxchg(&l->oul_value, OS_LOCK_NO_OWNER, self, acquire);
return r;
}
OS_ALWAYS_INLINE
static bool
_os_unfair_lock_trylock_with_options_deadline_inline(_os_unfair_lock_t l,
os_unfair_lock_options_t options, uint64_t timeout, os_lock_owner_t self)
{
// Until the chroot moves to Sunburst, handle any deadlines as timeouts.
return _os_unfair_lock_lock_slow(l, options, timeout, self);
}
static bool
_os_unfair_lock_trylock_with_options_deadline(_os_unfair_lock_t l,
os_unfair_lock_options_t options, uint64_t timeout, os_lock_owner_t self)
{
if (timeout < mach_absolute_time()) return false;
return _os_unfair_lock_trylock_with_options_deadline_inline(l, options, timeout, self);
}
static bool
_os_unfair_lock_trylock_with_options_duration(_os_unfair_lock_t l,
os_unfair_lock_options_t options, uint64_t timeout_ns, os_lock_owner_t self)
{
uint64_t time_to_wait;
time_to_wait = _os_lock_duration_to_deadline(timeout_ns);
options |= OS_UNFAIR_LOCK_DEADLINE;
return _os_unfair_lock_trylock_with_options_deadline_inline(l, options, time_to_wait, self);
}
static bool
_os_unfair_lock_trylock_with_options_slow(_os_unfair_lock_t l,
os_unfair_lock_options_t options, uint64_t timeout, os_lock_owner_t self)
{
if (!(options & OS_UNFAIR_LOCK_DEADLINE)) {
if (timeout == 0) return false;
return _os_unfair_lock_trylock_with_options_duration(l, options, timeout, self);
}
return _os_unfair_lock_trylock_with_options_deadline(l, options, timeout, self);
}
bool
os_unfair_lock_trylock_with_options(os_unfair_lock_t lock,
os_unfair_lock_options_t options, uint64_t timeout)
{
_os_unfair_lock_t l = (_os_unfair_lock_t) lock;
os_lock_owner_t self = _os_lock_owner_get_self();
bool r = os_atomic_cmpxchg(&l->oul_value, OS_LOCK_NO_OWNER, self, acquire);
if (likely(r)) return r;
return _os_unfair_lock_trylock_with_options_slow(l, options, timeout, self);
}
void
os_unfair_lock_unlock(os_unfair_lock_t lock)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
os_lock_owner_t self = _os_lock_owner_get_self();
os_ulock_value_t current;
current = os_atomic_xchg(&l->oul_value, OS_LOCK_NO_OWNER, release);
if (likely(current == self)) return;
return _os_unfair_lock_unlock_slow(l, self, current, 0);
}
void
os_unfair_lock_lock_no_tsd(os_unfair_lock_t lock,
os_unfair_lock_options_t options, mach_port_t self)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
bool r = os_atomic_cmpxchg(&l->oul_value, OS_LOCK_NO_OWNER, self, acquire);
if (likely(r)) return;
return (void) _os_unfair_lock_lock_slow(l, options, 0, self);
}
void
os_unfair_lock_unlock_no_tsd(os_unfair_lock_t lock, mach_port_t self)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
os_ulock_value_t current;
current = os_atomic_xchg(&l->oul_value, OS_LOCK_NO_OWNER, release);
if (likely(current == self)) return;
return _os_unfair_lock_unlock_slow(l, self, current, 0);
}
void
os_unfair_lock_assert_owner(const os_unfair_lock *lock)
{
const struct _os_unfair_lock_s *l = (const struct _os_unfair_lock_s *)lock;
os_lock_owner_t self = _os_lock_owner_get_self();
os_ulock_value_t current = os_atomic_load(&l->oul_value, relaxed);
if (unlikely(OS_ULOCK_IS_NOT_OWNER(current, self, 0))) {
__LIBPLATFORM_CLIENT_CRASH__(current, "Assertion failed: "
"Lock unexpectedly not owned by current thread");
}
}
void
os_unfair_lock_assert_not_owner(const os_unfair_lock *lock)
{
const struct _os_unfair_lock_s *l = (const struct _os_unfair_lock_s *)lock;
os_lock_owner_t self = _os_lock_owner_get_self();
os_ulock_value_t current = os_atomic_load(&l->oul_value, relaxed);
if (unlikely(OS_ULOCK_IS_OWNER(current, self, 0))) {
__LIBPLATFORM_CLIENT_CRASH__(current, "Assertion failed: "
"Lock unexpectedly owned by current thread");
}
}
#pragma mark -
#pragma mark os_unfair_recursive_lock
OS_ATOMIC_EXPORT
void os_unfair_recursive_lock_lock_with_options(os_unfair_recursive_lock_t lock,
os_unfair_lock_options_t options);
OS_ATOMIC_EXPORT
bool os_unfair_recursive_lock_trylock(os_unfair_recursive_lock_t lock);
OS_ATOMIC_EXPORT
void os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock);
OS_ATOMIC_EXPORT
bool os_unfair_recursive_lock_tryunlock4objc(os_unfair_recursive_lock_t lock);
static inline os_lock_owner_t
_os_unfair_lock_owner(os_unfair_lock_t lock)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
return OS_ULOCK_OWNER(os_atomic_load(&l->oul_value, relaxed));
}
bool
os_unfair_recursive_lock_owned(os_unfair_recursive_lock_t lock)
{
return _os_unfair_lock_owner(&lock->ourl_lock) ==
_os_lock_owner_get_self();
}
void
os_unfair_recursive_lock_lock_with_options(os_unfair_recursive_lock_t lock,
os_unfair_lock_options_t options)
{
os_lock_owner_t cur, self = _os_lock_owner_get_self();
_os_unfair_lock_t l = (_os_unfair_lock_t)&lock->ourl_lock;
if (likely(os_atomic_cmpxchgv(&l->oul_value,
OS_LOCK_NO_OWNER, self, &cur, acquire))) {
return;
}
if (OS_ULOCK_OWNER(cur) == self) {
lock->ourl_count++;
return;
}
return (void) _os_unfair_lock_lock_slow(l, options, 0, self);
}
bool
os_unfair_recursive_lock_trylock(os_unfair_recursive_lock_t lock)
{
os_lock_owner_t cur, self = _os_lock_owner_get_self();
_os_unfair_lock_t l = (_os_unfair_lock_t)&lock->ourl_lock;
if (likely(os_atomic_cmpxchgv(&l->oul_value,
OS_LOCK_NO_OWNER, self, &cur, acquire))) {
return true;
}
if (likely(OS_ULOCK_OWNER(cur) == self)) {
lock->ourl_count++;
return true;
}
return false;
}
OS_ALWAYS_INLINE
static inline void
_os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock,
os_lock_owner_t self)
{
if (unlikely(lock->ourl_count)) {
os_lock_owner_t cur = _os_unfair_lock_owner(&lock->ourl_lock);
if (unlikely(cur != self)) {
_os_unfair_lock_unowned_abort(cur);
}
lock->ourl_count--;
return;
}
_os_unfair_lock_t l = (_os_unfair_lock_t)lock;
os_ulock_value_t current;
current = os_atomic_xchg(&l->oul_value, OS_LOCK_NO_OWNER, release);
if (likely(current == self)) return;
return _os_unfair_lock_unlock_slow(l, self, current, 0);
}
void
os_unfair_recursive_lock_unlock(os_unfair_recursive_lock_t lock)
{
os_lock_owner_t self = _os_lock_owner_get_self();
_os_unfair_recursive_lock_unlock(lock, self);
}
bool
os_unfair_recursive_lock_tryunlock4objc(os_unfair_recursive_lock_t lock)
{
os_lock_owner_t cur = _os_unfair_lock_owner(&lock->ourl_lock);
os_lock_owner_t self = _os_lock_owner_get_self();
if (likely(cur == self)) {
_os_unfair_recursive_lock_unlock(lock, self);
return true;
}
return false;
}
void
os_unfair_recursive_lock_unlock_forked_child(os_unfair_recursive_lock_t lock)
{
_os_unfair_lock_t l = (_os_unfair_lock_t)&lock->ourl_lock;
if (os_atomic_load(&l->oul_value, relaxed) == OS_LOCK_NO_OWNER) {
__LIBPLATFORM_CLIENT_CRASH__(0, "Lock was not held");
}
if (lock->ourl_count) {
os_lock_owner_t self = _os_lock_owner_get_self();
lock->ourl_count--;
os_atomic_store(&l->oul_value, self, relaxed);
} else {
os_atomic_store(&l->oul_value, OS_LOCK_NO_OWNER, relaxed);
}
}
#pragma mark -
#pragma mark _os_lock_unfair_t
OS_LOCK_STRUCT_DECL_INTERNAL(unfair,
os_unfair_lock osl_unfair_lock;
);
OS_LOCK_METHODS_DECL(unfair);
OS_LOCK_TYPE_INSTANCE(unfair);
void
_os_lock_unfair_lock(_os_lock_unfair_t l)
{
return os_unfair_lock_lock(&l->osl_unfair_lock);
}
bool
_os_lock_unfair_trylock(_os_lock_unfair_t l)
{
return os_unfair_lock_trylock(&l->osl_unfair_lock);
}
void
_os_lock_unfair_unlock(_os_lock_unfair_t l)
{
return os_unfair_lock_unlock(&l->osl_unfair_lock);
}
#pragma mark -
#pragma mark _os_nospin_lock
typedef struct _os_nospin_lock_s {
os_ulock_value_t oul_value;
} _os_nospin_lock, *_os_nospin_lock_t;
_Static_assert(sizeof(OSSpinLock) ==
sizeof(struct _os_nospin_lock_s), "os_nospin_lock size mismatch");
OS_ATOMIC_EXPORT void _os_nospin_lock_lock(_os_nospin_lock_t lock);
OS_ATOMIC_EXPORT bool _os_nospin_lock_trylock(_os_nospin_lock_t lock);
OS_ATOMIC_EXPORT void _os_nospin_lock_unlock(_os_nospin_lock_t lock);
OS_NOINLINE
static void
_os_nospin_lock_lock_slow(_os_nospin_lock_t l)
{
os_lock_owner_t self = _os_lock_owner_get_self();
os_ulock_value_t current, new, waiters_mask = 0;
uint32_t timeout = 1;
while (unlikely((current = os_atomic_load(&l->oul_value, relaxed)) !=
OS_LOCK_NO_OWNER)) {
_retry:
new = current & ~OS_ULOCK_NOWAITERS_BIT;
// For safer compatibility with OSSpinLock where _OSSpinLockLocked may
// be 1, check that new didn't become 0 (unlocked) by clearing this bit
if (current != new && new) {
// Clear nowaiters bit in lock value before waiting
if (!os_atomic_cmpxchgv(&l->oul_value, current, new, ¤t,
relaxed)){
continue;
}
current = new;
}
int ret = __ulock_wait(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, l, current,
timeout * 1000);
if (unlikely(ret < 0)) {
switch (-ret) {
case ETIMEDOUT:
timeout++;
continue;
case EINTR:
case EFAULT:
continue;
default:
__LIBPLATFORM_INTERNAL_CRASH__(-ret, "ulock_wait failure");
}
}
if (ret > 0) {
// If there are more waiters, unset nowaiters bit when acquiring lock
waiters_mask = OS_ULOCK_NOWAITERS_BIT;
}
}
new = self & ~waiters_mask;