Repository navigation
Expand file tree
/
Copy pathqueue.c
More file actions
3161 lines (2837 loc) · 86.5 KB
/
Copy pathqueue.c
File metadata and controls
3161 lines (2837 loc) · 86.5 KB
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-2011 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"
#endif
#if __linux__
#include <sys/eventfd.h>
#define DISPATCH_LINUX_COMPAT 1
#endif
#if (!HAVE_PTHREAD_WORKQUEUES || DISPATCH_DEBUG) && \
!defined(DISPATCH_ENABLE_THREAD_POOL)
#define DISPATCH_ENABLE_THREAD_POOL 1
#endif
#if DISPATCH_ENABLE_THREAD_POOL && !DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
#define pthread_workqueue_t void*
#endif
static void _dispatch_cache_cleanup(void *value);
static void _dispatch_async_f_redirect(dispatch_queue_t dq,
dispatch_continuation_t dc);
static void _dispatch_queue_cleanup(void *ctxt);
static inline void _dispatch_queue_wakeup_global2(dispatch_queue_t dq,
unsigned int n);
static inline void _dispatch_queue_wakeup_global(dispatch_queue_t dq);
static _dispatch_thread_semaphore_t _dispatch_queue_drain(dispatch_queue_t dq);
static inline _dispatch_thread_semaphore_t
_dispatch_queue_drain_one_barrier_sync(dispatch_queue_t dq);
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
static void _dispatch_worker_thread3(void *context);
#endif
#if HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP
static void _dispatch_worker_thread2(int priority, int options, void *context);
#endif
#if DISPATCH_ENABLE_THREAD_POOL
static void *_dispatch_worker_thread(void *context);
static int _dispatch_pthread_sigmask(int how, sigset_t *set, sigset_t *oset);
#endif
#if DISPATCH_COCOA_COMPAT || DISPATCH_LINUX_COMPAT
static dispatch_queue_t _dispatch_queue_wakeup_main(void);
static void _dispatch_main_queue_drain(void);
#endif
#if DISPATCH_COCOA_COMPAT
static unsigned int _dispatch_worker_threads;
static dispatch_once_t _dispatch_main_q_port_pred;
static mach_port_t main_q_port;
static void _dispatch_main_q_port_init(void *ctxt);
#endif
#if DISPATCH_LINUX_COMPAT
static dispatch_once_t _dispatch_main_q_eventfd_pred;
static void _dispatch_main_q_eventfd_init(void *ctxt);
static int main_q_eventfd = -1;
#endif
#pragma mark -
#pragma mark dispatch_root_queue
#if DISPATCH_ENABLE_THREAD_POOL
static struct dispatch_semaphore_s _dispatch_thread_mediator[] = {
[DISPATCH_ROOT_QUEUE_IDX_LOW_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_LOW_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_HIGH_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(semaphore),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
},
};
#endif
#define MAX_THREAD_COUNT 255
struct dispatch_root_queue_context_s {
union {
struct {
unsigned int volatile dgq_pending;
#if HAVE_PTHREAD_WORKQUEUES
int dgq_wq_priority, dgq_wq_options;
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_ENABLE_THREAD_POOL
pthread_workqueue_t dgq_kworkqueue;
#endif
#endif // HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_ENABLE_THREAD_POOL
dispatch_semaphore_t dgq_thread_mediator;
uint32_t dgq_thread_pool_size;
#endif
};
char _dgq_pad[DISPATCH_CACHELINE_SIZE];
};
};
DISPATCH_CACHELINE_ALIGN
static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
[DISPATCH_ROOT_QUEUE_IDX_LOW_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
.dgq_wq_options = 0,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_LOW_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_LOW_OVERCOMMIT_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_LOW_OVERCOMMIT_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
.dgq_wq_options = 0,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_HIGH_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
.dgq_wq_options = 0,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_HIGH_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
.dgq_wq_options = 0,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_OVERCOMMIT_PRIORITY] = {{{
.dgq_pending = 0,
#if HAVE_PTHREAD_WORKQUEUES
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
#endif
#if DISPATCH_ENABLE_THREAD_POOL
.dgq_thread_mediator = &_dispatch_thread_mediator[
DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_OVERCOMMIT_PRIORITY],
.dgq_thread_pool_size = MAX_THREAD_COUNT,
#endif
}}},
};
// 6618342 Contact the team that owns the Instrument DTrace probe before
// renaming this symbol
// dq_running is set to 2 so that barrier operations go through the slow path
DISPATCH_CACHELINE_ALIGN
struct dispatch_queue_s _dispatch_root_queues[] = {
[DISPATCH_ROOT_QUEUE_IDX_LOW_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_LOW_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 4,
.dq_specific_q = NULL,
// "com.apple.root.low-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'l', 'o', 'w', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_LOW_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_LOW_OVERCOMMIT_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 5,
.dq_specific_q = NULL,
// "com.apple.root.low-overcommit-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'l', 'o', 'w', '-', 'o', 'v', 'e', 'r', 'c', 'o', 'm', 'm', 'i', 't', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 6,
.dq_specific_q = NULL,
// "com.apple.root.default-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'd', 'e', 'f', 'a', 'u', 'l', 't', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 7,
.dq_specific_q = NULL,
// "com.apple.root.default-overcommit-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'd', 'e', 'f', 'a', 'u', 'l', 't', '-', 'o', 'v', 'e', 'r', 'c', 'o', 'm', 'm', 'i', 't', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_HIGH_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_HIGH_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 8,
.dq_specific_q = NULL,
// "com.apple.root.high-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'h', 'i', 'g', 'h', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 9,
.dq_specific_q = NULL,
// "com.apple.root.high-overcommit-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'h', 'i', 'g', 'h', '-', 'o', 'v', 'e', 'r', 'c', 'o', 'm', 'm', 'i', 't', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 10,
.dq_specific_q = NULL,
// "com.apple.root.background-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'b', 'a', 'c', 'k', 'g', 'r', 'o', 'u', 'n', 'd', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_OVERCOMMIT_PRIORITY] = {
.do_vtable = DISPATCH_VTABLE(queue_root),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = NULL,
.do_ctxt = &_dispatch_root_queue_contexts[
DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_OVERCOMMIT_PRIORITY],
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 2,
.dq_width = UINT32_MAX,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 11,
.dq_specific_q = NULL,
// "com.apple.root.background-overcommit-priority"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'r', 'o', 'o', 't', '.', 'b', 'a', 'c', 'k', 'g', 'r', 'o', 'u', 'n', 'd', '-', 'o', 'v', 'e', 'r', 'c', 'o', 'm', 'm', 'i', 't', '-', 'p', 'r', 'i', 'o', 'r', 'i', 't', 'y', '\0'},
},
};
#if HAVE_PTHREAD_WORKQUEUES
static const dispatch_queue_t _dispatch_wq2root_queues[][2] = {
[WORKQ_HIGH_PRIOQUEUE] = {
[0] = &_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_HIGH_PRIORITY],
[WORKQ_ADDTHREADS_OPTION_OVERCOMMIT] =
&_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY],
},
[WORKQ_DEFAULT_PRIOQUEUE] = {
[0] = &_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_PRIORITY],
[WORKQ_ADDTHREADS_OPTION_OVERCOMMIT] =
&_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY],
},
[WORKQ_LOW_PRIOQUEUE] = {
[0] = &_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_LOW_PRIORITY],
[WORKQ_ADDTHREADS_OPTION_OVERCOMMIT] =
&_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_LOW_OVERCOMMIT_PRIORITY],
},
[WORKQ_BG_PRIOQUEUE] = {
[0] = &_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_PRIORITY],
[WORKQ_ADDTHREADS_OPTION_OVERCOMMIT] =
&_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_OVERCOMMIT_PRIORITY],
},
};
#endif // HAVE_PTHREAD_WORKQUEUES
// 6618342 Contact the team that owns the Instrument DTrace probe before
// renaming this symbol
DISPATCH_CACHELINE_ALIGN
struct dispatch_queue_s _dispatch_mgr_q = {
.do_vtable = DISPATCH_VTABLE(queue_mgr),
.do_ref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_xref_cnt = DISPATCH_OBJECT_GLOBAL_REFCNT,
.do_next = NULL,
.do_targetq = &_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_HIGH_OVERCOMMIT_PRIORITY],
.do_ctxt = NULL,
.do_finalizer = NULL,
.do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_LOCK,
.dq_running = 0,
.dq_width = 1,
.dq_items_tail = NULL,
.dq_items_head = NULL,
.dq_serialnum = 2,
.dq_specific_q = NULL,
// "com.apple.libdispatch-manager"
.dq_label = {'c', 'o', 'm', '.', 'a', 'p', 'p', 'l', 'e', '.', 'l', 'i', 'b', 'd', 'i', 's', 'p', 'a', 't', 'c', 'h', '-', 'm', 'a', 'n', 'a', 'g', 'e', 'r', '\0'},
};
dispatch_queue_t
dispatch_get_global_queue(long priority, unsigned long flags)
{
if (flags & ~DISPATCH_QUEUE_OVERCOMMIT) {
return NULL;
}
return _dispatch_get_root_queue(priority,
flags & DISPATCH_QUEUE_OVERCOMMIT);
}
dispatch_queue_t
dispatch_get_current_queue(void)
{
return _dispatch_queue_get_current() ?: _dispatch_get_root_queue(0, true);
}
#pragma mark -
#pragma mark dispatch_init
static void
_dispatch_hw_config_init(void)
{
_dispatch_hw_config.cc_max_active = _dispatch_get_activecpu();
_dispatch_hw_config.cc_max_logical = _dispatch_get_logicalcpu_max();
_dispatch_hw_config.cc_max_physical = _dispatch_get_physicalcpu_max();
}
static inline bool
_dispatch_root_queues_init_workq(void)
{
bool result = false;
#if HAVE_PTHREAD_WORKQUEUES
bool disable_wq = false;
#if DISPATCH_ENABLE_THREAD_POOL
disable_wq = slowpath(getenv("LIBDISPATCH_DISABLE_KWQ"));
#endif
int r;
#if HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP
if (!disable_wq) {
r = pthread_workqueue_setdispatch_np(_dispatch_worker_thread2);
#if !DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
(void)dispatch_assume_zero(r);
#endif
result = !r;
}
#endif // HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_ENABLE_THREAD_POOL
if (!result) {
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
pthread_workqueue_attr_t pwq_attr;
if (!disable_wq) {
r = pthread_workqueue_attr_init_np(&pwq_attr);
(void)dispatch_assume_zero(r);
}
#endif
int i;
for (i = 0; i < DISPATCH_ROOT_QUEUE_COUNT; i++) {
pthread_workqueue_t pwq = NULL;
struct dispatch_root_queue_context_s *qc =
&_dispatch_root_queue_contexts[i];
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
if (!disable_wq
#if DISPATCH_NO_BG_PRIORITY
&& (qc->dgq_wq_priority != WORKQ_BG_PRIOQUEUE)
#endif
) {
r = pthread_workqueue_attr_setqueuepriority_np(&pwq_attr,
qc->dgq_wq_priority);
(void)dispatch_assume_zero(r);
r = pthread_workqueue_attr_setovercommit_np(&pwq_attr,
qc->dgq_wq_options & WORKQ_ADDTHREADS_OPTION_OVERCOMMIT);
(void)dispatch_assume_zero(r);
r = pthread_workqueue_create_np(&pwq, &pwq_attr);
(void)dispatch_assume_zero(r);
result = result || dispatch_assume(pwq);
}
#endif
qc->dgq_kworkqueue = pwq ? pwq : (pthread_workqueue_t)(~0ul);
}
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
if (!disable_wq) {
r = pthread_workqueue_attr_destroy_np(&pwq_attr);
(void)dispatch_assume_zero(r);
}
#endif
}
#endif // DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_ENABLE_THREAD_POOL
#endif // HAVE_PTHREAD_WORKQUEUES
return result;
}
static inline void
_dispatch_root_queues_init_thread_pool(void)
{
#if DISPATCH_ENABLE_THREAD_POOL
int i;
for (i = 0; i < DISPATCH_ROOT_QUEUE_COUNT; i++) {
#if TARGET_OS_EMBEDDED
// some software hangs if the non-overcommitting queues do not
// overcommit when threads block. Someday, this behavior should apply
// to all platforms
if (!(i & 1)) {
_dispatch_root_queue_contexts[i].dgq_thread_pool_size =
_dispatch_hw_config.cc_max_active;
}
#endif
#if USE_MACH_SEM
// override the default FIFO behavior for the pool semaphores
kern_return_t kr = semaphore_create(mach_task_self(),
&_dispatch_thread_mediator[i].dsema_port, SYNC_POLICY_LIFO, 0);
DISPATCH_VERIFY_MIG(kr);
(void)dispatch_assume_zero(kr);
(void)dispatch_assume(_dispatch_thread_mediator[i].dsema_port);
#elif USE_POSIX_SEM
/* XXXRW: POSIX semaphores don't support LIFO? */
int ret = sem_init(&_dispatch_thread_mediator[i].dsema_sem, 0, 0);
(void)dispatch_assume_zero(ret);
#endif
}
#else
DISPATCH_CRASH("Thread pool creation failed");
#endif // DISPATCH_ENABLE_THREAD_POOL
}
static void
_dispatch_root_queues_init(void *context DISPATCH_UNUSED)
{
_dispatch_safe_fork = false;
if (!_dispatch_root_queues_init_workq()) {
_dispatch_root_queues_init_thread_pool();
}
}
#define countof(x) (sizeof(x) / sizeof(x[0]))
DISPATCH_EXPORT DISPATCH_NOTHROW
void
libdispatch_init(void)
{
dispatch_assert(DISPATCH_QUEUE_PRIORITY_COUNT == 4);
dispatch_assert(DISPATCH_ROOT_QUEUE_COUNT == 8);
dispatch_assert(DISPATCH_QUEUE_PRIORITY_LOW ==
-DISPATCH_QUEUE_PRIORITY_HIGH);
dispatch_assert(countof(_dispatch_root_queues) ==
DISPATCH_ROOT_QUEUE_COUNT);
dispatch_assert(countof(_dispatch_root_queue_contexts) ==
DISPATCH_ROOT_QUEUE_COUNT);
#if HAVE_PTHREAD_WORKQUEUES
dispatch_assert(sizeof(_dispatch_wq2root_queues) /
sizeof(_dispatch_wq2root_queues[0][0]) ==
DISPATCH_ROOT_QUEUE_COUNT);
#endif
#if DISPATCH_ENABLE_THREAD_POOL
dispatch_assert(countof(_dispatch_thread_mediator) ==
DISPATCH_ROOT_QUEUE_COUNT);
#endif
dispatch_assert(sizeof(struct dispatch_apply_s) <=
ROUND_UP_TO_CACHELINE_SIZE(sizeof(
struct dispatch_continuation_s)));
dispatch_assert(sizeof(struct dispatch_source_s) ==
sizeof(struct dispatch_queue_s) - DISPATCH_QUEUE_CACHELINE_PAD);
dispatch_assert(sizeof(struct dispatch_queue_s) % DISPATCH_CACHELINE_SIZE
== 0);
dispatch_assert(sizeof(struct dispatch_root_queue_context_s) %
DISPATCH_CACHELINE_SIZE == 0);
_dispatch_thread_key_create(&dispatch_queue_key, _dispatch_queue_cleanup);
_dispatch_thread_key_create(&dispatch_sema4_key,
(void (*)(void *))_dispatch_thread_semaphore_dispose);
_dispatch_thread_key_create(&dispatch_cache_key, _dispatch_cache_cleanup);
_dispatch_thread_key_create(&dispatch_io_key, NULL);
_dispatch_thread_key_create(&dispatch_apply_key, NULL);
#if DISPATCH_PERF_MON
_dispatch_thread_key_create(&dispatch_bcounter_key, NULL);
#endif
#if DISPATCH_USE_RESOLVERS // rdar://problem/8541707
_dispatch_main_q.do_targetq = &_dispatch_root_queues[
DISPATCH_ROOT_QUEUE_IDX_DEFAULT_OVERCOMMIT_PRIORITY];
#endif
_dispatch_thread_setspecific(dispatch_queue_key, &_dispatch_main_q);
#if DISPATCH_USE_PTHREAD_ATFORK
(void)dispatch_assume_zero(pthread_atfork(dispatch_atfork_prepare,
dispatch_atfork_parent, dispatch_atfork_child));
#endif
_dispatch_hw_config_init();
_dispatch_vtable_init();
_os_object_init();
}
DISPATCH_EXPORT DISPATCH_NOTHROW
void
dispatch_atfork_child(void)
{
struct dispatch_object_s *crash = (struct dispatch_object_s *)0x100;
size_t i;
if (_dispatch_safe_fork) {
return;
}
_dispatch_main_q.dq_items_head = crash;
_dispatch_main_q.dq_items_tail = crash;
_dispatch_mgr_q.dq_items_head = crash;
_dispatch_mgr_q.dq_items_tail = crash;
for (i = 0; i < DISPATCH_ROOT_QUEUE_COUNT; i++) {
_dispatch_root_queues[i].dq_items_head = crash;
_dispatch_root_queues[i].dq_items_tail = crash;
}
}
#pragma mark -
#pragma mark dispatch_queue_t
// skip zero
// 1 - main_q
// 2 - mgr_q
// 3 - _unused_
// 4,5,6,7,8,9,10,11 - global queues
// we use 'xadd' on Intel, so the initial value == next assigned
unsigned long _dispatch_queue_serial_numbers = 12;
dispatch_queue_t
dispatch_queue_create(const char *label, dispatch_queue_attr_t attr)
{
dispatch_queue_t dq;
size_t label_len;
if (!label) {
label = "";
}
label_len = strlen(label);
if (label_len < (DISPATCH_QUEUE_MIN_LABEL_SIZE - 1)) {
label_len = (DISPATCH_QUEUE_MIN_LABEL_SIZE - 1);
}
// XXX switch to malloc()
dq = (dispatch_queue_t)_dispatch_alloc(DISPATCH_VTABLE(queue),
sizeof(struct dispatch_queue_s) - DISPATCH_QUEUE_MIN_LABEL_SIZE -
DISPATCH_QUEUE_CACHELINE_PAD + label_len + 1);
_dispatch_queue_init(dq);
strcpy(dq->dq_label, label);
if (fastpath(!attr)) {
return dq;
}
if (fastpath(attr == DISPATCH_QUEUE_CONCURRENT)) {
dq->dq_width = UINT32_MAX;
dq->do_targetq = _dispatch_get_root_queue(0, false);
} else {
dispatch_debug_assert(!attr, "Invalid attribute");
}
return dq;
}
// 6618342 Contact the team that owns the Instrument DTrace probe before
// renaming this symbol
void
_dispatch_queue_dispose(dispatch_queue_t dq)
{
if (slowpath(dq == _dispatch_queue_get_current())) {
DISPATCH_CRASH("Release of a queue by itself");
}
if (slowpath(dq->dq_items_tail)) {
DISPATCH_CRASH("Release of a queue while items are enqueued");
}
// trash the tail queue so that use after free will crash
dq->dq_items_tail = (struct dispatch_object_s *)0x200;
dispatch_queue_t dqsq = dispatch_atomic_xchg2o(dq, dq_specific_q,
(dispatch_queue_t)0x200);
if (dqsq) {
_dispatch_release(dqsq);
}
}
const char *
dispatch_queue_get_label(dispatch_queue_t dq)
{
return dq->dq_label;
}
static void
_dispatch_queue_set_width2(void *ctxt)
{
int w = (int)(intptr_t)ctxt; // intentional truncation
uint32_t tmp;
dispatch_queue_t dq = _dispatch_queue_get_current();
if (w == 1 || w == 0) {
dq->dq_width = 1;
return;
}
if (w > 0) {
tmp = w;
} else switch (w) {
case DISPATCH_QUEUE_WIDTH_MAX_PHYSICAL_CPUS:
tmp = _dispatch_hw_config.cc_max_physical;
break;
case DISPATCH_QUEUE_WIDTH_ACTIVE_CPUS:
tmp = _dispatch_hw_config.cc_max_active;
break;
default:
// fall through
case DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS:
tmp = _dispatch_hw_config.cc_max_logical;
break;
}
// multiply by two since the running count is inc/dec by two
// (the low bit == barrier)
dq->dq_width = tmp * 2;
}
void
dispatch_queue_set_width(dispatch_queue_t dq, long width)
{
if (slowpath(dq->do_ref_cnt == DISPATCH_OBJECT_GLOBAL_REFCNT)) {
return;
}
dispatch_barrier_async_f(dq, (void*)(intptr_t)width,
_dispatch_queue_set_width2);
}
// 6618342 Contact the team that owns the Instrument DTrace probe before
// renaming this symbol
static void
_dispatch_set_target_queue2(void *ctxt)
{
dispatch_queue_t prev_dq, dq = (dispatch_queue_t)
_dispatch_queue_get_current();
prev_dq = dq->do_targetq;
dq->do_targetq = (dispatch_queue_t)ctxt;
_dispatch_release(prev_dq);
}
void
dispatch_set_target_queue(dispatch_object_t dou, dispatch_queue_t dq)
{
dispatch_queue_t prev_dq;
unsigned long type;
if (slowpath(dou._do->do_xref_cnt == DISPATCH_OBJECT_GLOBAL_REFCNT)) {
return;
}
type = dx_type(dou._do) & _DISPATCH_META_TYPE_MASK;
if (slowpath(!dq)) {
bool is_concurrent_q = (type == _DISPATCH_QUEUE_TYPE &&
slowpath(dou._dq->dq_width > 1));
dq = _dispatch_get_root_queue(0, !is_concurrent_q);
}
// TODO: put into the vtable
switch(type) {
case _DISPATCH_QUEUE_TYPE:
case _DISPATCH_SOURCE_TYPE:
_dispatch_retain(dq);
return dispatch_barrier_async_f(dou._dq, dq,
_dispatch_set_target_queue2);
#if WITH_DISPATCH_IO
case _DISPATCH_IO_TYPE:
return _dispatch_io_set_target_queue(dou._dchannel, dq);
#endif
default:
_dispatch_retain(dq);
dispatch_atomic_store_barrier();
prev_dq = dispatch_atomic_xchg2o(dou._do, do_targetq, dq);
if (prev_dq) _dispatch_release(prev_dq);
return;
}
}
void
dispatch_set_current_target_queue(dispatch_queue_t dq)
{
dispatch_queue_t queue = _dispatch_queue_get_current();
if (slowpath(!queue)) {
DISPATCH_CLIENT_CRASH("SPI not called from a queue");
}
if (slowpath(queue->do_xref_cnt == DISPATCH_OBJECT_GLOBAL_REFCNT)) {
DISPATCH_CLIENT_CRASH("SPI not supported on this queue");
}
if (slowpath(queue->dq_width != 1)) {
DISPATCH_CLIENT_CRASH("SPI not called from a serial queue");
}
if (slowpath(!dq)) {
dq = _dispatch_get_root_queue(0, true);
}
_dispatch_retain(dq);
_dispatch_set_target_queue2(dq);
}
#pragma mark -
#pragma mark dispatch_queue_specific
typedef TAILQ_HEAD(dispatch_queue_specific_head_s,
dispatch_queue_specific_s) dispatch_queue_specific_head_t;
struct dispatch_queue_specific_queue_s {
DISPATCH_STRUCT_HEADER(queue_specific_queue);
DISPATCH_QUEUE_HEADER;
union {
char _dqsq_pad[DISPATCH_QUEUE_MIN_LABEL_SIZE];
struct {
char dq_label[16];
dispatch_queue_specific_head_t dqsq_contexts;
};
};
};
struct dispatch_queue_specific_s {
const void *dqs_key;
void *dqs_ctxt;
dispatch_function_t dqs_destructor;
TAILQ_ENTRY(dispatch_queue_specific_s) dqs_list;
};
DISPATCH_DECL(dispatch_queue_specific);
void
_dispatch_queue_specific_queue_dispose(dispatch_queue_specific_queue_t dqsq)
{
dispatch_queue_specific_t dqs, tmp;
TAILQ_FOREACH_SAFE(dqs, &dqsq->dqsq_contexts, dqs_list, tmp) {
if (dqs->dqs_destructor) {
dispatch_async_f(_dispatch_get_root_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, false), dqs->dqs_ctxt,
dqs->dqs_destructor);
}
free(dqs);
}
_dispatch_queue_dispose((dispatch_queue_t)dqsq);
}
static void
_dispatch_queue_init_specific(dispatch_queue_t dq)
{
dispatch_queue_specific_queue_t dqsq;
dqsq = (dispatch_queue_specific_queue_t)_dispatch_alloc(
DISPATCH_VTABLE(queue_specific_queue),
sizeof(struct dispatch_queue_specific_queue_s));
_dispatch_queue_init((dispatch_queue_t)dqsq);
dqsq->do_xref_cnt = -1;
dqsq->do_targetq = _dispatch_get_root_queue(DISPATCH_QUEUE_PRIORITY_HIGH,
true);
dqsq->dq_width = UINT32_MAX;
strlcpy(dqsq->dq_label, "queue-specific", sizeof(dqsq->dq_label));
TAILQ_INIT(&dqsq->dqsq_contexts);
dispatch_atomic_store_barrier();
if (slowpath(!dispatch_atomic_cmpxchg2o(dq, dq_specific_q, NULL,
(dispatch_queue_t)dqsq))) {
_dispatch_release((dispatch_queue_t)dqsq);
}
}
static void
_dispatch_queue_set_specific(void *ctxt)
{
dispatch_queue_specific_t dqs, dqsn = (dispatch_queue_specific_t)ctxt;
dispatch_queue_specific_queue_t dqsq =
(dispatch_queue_specific_queue_t)_dispatch_queue_get_current();
TAILQ_FOREACH(dqs, &dqsq->dqsq_contexts, dqs_list) {
if (dqs->dqs_key == dqsn->dqs_key) {
// Destroy previous context for existing key
if (dqs->dqs_destructor) {
dispatch_async_f(_dispatch_get_root_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, false), dqs->dqs_ctxt,
dqs->dqs_destructor);
}
if (dqsn->dqs_ctxt) {
// Copy new context for existing key
dqs->dqs_ctxt = dqsn->dqs_ctxt;
dqs->dqs_destructor = dqsn->dqs_destructor;
} else {
// Remove context storage for existing key
TAILQ_REMOVE(&dqsq->dqsq_contexts, dqs, dqs_list);
free(dqs);
}
return free(dqsn);
}
}
// Insert context storage for new key
TAILQ_INSERT_TAIL(&dqsq->dqsq_contexts, dqsn, dqs_list);
}
DISPATCH_NOINLINE
void
dispatch_queue_set_specific(dispatch_queue_t dq, const void *key,
void *ctxt, dispatch_function_t destructor)
{
if (slowpath(!key)) {
return;
}
dispatch_queue_specific_t dqs;
dqs = (dispatch_queue_specific_t)
calloc(1, sizeof(struct dispatch_queue_specific_s));
dqs->dqs_key = key;
dqs->dqs_ctxt = ctxt;
dqs->dqs_destructor = destructor;
if (slowpath(!dq->dq_specific_q)) {
_dispatch_queue_init_specific(dq);
}
dispatch_barrier_async_f(dq->dq_specific_q, dqs,
_dispatch_queue_set_specific);
}
static void
_dispatch_queue_get_specific(void *ctxt)
{
void **ctxtp = (void **)ctxt;
void *key = *ctxtp;
dispatch_queue_specific_queue_t dqsq =
(dispatch_queue_specific_queue_t)_dispatch_queue_get_current();
dispatch_queue_specific_t dqs;
TAILQ_FOREACH(dqs, &dqsq->dqsq_contexts, dqs_list) {
if (dqs->dqs_key == key) {
*ctxtp = dqs->dqs_ctxt;
return;
}
}
*ctxtp = NULL;
}