-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathqueue.c
7519 lines (6775 loc) · 232 KB
/
queue.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) 2008-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@
*/
#include "internal.h"
#if HAVE_MACH
#include "protocol.h" // _dispatch_send_wakeup_runloop_thread
#endif
static inline void _dispatch_root_queues_init(void);
static void _dispatch_lane_barrier_complete(dispatch_lane_class_t dqu,
dispatch_qos_t qos, dispatch_wakeup_flags_t flags);
static void _dispatch_lane_non_barrier_complete(dispatch_lane_t dq,
dispatch_wakeup_flags_t flags);
#if HAVE_PTHREAD_WORKQUEUE_QOS
static inline void _dispatch_queue_wakeup_with_override(
dispatch_queue_class_t dq, uint64_t dq_state,
dispatch_wakeup_flags_t flags);
#endif
static void _dispatch_workloop_drain_barrier_waiter(dispatch_workloop_t dwl,
struct dispatch_object_s *dc, dispatch_qos_t qos,
dispatch_wakeup_flags_t flags, uint64_t owned);
#pragma mark -
#pragma mark dispatch_assert_queue
DISPATCH_NOINLINE DISPATCH_NORETURN
static void
_dispatch_assert_queue_fail(dispatch_queue_t dq, bool expected)
{
_dispatch_client_assert_fail(
"Block was %sexpected to execute on queue [%s]",
expected ? "" : "not ", dq->dq_label ?: "");
}
DISPATCH_NOINLINE DISPATCH_NORETURN
static void
_dispatch_assert_queue_barrier_fail(dispatch_queue_t dq)
{
_dispatch_client_assert_fail(
"Block was expected to act as a barrier on queue [%s]",
dq->dq_label ?: "");
}
void
dispatch_assert_queue(dispatch_queue_t dq)
{
unsigned long metatype = dx_metatype(dq);
if (unlikely(metatype != _DISPATCH_LANE_TYPE &&
metatype != _DISPATCH_WORKLOOP_TYPE)) {
DISPATCH_CLIENT_CRASH(metatype, "invalid queue passed to "
"dispatch_assert_queue()");
}
uint64_t dq_state = os_atomic_load2o(dq, dq_state, relaxed);
if (likely(_dq_state_drain_locked_by_self(dq_state))) {
return;
}
if (likely(_dispatch_thread_frame_find_queue(dq))) {
return;
}
_dispatch_assert_queue_fail(dq, true);
}
void
dispatch_assert_queue_not(dispatch_queue_t dq)
{
unsigned long metatype = dx_metatype(dq);
if (unlikely(metatype != _DISPATCH_LANE_TYPE &&
metatype != _DISPATCH_WORKLOOP_TYPE)) {
DISPATCH_CLIENT_CRASH(metatype, "invalid queue passed to "
"dispatch_assert_queue_not()");
}
uint64_t dq_state = os_atomic_load2o(dq, dq_state, relaxed);
if (unlikely(_dq_state_drain_locked_by_self(dq_state))) {
_dispatch_assert_queue_fail(dq, false);
}
if (unlikely(_dispatch_thread_frame_find_queue(dq))) {
_dispatch_assert_queue_fail(dq, false);
}
}
void
dispatch_assert_queue_barrier(dispatch_queue_t dq)
{
dispatch_assert_queue(dq);
if (likely(dq->dq_width == 1)) {
return;
}
if (likely(dq->do_targetq)) {
uint64_t dq_state = os_atomic_load2o(dq, dq_state, relaxed);
if (likely(_dq_state_is_in_barrier(dq_state))) {
return;
}
}
_dispatch_assert_queue_barrier_fail(dq);
}
#pragma mark -
#pragma mark _dispatch_set_priority_and_mach_voucher
#if HAVE_PTHREAD_WORKQUEUE_QOS
DISPATCH_NOINLINE
void
_dispatch_set_priority_and_mach_voucher_slow(pthread_priority_t pp,
mach_voucher_t kv)
{
_pthread_set_flags_t pflags = 0;
if (pp && _dispatch_set_qos_class_enabled) {
pthread_priority_t old_pri = _dispatch_get_priority();
if (pp != old_pri) {
if (old_pri & _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG) {
pflags |= _PTHREAD_SET_SELF_WQ_KEVENT_UNBIND;
// when we unbind, overcomitness can flip, so we need to learn
// it from the defaultpri, see _dispatch_priority_compute_update
pp |= (_dispatch_get_basepri() &
DISPATCH_PRIORITY_FLAG_OVERCOMMIT);
} else {
// else we need to keep the one that is set in the current pri
pp |= (old_pri & _PTHREAD_PRIORITY_OVERCOMMIT_FLAG);
}
if (likely(old_pri & ~_PTHREAD_PRIORITY_FLAGS_MASK)) {
pflags |= _PTHREAD_SET_SELF_QOS_FLAG;
}
uint64_t mgr_dq_state =
os_atomic_load2o(&_dispatch_mgr_q, dq_state, relaxed);
if (unlikely(_dq_state_drain_locked_by_self(mgr_dq_state))) {
DISPATCH_INTERNAL_CRASH(pp,
"Changing the QoS while on the manager queue");
}
if (unlikely(pp & _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG)) {
DISPATCH_INTERNAL_CRASH(pp, "Cannot raise oneself to manager");
}
if (old_pri & _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG) {
DISPATCH_INTERNAL_CRASH(old_pri,
"Cannot turn a manager thread into a normal one");
}
}
}
if (kv != VOUCHER_NO_MACH_VOUCHER) {
#if VOUCHER_USE_MACH_VOUCHER
pflags |= _PTHREAD_SET_SELF_VOUCHER_FLAG;
#endif
}
if (!pflags) return;
int r = _pthread_set_properties_self(pflags, pp, kv);
if (r == EINVAL) {
DISPATCH_INTERNAL_CRASH(pp, "_pthread_set_properties_self failed");
}
(void)dispatch_assume_zero(r);
}
DISPATCH_NOINLINE
voucher_t
_dispatch_set_priority_and_voucher_slow(pthread_priority_t priority,
voucher_t v, dispatch_thread_set_self_t flags)
{
voucher_t ov = DISPATCH_NO_VOUCHER;
mach_voucher_t kv = VOUCHER_NO_MACH_VOUCHER;
if (v != DISPATCH_NO_VOUCHER) {
bool retained = flags & DISPATCH_VOUCHER_CONSUME;
ov = _voucher_get();
if (ov == v && (flags & DISPATCH_VOUCHER_REPLACE)) {
if (retained && v) _voucher_release_no_dispose(v);
ov = DISPATCH_NO_VOUCHER;
} else {
if (!retained && v) _voucher_retain(v);
kv = _voucher_swap_and_get_mach_voucher(ov, v);
}
}
if (!(flags & DISPATCH_THREAD_PARK)) {
_dispatch_set_priority_and_mach_voucher_slow(priority, kv);
}
if (ov != DISPATCH_NO_VOUCHER && (flags & DISPATCH_VOUCHER_REPLACE)) {
if (ov) _voucher_release(ov);
ov = DISPATCH_NO_VOUCHER;
}
return ov;
}
#endif
#pragma mark -
#pragma mark dispatch_continuation_t
static void _dispatch_async_redirect_invoke(dispatch_continuation_t dc,
dispatch_invoke_context_t dic, dispatch_invoke_flags_t flags);
#if HAVE_PTHREAD_WORKQUEUE_QOS
static void _dispatch_queue_override_invoke(dispatch_continuation_t dc,
dispatch_invoke_context_t dic, dispatch_invoke_flags_t flags);
static void _dispatch_workloop_stealer_invoke(dispatch_continuation_t dc,
dispatch_invoke_context_t dic, dispatch_invoke_flags_t flags);
#endif // HAVE_PTHREAD_WORKQUEUE_QOS
const struct dispatch_continuation_vtable_s _dispatch_continuation_vtables[] = {
DC_VTABLE_ENTRY(ASYNC_REDIRECT,
.do_invoke = _dispatch_async_redirect_invoke),
#if HAVE_MACH
DC_VTABLE_ENTRY(MACH_SEND_BARRRIER_DRAIN,
.do_invoke = _dispatch_mach_send_barrier_drain_invoke),
DC_VTABLE_ENTRY(MACH_SEND_BARRIER,
.do_invoke = _dispatch_mach_barrier_invoke),
DC_VTABLE_ENTRY(MACH_RECV_BARRIER,
.do_invoke = _dispatch_mach_barrier_invoke),
DC_VTABLE_ENTRY(MACH_ASYNC_REPLY,
.do_invoke = _dispatch_mach_msg_async_reply_invoke),
#endif
#if HAVE_PTHREAD_WORKQUEUE_QOS
DC_VTABLE_ENTRY(WORKLOOP_STEALING,
.do_invoke = _dispatch_workloop_stealer_invoke),
DC_VTABLE_ENTRY(OVERRIDE_STEALING,
.do_invoke = _dispatch_queue_override_invoke),
DC_VTABLE_ENTRY(OVERRIDE_OWNING,
.do_invoke = _dispatch_queue_override_invoke),
#endif
#if HAVE_MACH
DC_VTABLE_ENTRY(MACH_IPC_HANDOFF,
.do_invoke = _dispatch_mach_ipc_handoff_invoke),
#endif
};
DISPATCH_NOINLINE
static void DISPATCH_TSD_DTOR_CC
_dispatch_cache_cleanup(void *value)
{
dispatch_continuation_t dc, next_dc = value;
while ((dc = next_dc)) {
next_dc = dc->do_next;
_dispatch_continuation_free_to_heap(dc);
}
}
static void
_dispatch_force_cache_cleanup(void)
{
dispatch_continuation_t dc;
dc = _dispatch_thread_getspecific(dispatch_cache_key);
if (dc) {
_dispatch_thread_setspecific(dispatch_cache_key, NULL);
_dispatch_cache_cleanup(dc);
}
}
#if DISPATCH_USE_MEMORYPRESSURE_SOURCE
DISPATCH_NOINLINE
void
_dispatch_continuation_free_to_cache_limit(dispatch_continuation_t dc)
{
_dispatch_continuation_free_to_heap(dc);
dispatch_continuation_t next_dc;
dc = _dispatch_thread_getspecific(dispatch_cache_key);
int cnt;
if (!dc || (cnt = dc->dc_cache_cnt -
_dispatch_continuation_cache_limit) <= 0) {
return;
}
do {
next_dc = dc->do_next;
_dispatch_continuation_free_to_heap(dc);
} while (--cnt && (dc = next_dc));
_dispatch_thread_setspecific(dispatch_cache_key, next_dc);
}
#endif
DISPATCH_NOINLINE
void
_dispatch_continuation_pop(dispatch_object_t dou, dispatch_invoke_context_t dic,
dispatch_invoke_flags_t flags, dispatch_queue_class_t dqu)
{
_dispatch_continuation_pop_inline(dou, dic, flags, dqu._dq);
}
#pragma mark -
#pragma mark dispatch_block_create
#if __BLOCKS__
DISPATCH_ALWAYS_INLINE
static inline bool
_dispatch_block_flags_valid(dispatch_block_flags_t flags)
{
return ((flags & ~DISPATCH_BLOCK_API_MASK) == 0);
}
DISPATCH_ALWAYS_INLINE
static inline dispatch_block_flags_t
_dispatch_block_normalize_flags(dispatch_block_flags_t flags)
{
if (flags & (DISPATCH_BLOCK_NO_QOS_CLASS|DISPATCH_BLOCK_DETACHED)) {
flags |= DISPATCH_BLOCK_HAS_PRIORITY;
}
if (flags & DISPATCH_BLOCK_ENFORCE_QOS_CLASS) {
flags &= ~(dispatch_block_flags_t)DISPATCH_BLOCK_INHERIT_QOS_CLASS;
}
return flags;
}
static inline dispatch_block_t
_dispatch_block_create_with_voucher_and_priority(dispatch_block_flags_t flags,
voucher_t voucher, pthread_priority_t pri, dispatch_block_t block)
{
dispatch_block_flags_t unmodified_flags = flags;
pthread_priority_t unmodified_pri = pri;
flags = _dispatch_block_normalize_flags(flags);
bool assign = (flags & DISPATCH_BLOCK_ASSIGN_CURRENT);
if (!(flags & DISPATCH_BLOCK_HAS_VOUCHER)) {
if (flags & DISPATCH_BLOCK_DETACHED) {
voucher = VOUCHER_NULL;
flags |= DISPATCH_BLOCK_HAS_VOUCHER;
} else if (flags & DISPATCH_BLOCK_NO_VOUCHER) {
voucher = DISPATCH_NO_VOUCHER;
flags |= DISPATCH_BLOCK_HAS_VOUCHER;
} else if (assign) {
#if OS_VOUCHER_ACTIVITY_SPI
voucher = VOUCHER_CURRENT;
#endif
flags |= DISPATCH_BLOCK_HAS_VOUCHER;
}
}
#if OS_VOUCHER_ACTIVITY_SPI
if (voucher == VOUCHER_CURRENT) {
voucher = _voucher_get();
}
#endif
if (assign && !(flags & DISPATCH_BLOCK_HAS_PRIORITY)) {
pri = _dispatch_priority_propagate();
flags |= DISPATCH_BLOCK_HAS_PRIORITY;
}
dispatch_block_t db = _dispatch_block_create(flags, voucher, pri, block);
#if DISPATCH_DEBUG
dispatch_assert(_dispatch_block_get_data(db));
#endif
_dispatch_trace_block_create_with_voucher_and_priority(db,
_dispatch_Block_invoke(block), unmodified_flags,
((unmodified_flags & DISPATCH_BLOCK_HAS_PRIORITY) ? unmodified_pri :
(unsigned long)UINT32_MAX),
_dispatch_get_priority(), pri);
return db;
}
dispatch_block_t
dispatch_block_create(dispatch_block_flags_t flags, dispatch_block_t block)
{
if (!_dispatch_block_flags_valid(flags)) return DISPATCH_BAD_INPUT;
return _dispatch_block_create_with_voucher_and_priority(flags, NULL, 0,
block);
}
dispatch_block_t
dispatch_block_create_with_qos_class(dispatch_block_flags_t flags,
dispatch_qos_class_t qos_class, int relative_priority,
dispatch_block_t block)
{
if (!_dispatch_block_flags_valid(flags) ||
!_dispatch_qos_class_valid(qos_class, relative_priority)) {
return DISPATCH_BAD_INPUT;
}
flags |= DISPATCH_BLOCK_HAS_PRIORITY;
pthread_priority_t pri = 0;
#if HAVE_PTHREAD_WORKQUEUE_QOS
pri = _pthread_qos_class_encode(qos_class, relative_priority, 0);
#endif
return _dispatch_block_create_with_voucher_and_priority(flags, NULL,
pri, block);
}
dispatch_block_t
dispatch_block_create_with_voucher(dispatch_block_flags_t flags,
voucher_t voucher, dispatch_block_t block)
{
if (!_dispatch_block_flags_valid(flags)) return DISPATCH_BAD_INPUT;
flags |= DISPATCH_BLOCK_HAS_VOUCHER;
flags &= ~DISPATCH_BLOCK_NO_VOUCHER;
return _dispatch_block_create_with_voucher_and_priority(flags, voucher, 0,
block);
}
dispatch_block_t
dispatch_block_create_with_voucher_and_qos_class(dispatch_block_flags_t flags,
voucher_t voucher, dispatch_qos_class_t qos_class,
int relative_priority, dispatch_block_t block)
{
if (!_dispatch_block_flags_valid(flags) ||
!_dispatch_qos_class_valid(qos_class, relative_priority)) {
return DISPATCH_BAD_INPUT;
}
flags |= (DISPATCH_BLOCK_HAS_VOUCHER|DISPATCH_BLOCK_HAS_PRIORITY);
flags &= ~(DISPATCH_BLOCK_NO_VOUCHER|DISPATCH_BLOCK_NO_QOS_CLASS);
pthread_priority_t pri = 0;
#if HAVE_PTHREAD_WORKQUEUE_QOS
pri = _pthread_qos_class_encode(qos_class, relative_priority, 0);
#endif
return _dispatch_block_create_with_voucher_and_priority(flags, voucher,
pri, block);
}
void
dispatch_block_perform(dispatch_block_flags_t flags, dispatch_block_t block)
{
if (!_dispatch_block_flags_valid(flags)) {
DISPATCH_CLIENT_CRASH(flags, "Invalid flags passed to "
"dispatch_block_perform()");
}
flags = _dispatch_block_normalize_flags(flags);
voucher_t voucher = DISPATCH_NO_VOUCHER;
if (flags & DISPATCH_BLOCK_DETACHED) {
voucher = VOUCHER_NULL;
flags |= DISPATCH_BLOCK_HAS_VOUCHER;
}
struct dispatch_block_private_data_s dbpds =
DISPATCH_BLOCK_PRIVATE_DATA_PERFORM_INITIALIZER(flags, block, voucher);
return _dispatch_block_invoke_direct(&dbpds);
}
void
_dispatch_block_invoke_direct(const struct dispatch_block_private_data_s *dbcpd)
{
dispatch_block_private_data_t dbpd = (dispatch_block_private_data_t)dbcpd;
dispatch_block_flags_t flags = dbpd->dbpd_flags;
unsigned int atomic_flags = dbpd->dbpd_atomic_flags;
if (unlikely(atomic_flags & DBF_WAITED)) {
DISPATCH_CLIENT_CRASH(atomic_flags, "A block object may not be both "
"run more than once and waited for");
}
if (atomic_flags & DBF_CANCELED) goto out;
pthread_priority_t op = 0, p = 0;
op = _dispatch_block_invoke_should_set_priority(flags, dbpd->dbpd_priority);
if (op) {
p = dbpd->dbpd_priority;
}
voucher_t ov, v = DISPATCH_NO_VOUCHER;
if (flags & DISPATCH_BLOCK_HAS_VOUCHER) {
v = dbpd->dbpd_voucher;
}
ov = _dispatch_set_priority_and_voucher(p, v, 0);
dbpd->dbpd_thread = _dispatch_tid_self();
_dispatch_client_callout(dbpd->dbpd_block,
_dispatch_Block_invoke(dbpd->dbpd_block));
_dispatch_reset_priority_and_voucher(op, ov);
out:
if ((atomic_flags & DBF_PERFORM) == 0) {
if (os_atomic_inc2o(dbpd, dbpd_performed, relaxed) == 1) {
dispatch_group_leave(dbpd->dbpd_group);
}
}
}
void
_dispatch_block_sync_invoke(void *block)
{
dispatch_block_t b = block;
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(b);
dispatch_block_flags_t flags = dbpd->dbpd_flags;
unsigned int atomic_flags = dbpd->dbpd_atomic_flags;
if (unlikely(atomic_flags & DBF_WAITED)) {
DISPATCH_CLIENT_CRASH(atomic_flags, "A block object may not be both "
"run more than once and waited for");
}
if (atomic_flags & DBF_CANCELED) goto out;
voucher_t ov = DISPATCH_NO_VOUCHER;
if (flags & DISPATCH_BLOCK_HAS_VOUCHER) {
ov = _dispatch_adopt_priority_and_set_voucher(0, dbpd->dbpd_voucher, 0);
}
dbpd->dbpd_block();
_dispatch_reset_voucher(ov, 0);
out:
if ((atomic_flags & DBF_PERFORM) == 0) {
if (os_atomic_inc2o(dbpd, dbpd_performed, relaxed) == 1) {
dispatch_group_leave(dbpd->dbpd_group);
}
}
dispatch_queue_t boost_dq;
boost_dq = os_atomic_xchg2o(dbpd, dbpd_queue, NULL, relaxed);
if (boost_dq) {
// balances dispatch_{,barrier_,}sync
_dispatch_release_2(boost_dq);
}
}
#define DISPATCH_BLOCK_ASYNC_INVOKE_RELEASE 0x1
DISPATCH_NOINLINE
static void
_dispatch_block_async_invoke2(dispatch_block_t b, unsigned long invoke_flags)
{
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(b);
unsigned int atomic_flags = dbpd->dbpd_atomic_flags;
if (unlikely(atomic_flags & DBF_WAITED)) {
DISPATCH_CLIENT_CRASH(atomic_flags, "A block object may not be both "
"run more than once and waited for");
}
if (likely(!(atomic_flags & DBF_CANCELED))) {
dbpd->dbpd_block();
}
if ((atomic_flags & DBF_PERFORM) == 0) {
if (os_atomic_inc2o(dbpd, dbpd_performed, relaxed) == 1) {
dispatch_group_leave(dbpd->dbpd_group);
}
}
dispatch_queue_t boost_dq;
boost_dq = os_atomic_xchg2o(dbpd, dbpd_queue, NULL, relaxed);
if (boost_dq) {
// balances dispatch_{,barrier_,group_}async
_dispatch_release_2(boost_dq);
}
if (invoke_flags & DISPATCH_BLOCK_ASYNC_INVOKE_RELEASE) {
Block_release(b);
}
}
static void
_dispatch_block_async_invoke(void *block)
{
_dispatch_block_async_invoke2(block, 0);
}
static void
_dispatch_block_async_invoke_and_release(void *block)
{
_dispatch_block_async_invoke2(block, DISPATCH_BLOCK_ASYNC_INVOKE_RELEASE);
}
void
dispatch_block_cancel(dispatch_block_t db)
{
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(db);
if (unlikely(!dbpd)) {
DISPATCH_CLIENT_CRASH(0, "Invalid block object passed to "
"dispatch_block_cancel()");
}
(void)os_atomic_or2o(dbpd, dbpd_atomic_flags, DBF_CANCELED, relaxed);
}
intptr_t
dispatch_block_testcancel(dispatch_block_t db)
{
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(db);
if (unlikely(!dbpd)) {
DISPATCH_CLIENT_CRASH(0, "Invalid block object passed to "
"dispatch_block_testcancel()");
}
return (bool)(dbpd->dbpd_atomic_flags & DBF_CANCELED);
}
intptr_t
dispatch_block_wait(dispatch_block_t db, dispatch_time_t timeout)
{
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(db);
if (unlikely(!dbpd)) {
DISPATCH_CLIENT_CRASH(0, "Invalid block object passed to "
"dispatch_block_wait()");
}
unsigned int flags = os_atomic_or_orig2o(dbpd, dbpd_atomic_flags,
DBF_WAITING, relaxed);
if (unlikely(flags & (DBF_WAITED | DBF_WAITING))) {
DISPATCH_CLIENT_CRASH(flags, "A block object may not be waited for "
"more than once");
}
// <rdar://problem/17703192> If we know the queue where this block is
// enqueued, or the thread that's executing it, then we should boost
// it here.
pthread_priority_t pp = _dispatch_get_priority();
dispatch_queue_t boost_dq;
boost_dq = os_atomic_xchg2o(dbpd, dbpd_queue, NULL, relaxed);
if (boost_dq) {
// release balances dispatch_{,barrier_,group_}async.
// Can't put the queue back in the timeout case: the block might
// finish after we fell out of group_wait and see our NULL, so
// neither of us would ever release. Side effect: After a _wait
// that times out, subsequent waits will not boost the qos of the
// still-running block.
dx_wakeup(boost_dq, _dispatch_qos_from_pp(pp),
DISPATCH_WAKEUP_BLOCK_WAIT | DISPATCH_WAKEUP_CONSUME_2);
}
mach_port_t boost_th = dbpd->dbpd_thread;
if (boost_th) {
_dispatch_thread_override_start(boost_th, pp, dbpd);
}
int performed = os_atomic_load2o(dbpd, dbpd_performed, relaxed);
if (unlikely(performed > 1 || (boost_th && boost_dq))) {
DISPATCH_CLIENT_CRASH(performed, "A block object may not be both "
"run more than once and waited for");
}
long ret = dispatch_group_wait(dbpd->dbpd_group, timeout);
if (boost_th) {
_dispatch_thread_override_end(boost_th, dbpd);
}
if (ret) {
// timed out: reverse our changes
os_atomic_and2o(dbpd, dbpd_atomic_flags, ~DBF_WAITING, relaxed);
} else {
os_atomic_or2o(dbpd, dbpd_atomic_flags, DBF_WAITED, relaxed);
// don't need to re-test here: the second call would see
// the first call's WAITING
}
return ret;
}
void
dispatch_block_notify(dispatch_block_t db, dispatch_queue_t queue,
dispatch_block_t notification_block)
{
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(db);
if (!dbpd) {
DISPATCH_CLIENT_CRASH(db, "Invalid block object passed to "
"dispatch_block_notify()");
}
int performed = os_atomic_load2o(dbpd, dbpd_performed, relaxed);
if (unlikely(performed > 1)) {
DISPATCH_CLIENT_CRASH(performed, "A block object may not be both "
"run more than once and observed");
}
return dispatch_group_notify(dbpd->dbpd_group, queue, notification_block);
}
DISPATCH_NOINLINE
dispatch_qos_t
_dispatch_continuation_init_slow(dispatch_continuation_t dc,
dispatch_queue_t dq, dispatch_block_flags_t flags)
{
dispatch_block_private_data_t dbpd = _dispatch_block_get_data(dc->dc_ctxt);
dispatch_block_flags_t block_flags = dbpd->dbpd_flags;
uintptr_t dc_flags = dc->dc_flags;
pthread_priority_t pp = 0;
// balanced in d_block_async_invoke_and_release or d_block_wait
if (os_atomic_cmpxchg2o(dbpd, dbpd_queue, NULL, dq, relaxed)) {
_dispatch_retain_2(dq);
}
if (dc_flags & DC_FLAG_CONSUME) {
dc->dc_func = _dispatch_block_async_invoke_and_release;
} else {
dc->dc_func = _dispatch_block_async_invoke;
}
flags |= block_flags;
if (block_flags & DISPATCH_BLOCK_HAS_PRIORITY) {
pp = dbpd->dbpd_priority & ~_PTHREAD_PRIORITY_FLAGS_MASK;
} else if (flags & DISPATCH_BLOCK_HAS_PRIORITY) {
// _dispatch_source_handler_alloc is calling is and doesn't want us
// to propagate priorities
pp = 0;
} else {
pp = _dispatch_priority_propagate();
}
_dispatch_continuation_priority_set(dc, dq, pp, flags);
if (block_flags & DISPATCH_BLOCK_BARRIER) {
dc_flags |= DC_FLAG_BARRIER;
}
if (block_flags & DISPATCH_BLOCK_HAS_VOUCHER) {
voucher_t v = dbpd->dbpd_voucher;
dc->dc_voucher = (v && v != DISPATCH_NO_VOUCHER) ? _voucher_retain(v)
: v;
_dispatch_voucher_debug("continuation[%p] set", dc->dc_voucher, dc);
_dispatch_voucher_ktrace_dc_push(dc);
} else {
_dispatch_continuation_voucher_set(dc, flags);
}
dc_flags |= DC_FLAG_BLOCK_WITH_PRIVATE_DATA;
dc->dc_flags = dc_flags;
return _dispatch_qos_from_pp(dc->dc_priority);
}
#endif // __BLOCKS__
#pragma mark -
#pragma mark dispatch_barrier_async
DISPATCH_NOINLINE
static void
_dispatch_async_f_slow(dispatch_queue_t dq, void *ctxt,
dispatch_function_t func, dispatch_block_flags_t flags,
uintptr_t dc_flags)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc_from_heap();
dispatch_qos_t qos;
qos = _dispatch_continuation_init_f(dc, dq, ctxt, func, flags, dc_flags);
_dispatch_continuation_async(dq, dc, qos, dc->dc_flags);
}
DISPATCH_NOINLINE
void
dispatch_barrier_async_f(dispatch_queue_t dq, void *ctxt,
dispatch_function_t func)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc_cacheonly();
uintptr_t dc_flags = DC_FLAG_CONSUME | DC_FLAG_BARRIER;
dispatch_qos_t qos;
if (likely(!dc)) {
return _dispatch_async_f_slow(dq, ctxt, func, 0, dc_flags);
}
qos = _dispatch_continuation_init_f(dc, dq, ctxt, func, 0, dc_flags);
_dispatch_continuation_async(dq, dc, qos, dc_flags);
}
DISPATCH_NOINLINE
void
_dispatch_barrier_async_detached_f(dispatch_queue_class_t dq, void *ctxt,
dispatch_function_t func)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc();
dc->dc_flags = DC_FLAG_CONSUME | DC_FLAG_BARRIER | DC_FLAG_ALLOCATED;
dc->dc_func = func;
dc->dc_ctxt = ctxt;
dc->dc_voucher = DISPATCH_NO_VOUCHER;
dc->dc_priority = DISPATCH_NO_PRIORITY;
_dispatch_trace_item_push(dq, dc);
dx_push(dq._dq, dc, 0);
}
#ifdef __BLOCKS__
void
dispatch_barrier_async(dispatch_queue_t dq, dispatch_block_t work)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc();
uintptr_t dc_flags = DC_FLAG_CONSUME | DC_FLAG_BARRIER;
dispatch_qos_t qos;
qos = _dispatch_continuation_init(dc, dq, work, 0, dc_flags);
_dispatch_continuation_async(dq, dc, qos, dc_flags);
}
#endif
#pragma mark -
#pragma mark dispatch_async
void
_dispatch_async_redirect_invoke(dispatch_continuation_t dc,
dispatch_invoke_context_t dic, dispatch_invoke_flags_t flags)
{
dispatch_thread_frame_s dtf;
struct dispatch_continuation_s *other_dc = dc->dc_other;
dispatch_invoke_flags_t ctxt_flags = (dispatch_invoke_flags_t)(uintptr_t)dc->dc_ctxt;
// if we went through _dispatch_root_queue_push_override,
// the "right" root queue was stuffed into dc_func
dispatch_queue_global_t assumed_rq = (dispatch_queue_global_t)dc->dc_func;
dispatch_lane_t dq = dc->dc_data;
dispatch_queue_t rq, old_dq;
dispatch_priority_t old_dbp;
if (ctxt_flags) {
flags &= ~_DISPATCH_INVOKE_AUTORELEASE_MASK;
flags |= ctxt_flags;
}
old_dq = _dispatch_queue_get_current();
if (assumed_rq) {
old_dbp = _dispatch_root_queue_identity_assume(assumed_rq);
_dispatch_set_basepri(dq->dq_priority);
} else {
old_dbp = _dispatch_set_basepri(dq->dq_priority);
}
uintptr_t dc_flags = DC_FLAG_CONSUME | DC_FLAG_NO_INTROSPECTION;
_dispatch_thread_frame_push(&dtf, dq);
_dispatch_continuation_pop_forwarded(dc, dc_flags, NULL, {
_dispatch_continuation_pop(other_dc, dic, flags, dq);
});
_dispatch_thread_frame_pop(&dtf);
if (assumed_rq) _dispatch_queue_set_current(old_dq);
_dispatch_reset_basepri(old_dbp);
rq = dq->do_targetq;
while (unlikely(rq->do_targetq && rq != old_dq)) {
_dispatch_lane_non_barrier_complete(upcast(rq)._dl, 0);
rq = rq->do_targetq;
}
// pairs with _dispatch_async_redirect_wrap
_dispatch_lane_non_barrier_complete(dq, DISPATCH_WAKEUP_CONSUME_2);
}
DISPATCH_ALWAYS_INLINE
static inline dispatch_continuation_t
_dispatch_async_redirect_wrap(dispatch_lane_t dq, dispatch_object_t dou)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc();
dou._do->do_next = NULL;
dc->do_vtable = DC_VTABLE(ASYNC_REDIRECT);
dc->dc_func = NULL;
dc->dc_ctxt = (void *)(uintptr_t)_dispatch_queue_autorelease_frequency(dq);
dc->dc_data = dq;
dc->dc_other = dou._do;
dc->dc_voucher = DISPATCH_NO_VOUCHER;
dc->dc_priority = DISPATCH_NO_PRIORITY;
_dispatch_retain_2(dq); // released in _dispatch_async_redirect_invoke
return dc;
}
DISPATCH_NOINLINE
static void
_dispatch_continuation_redirect_push(dispatch_lane_t dl,
dispatch_object_t dou, dispatch_qos_t qos)
{
if (likely(!_dispatch_object_is_redirection(dou))) {
dou._dc = _dispatch_async_redirect_wrap(dl, dou);
} else if (!dou._dc->dc_ctxt) {
// find first queue in descending target queue order that has
// an autorelease frequency set, and use that as the frequency for
// this continuation.
dou._dc->dc_ctxt = (void *)
(uintptr_t)_dispatch_queue_autorelease_frequency(dl);
}
dispatch_queue_t dq = dl->do_targetq;
if (!qos) qos = _dispatch_priority_qos(dq->dq_priority);
dx_push(dq, dou, qos);
}
DISPATCH_ALWAYS_INLINE
static inline void
_dispatch_async_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func,
dispatch_block_flags_t flags)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc_cacheonly();
uintptr_t dc_flags = DC_FLAG_CONSUME;
dispatch_qos_t qos;
if (unlikely(!dc)) {
return _dispatch_async_f_slow(dq, ctxt, func, flags, dc_flags);
}
qos = _dispatch_continuation_init_f(dc, dq, ctxt, func, flags, dc_flags);
_dispatch_continuation_async(dq, dc, qos, dc->dc_flags);
}
DISPATCH_NOINLINE
void
dispatch_async_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
{
_dispatch_async_f(dq, ctxt, func, 0);
}
DISPATCH_NOINLINE
void
dispatch_async_enforce_qos_class_f(dispatch_queue_t dq, void *ctxt,
dispatch_function_t func)
{
_dispatch_async_f(dq, ctxt, func, DISPATCH_BLOCK_ENFORCE_QOS_CLASS);
}
#ifdef __BLOCKS__
void
dispatch_async(dispatch_queue_t dq, dispatch_block_t work)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc();
uintptr_t dc_flags = DC_FLAG_CONSUME;
dispatch_qos_t qos;
qos = _dispatch_continuation_init(dc, dq, work, 0, dc_flags);
_dispatch_continuation_async(dq, dc, qos, dc->dc_flags);
}
#endif
#pragma mark -
#pragma mark _dispatch_sync_invoke / _dispatch_sync_complete
DISPATCH_ALWAYS_INLINE
static uint64_t
_dispatch_lane_non_barrier_complete_try_lock(dispatch_lane_t dq,
uint64_t old_state, uint64_t new_state, uint64_t owner_self)
{
uint64_t full_width = new_state;
if (_dq_state_has_pending_barrier(new_state)) {
full_width -= DISPATCH_QUEUE_PENDING_BARRIER;
full_width += DISPATCH_QUEUE_WIDTH_INTERVAL;
full_width += DISPATCH_QUEUE_IN_BARRIER;
} else {
full_width += dq->dq_width * DISPATCH_QUEUE_WIDTH_INTERVAL;
full_width += DISPATCH_QUEUE_IN_BARRIER;
}
if ((full_width & DISPATCH_QUEUE_WIDTH_MASK) ==
DISPATCH_QUEUE_WIDTH_FULL_BIT) {
new_state = full_width;
new_state &= ~DISPATCH_QUEUE_DIRTY;
new_state |= owner_self;
} else if (_dq_state_is_dirty(old_state)) {
new_state |= DISPATCH_QUEUE_ENQUEUED;
}
return new_state;
}
DISPATCH_ALWAYS_INLINE
static void
_dispatch_lane_non_barrier_complete_finish(dispatch_lane_t dq,
dispatch_wakeup_flags_t flags, uint64_t old_state, uint64_t new_state)
{
if (_dq_state_received_override(old_state)) {
// Ensure that the root queue sees that this thread was overridden.
_dispatch_set_basepri_override_qos(_dq_state_max_qos(old_state));
}
if ((old_state ^ new_state) & DISPATCH_QUEUE_IN_BARRIER) {
if (_dq_state_is_dirty(old_state)) {
// <rdar://problem/14637483>
// dependency ordering for dq state changes that were flushed
// and not acted upon
os_atomic_thread_fence(dependency);
dq = os_atomic_force_dependency_on(dq, old_state);
}
return _dispatch_lane_barrier_complete(dq, 0, flags);
}
if ((old_state ^ new_state) & DISPATCH_QUEUE_ENQUEUED) {
if (!(flags & DISPATCH_WAKEUP_CONSUME_2)) {
_dispatch_retain_2(dq);
}
dispatch_assert(!_dq_state_is_base_wlh(new_state));
_dispatch_trace_item_push(dq->do_targetq, dq);
return dx_push(dq->do_targetq, dq, _dq_state_max_qos(new_state));
}
if (flags & DISPATCH_WAKEUP_CONSUME_2) {
_dispatch_release_2_tailcall(dq);
}
}
DISPATCH_NOINLINE
static void
_dispatch_lane_non_barrier_complete(dispatch_lane_t dq,
dispatch_wakeup_flags_t flags)
{
uint64_t old_state, new_state, owner_self = _dispatch_lock_value_for_self();
// see _dispatch_lane_resume()
os_atomic_rmw_loop2o(dq, dq_state, old_state, new_state, relaxed, {
new_state = old_state - DISPATCH_QUEUE_WIDTH_INTERVAL;
if (unlikely(_dq_state_drain_locked(old_state))) {
// make drain_try_unlock() fail and reconsider whether there's
// enough width now for a new item
new_state |= DISPATCH_QUEUE_DIRTY;
} else if (likely(_dq_state_is_runnable(new_state))) {
new_state = _dispatch_lane_non_barrier_complete_try_lock(dq,
old_state, new_state, owner_self);
}
});
_dispatch_lane_non_barrier_complete_finish(dq, flags, old_state, new_state);
}
DISPATCH_ALWAYS_INLINE
static inline void
_dispatch_sync_function_invoke_inline(dispatch_queue_class_t dq, void *ctxt,
dispatch_function_t func)
{
dispatch_thread_frame_s dtf;
_dispatch_thread_frame_push(&dtf, dq);
_dispatch_client_callout(ctxt, func);
_dispatch_perfmon_workitem_inc();
_dispatch_thread_frame_pop(&dtf);
}
DISPATCH_NOINLINE
static void
_dispatch_sync_function_invoke(dispatch_queue_class_t dq, void *ctxt,
dispatch_function_t func)
{