/
queue.c
1968 lines (1716 loc) · 54.4 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-2009 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 defined(_MSC_VER)
#define __PRETTY_FUNCTION__ __FUNCSIG__
#endif
static dispatch_once_t _dispatch_root_queues_pred;
static void _dispatch_root_queues_init(void *);
void
dummy_function(void)
{
}
long
dummy_function_r0(void)
{
return 0;
}
static struct dispatch_semaphore_s _dispatch_thread_mediator[] = {
{
/*.do_vtable = */ &_dispatch_semaphore_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
},
{
/*.do_vtable = */ &_dispatch_semaphore_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
},
{
/*.do_vtable = */ &_dispatch_semaphore_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
},
{
/*.do_vtable = */ &_dispatch_semaphore_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
},
{
/*.do_vtable = */ &_dispatch_semaphore_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
},
{
/*.do_vtable = */ &_dispatch_semaphore_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
},
};
static DISPATCH_INLINE dispatch_queue_t
_dispatch_get_root_queue(long priority, bool overcommit)
{
if (overcommit) switch (priority) {
case DISPATCH_QUEUE_PRIORITY_LOW:
return &_dispatch_root_queues[1];
case DISPATCH_QUEUE_PRIORITY_DEFAULT:
return &_dispatch_root_queues[3];
case DISPATCH_QUEUE_PRIORITY_HIGH:
return &_dispatch_root_queues[5];
}
switch (priority) {
case DISPATCH_QUEUE_PRIORITY_LOW:
return &_dispatch_root_queues[0];
case DISPATCH_QUEUE_PRIORITY_DEFAULT:
return &_dispatch_root_queues[2];
case DISPATCH_QUEUE_PRIORITY_HIGH:
return &_dispatch_root_queues[4];
default:
return NULL;
}
}
#ifdef __BLOCKS__
dispatch_block_t
_dispatch_Block_copy(dispatch_block_t db)
{
dispatch_block_t rval;
while (!(rval = Block_copy(db))) {
sleep(1);
}
return rval;
}
#define _dispatch_Block_copy(x) ((typeof(x))_dispatch_Block_copy(x))
void
_dispatch_call_block_and_release(void *block)
{
void (^b)(void) = block;
b();
Block_release(b);
}
void
_dispatch_call_block_and_release2(void *block, void *ctxt)
{
void (^b)(void*) = block;
b(ctxt);
Block_release(b);
}
#endif /* __BLOCKS__ */
struct dispatch_queue_attr_vtable_s {
DISPATCH_VTABLE_HEADER(dispatch_queue_attr_s);
};
struct dispatch_queue_attr_s {
DISPATCH_STRUCT_HEADER(dispatch_queue_attr_s, dispatch_queue_attr_vtable_s);
#ifndef DISPATCH_NO_LEGACY
// Public:
int qa_priority;
void* finalizer_ctxt;
dispatch_queue_finalizer_function_t finalizer_func;
// Private:
unsigned long qa_flags;
#endif
};
#if !TARGET_OS_WIN32
static int _dispatch_pthread_sigmask(int how, sigset_t *set, sigset_t *oset);
#endif
#define _dispatch_queue_trylock(dq) dispatch_atomic_cmpxchg(&(dq)->dq_running, 0, 1)
static DISPATCH_INLINE void _dispatch_queue_unlock(dispatch_queue_t dq);
static void _dispatch_queue_invoke(dispatch_queue_t dq);
static bool _dispatch_queue_wakeup_global(dispatch_queue_t dq);
static struct dispatch_object_s *_dispatch_queue_concurrent_drain_one(dispatch_queue_t dq);
const struct dispatch_queue_vtable_s _dispatch_queue_vtable = {
/*.do_type = */ DISPATCH_QUEUE_TYPE,
/*.do_kind = */ "queue",
/*.do_debug = */ dispatch_queue_debug,
/*.do_invoke = */ (struct dispatch_queue_s *(*)(struct dispatch_queue_s *))dummy_function_r0,
/*.do_probe = */ (bool(*)(struct dispatch_queue_s*))dummy_function_r0,
/*.do_dispose = */ _dispatch_queue_dispose,
};
static const struct dispatch_queue_vtable_s _dispatch_queue_root_vtable = {
/*.do_type = */ DISPATCH_QUEUE_GLOBAL_TYPE,
/*.do_kind = */ "global-queue",
/*.do_debug = */ dispatch_queue_debug,
/*.do_invoke = */ 0,
/*.do_probe = */ _dispatch_queue_wakeup_global,
/*.do_dispose = */ 0,
};
#define MAX_THREAD_COUNT 255
struct dispatch_root_queue_context_s {
#if HAVE_PTHREAD_WORKQUEUES
pthread_workqueue_t dgq_kworkqueue;
#endif
intptr_t dgq_pending;
intptr_t dgq_thread_pool_size;
dispatch_semaphore_t dgq_thread_mediator;
};
static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
{
#if HAVE_PTHREAD_WORKQUEUES
/*.dgq_kworkqueue = */ 0,
#endif
/*.dgq_pending = */ 0,
/*.dgq_thread_pool_size = */ MAX_THREAD_COUNT,
/*.dgq_thread_mediator = */ &_dispatch_thread_mediator[0],
},
{
#if HAVE_PTHREAD_WORKQUEUES
/*.dgq_kworkqueue = */ 0,
#endif
/*.dgq_pending = */ 0,
/*.dgq_thread_pool_size = */ MAX_THREAD_COUNT,
/*.dgq_thread_mediator = */ &_dispatch_thread_mediator[1],
},
{
#if HAVE_PTHREAD_WORKQUEUES
/*.dgq_kworkqueue = */ 0,
#endif
/*.dgq_pending = */ 0,
/*.dgq_thread_pool_size = */ MAX_THREAD_COUNT,
/*.dgq_thread_mediator = */ &_dispatch_thread_mediator[2],
},
{
#if HAVE_PTHREAD_WORKQUEUES
/*.dgq_kworkqueue = */ 0,
#endif
/*.dgq_pending = */ 0,
/*.dgq_thread_pool_size = */ MAX_THREAD_COUNT,
/*.dgq_thread_mediator = */ &_dispatch_thread_mediator[3],
},
{
#if HAVE_PTHREAD_WORKQUEUES
/*.dgq_kworkqueue = */ 0,
#endif
/*.dgq_pending = */ 0,
/*.dgq_thread_pool_size = */ MAX_THREAD_COUNT,
/*.dgq_thread_mediator = */ &_dispatch_thread_mediator[4],
},
{
#if HAVE_PTHREAD_WORKQUEUES
/*.dgq_kworkqueue = */ 0,
#endif
/*.dgq_pending = */ 0,
/*.dgq_thread_pool_size = */ MAX_THREAD_COUNT,
/*.dgq_thread_mediator = */ &_dispatch_thread_mediator[5],
},
};
// 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
struct dispatch_queue_s _dispatch_root_queues[] = {
{
/*.do_vtable = */ &_dispatch_queue_root_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_suspend_cnt = */ DISPATCH_OBJECT_SUSPEND_LOCK,
/*.do_targetq = */ 0,
/*.do_ctxt = */ &_dispatch_root_queue_contexts[0],
/*.do_finalizer = */ 0,
/*.dq_running = */ 2,
/*.dq_width = */ INTPTR_MAX,
/*.dq_items_tail = */ 0,
/*.dq_items_head = */ 0,
/*.dq_serialnum = */ 4,
/*.dq_finalizer_ctxt = */ 0,
/*.dq_manually_drained = */ 0,
/*.dq_is_manually_draining = */ false,
/*.dq_label = */ "com.apple.root.low-priority",
},
{
/*.do_vtable = */ &_dispatch_queue_root_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_suspend_cnt = */ DISPATCH_OBJECT_SUSPEND_LOCK,
/*.do_targetq = */ 0,
/*.do_ctxt = */ &_dispatch_root_queue_contexts[1],
/*.do_finalizer = */ 0,
/*.dq_running = */ 2,
/*.dq_width = */ INTPTR_MAX,
/*.dq_items_tail = */ 0,
/*.dq_items_head = */ 0,
/*.dq_serialnum = */ 5,
/*.dq_finalizer_ctxt = */ 0,
/*.dq_manually_drained = */ 0,
/*.dq_is_manually_draining = */ false,
/*.dq_label = */ "com.apple.root.low-overcommit-priority",
},
{
/*.do_vtable = */ &_dispatch_queue_root_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_suspend_cnt = */ DISPATCH_OBJECT_SUSPEND_LOCK,
/*.do_targetq = */ 0,
/*.do_ctxt = */ &_dispatch_root_queue_contexts[2],
/*.do_finalizer = */ 0,
/*.dq_running = */ 2,
/*.dq_width = */ INTPTR_MAX,
/*.dq_items_tail = */ 0,
/*.dq_items_head = */ 0,
/*.dq_serialnum = */ 6,
/*.dq_finalizer_ctxt = */ 0,
/*.dq_manually_drained = */ 0,
/*.dq_is_manually_draining = */ false,
/*.dq_label = */ "com.apple.root.default-priority",
},
{
/*.do_vtable = */ &_dispatch_queue_root_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_suspend_cnt = */ DISPATCH_OBJECT_SUSPEND_LOCK,
/*.do_targetq = */ 0,
/*.do_ctxt = */ &_dispatch_root_queue_contexts[3],
/*.do_finalizer = */ 0,
/*.dq_running = */ 2,
/*.dq_width = */ INTPTR_MAX,
/*.dq_items_tail = */ 0,
/*.dq_items_head = */ 0,
/*.dq_serialnum = */ 7,
/*.dq_finalizer_ctxt = */ 0,
/*.dq_manually_drained = */ 0,
/*.dq_is_manually_draining = */ false,
/*.dq_label = */ "com.apple.root.default-overcommit-priority",
},
{
/*.do_vtable = */ &_dispatch_queue_root_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_suspend_cnt = */ DISPATCH_OBJECT_SUSPEND_LOCK,
/*.do_targetq = */ 0,
/*.do_ctxt = */ &_dispatch_root_queue_contexts[4],
/*.do_finalizer = */ 0,
/*.dq_running = */ 2,
/*.dq_width = */ INTPTR_MAX,
/*.dq_items_tail = */ 0,
/*.dq_items_head = */ 0,
/*.dq_serialnum = */ 8,
/*.dq_finalizer_ctxt = */ 0,
/*.dq_manually_drained = */ 0,
/*.dq_is_manually_draining = */ false,
/*.dq_label = */ "com.apple.root.high-priority",
},
{
/*.do_vtable = */ &_dispatch_queue_root_vtable,
/*.do_next = */ 0,
/*.do_ref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_xref_cnt = */ DISPATCH_OBJECT_GLOBAL_REFCNT,
/*.do_suspend_cnt = */ DISPATCH_OBJECT_SUSPEND_LOCK,
/*.do_targetq = */ 0,
/*.do_ctxt = */ &_dispatch_root_queue_contexts[5],
/*.do_finalizer = */ 0,
/*.dq_running = */ 2,
/*.dq_width = */ INTPTR_MAX,
/*.dq_items_tail = */ 0,
/*.dq_items_head = */ 0,
/*.dq_serialnum = */ 9,
/*.dq_finalizer_ctxt = */ 0,
/*.dq_manually_drained = */ 0,
/*.dq_is_manually_draining = */ false,
/*.dq_label = */ "com.apple.root.high-overcommit-priority",
},
};
#if DISPATCH_PERF_MON
static OSSpinLock _dispatch_stats_lock;
static uintptr_t _dispatch_bad_ratio;
static struct {
uint64_t time_total;
uint64_t count_total;
uint64_t thread_total;
} _dispatch_stats[65]; // ffs*/fls*() returns zero when no bits are set
static void _dispatch_queue_merge_stats(uint64_t start);
#endif
static void *_dispatch_worker_thread(void *context);
static void _dispatch_worker_thread2(void *context);
malloc_zone_t *_dispatch_ccache_zone;
static DISPATCH_INLINE void
_dispatch_continuation_free(dispatch_continuation_t dc)
{
dispatch_continuation_t prev_dc = _dispatch_thread_getspecific(dispatch_cache_key);
dc->do_next = prev_dc;
_dispatch_thread_setspecific(dispatch_cache_key, dc);
}
static DISPATCH_INLINE void
_dispatch_continuation_pop(dispatch_object_t dou)
{
dispatch_continuation_t dc = dou._dc;
dispatch_group_t dg;
if (DISPATCH_OBJ_IS_VTABLE(dou._do)) {
_dispatch_queue_invoke(dou._dq);
return;
}
// Add the item back to the cache before calling the function. This
// allows the 'hot' continuation to be used for a quick callback.
//
// The ccache version is per-thread.
// Therefore, the object has not been reused yet.
// This generates better assembly.
if ((uintptr_t)dou._do->do_vtable & DISPATCH_OBJ_ASYNC_BIT) {
_dispatch_continuation_free(dc);
}
if ((uintptr_t)dou._do->do_vtable & DISPATCH_OBJ_GROUP_BIT) {
dg = dc->dc_group;
} else {
dg = NULL;
}
dc->dc_func(dc->dc_ctxt);
if (dg) {
dispatch_group_leave(dg);
_dispatch_release(as_do(dg));
}
}
struct dispatch_object_s *
_dispatch_queue_concurrent_drain_one(dispatch_queue_t dq)
{
struct dispatch_object_s *head, *next, *const mediator = (void *)(uintptr_t)~0ul;
// The mediator value acts both as a "lock" and a signal
head = dispatch_atomic_xchg_pointer(&dq->dq_items_head, mediator);
if (slowpath(head == NULL)) {
// The first xchg on the tail will tell the enqueueing thread that it
// is safe to blindly write out to the head pointer. A cmpxchg honors
// the algorithm.
#ifdef __GNUC__
(void)
#endif
dispatch_atomic_cmpxchg_pointer(&dq->dq_items_head, mediator, NULL);
_dispatch_debug("no work on global work queue");
return NULL;
}
if (slowpath(head == mediator)) {
// This thread lost the race for ownership of the queue.
//
// The ratio of work to libdispatch overhead must be bad. This
// scenario implies that there are too many threads in the pool.
// Create a new pending thread and then exit this thread.
// The kernel will grant a new thread when the load subsides.
_dispatch_debug("Contention on queue: %p", dq);
_dispatch_queue_wakeup_global(dq);
#if DISPATCH_PERF_MON
dispatch_atomic_inc((intptr_t*)&_dispatch_bad_ratio);
#endif
return NULL;
}
// Restore the head pointer to a sane value before returning.
// If 'next' is NULL, then this item _might_ be the last item.
next = fastpath(head->do_next);
if (slowpath(!next)) {
dq->dq_items_head = NULL;
if (dispatch_atomic_cmpxchg_pointer(&dq->dq_items_tail, head, NULL)) {
// both head and tail are NULL now
goto out;
}
// There must be a next item now. This thread won't wait long.
while (!(next = head->do_next)) {
_dispatch_hardware_pause();
}
}
dq->dq_items_head = next;
_dispatch_queue_wakeup_global(dq);
out:
return head;
}
dispatch_queue_t
dispatch_get_current_queue(void)
{
dispatch_queue_t q = _dispatch_queue_get_current();
return q ? q : _dispatch_get_root_queue(0, true);
}
struct _dispatch_hw_config_s _dispatch_hw_config;
static void
_dispatch_queue_set_width_init(void)
{
#ifdef __APPLE__
size_t valsz = sizeof(uint32_t);
int ret;
ret = sysctlbyname("hw.activecpu", &_dispatch_hw_config.cc_max_active,
&valsz, NULL, 0);
(void)dispatch_assume_zero(ret);
(void)dispatch_assume(valsz == sizeof(uint32_t));
ret = sysctlbyname("hw.logicalcpu_max",
&_dispatch_hw_config.cc_max_logical, &valsz, NULL, 0);
(void)dispatch_assume_zero(ret);
(void)dispatch_assume(valsz == sizeof(uint32_t));
ret = sysctlbyname("hw.physicalcpu_max",
&_dispatch_hw_config.cc_max_physical, &valsz, NULL, 0);
(void)dispatch_assume_zero(ret);
(void)dispatch_assume(valsz == sizeof(uint32_t));
#elif defined(__FreeBSD__)
size_t valsz = sizeof(uint32_t);
int ret;
ret = sysctlbyname("kern.smp.cpus", &_dispatch_hw_config.cc_max_active,
&valsz, NULL, 0);
(void)dispatch_assume_zero(ret);
(void)dispatch_assume(valsz == sizeof(uint32_t));
_dispatch_hw_config.cc_max_logical =
_dispatch_hw_config.cc_max_physical =
_dispatch_hw_config.cc_max_active;
#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
int ret;
ret = sysconf(_SC_NPROCESSORS_ONLN);
_dispatch_hw_config.cc_max_logical =
_dispatch_hw_config.cc_max_physical =
_dispatch_hw_config.cc_max_active = (ret < 0) ? 1 : ret;
#elif defined( TARGET_OS_WIN32 ) && !defined( WINOBJC )
typedef DWORD(__stdcall *GetProcessorCountFn)(WORD);
typedef VOID(__stdcall *GetCurrentProcessorNumberExFn)(PROCESSOR_NUMBER*);
GetProcessorCountFn getMaximumProcessorCount = (GetProcessorCountFn)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetMaximumProcessorCount");
GetProcessorCountFn getActiveProcessorCount = (GetProcessorCountFn)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetActiveProcessorCount");
GetCurrentProcessorNumberExFn getCurrentProcessorNumberEx = (GetCurrentProcessorNumberExFn)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetCurrentProcessorNumberEx");
if(getMaximumProcessorCount && getActiveProcessorCount && getCurrentProcessorNumberEx) {
PROCESSOR_NUMBER pn = {0};
getCurrentProcessorNumberEx(&pn);
_dispatch_hw_config.cc_max_logical = (uint32_t)getMaximumProcessorCount(pn.Group);
_dispatch_hw_config.cc_max_physical = (uint32_t)getMaximumProcessorCount(pn.Group);
_dispatch_hw_config.cc_max_active = (uint32_t)getActiveProcessorCount(pn.Group);
} else {
SYSTEM_INFO si = {0};
GetSystemInfo(&si);
_dispatch_hw_config.cc_max_logical = si.dwNumberOfProcessors;
_dispatch_hw_config.cc_max_physical = si.dwNumberOfProcessors;
_dispatch_hw_config.cc_max_active = si.dwNumberOfProcessors;
}
#else
#warning "_dispatch_queue_set_width_init: no supported way to query CPU count"
_dispatch_hw_config.cc_max_logical =
_dispatch_hw_config.cc_max_physical =
_dispatch_hw_config.cc_max_active = 1;
#endif
}
void
dispatch_queue_set_width(dispatch_queue_t dq, long width)
{
int w = (int)width; // intentional truncation
uint32_t tmp;
if (slowpath(dq->do_ref_cnt == DISPATCH_OBJECT_GLOBAL_REFCNT)) {
return;
}
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;
// XXX if the queue has items and the width is increased, we should try to wake the queue
}
// skip zero
// 1 - main_q
// 2 - mgr_q
// 3 - _unused_
// 4,5,6,7,8,9 - global queues
// we use 'xadd' on Intel, so the initial value == next assigned
static intptr_t _dispatch_queue_serial_numbers = 10;
// Note to later developers: ensure that any initialization changes are
// made for statically allocated queues (i.e. _dispatch_main_q).
/*DISPATCH_INLINE*/ void
_dispatch_queue_init(dispatch_queue_t dq)
{
dq->do_vtable = &_dispatch_queue_vtable;
dq->do_next = DISPATCH_OBJECT_LISTLESS;
dq->do_ref_cnt = 1;
dq->do_xref_cnt = 1;
dq->do_targetq = _dispatch_get_root_queue(0, true);
dq->dq_running = 0;
dq->dq_width = 1;
dq->dq_serialnum = dispatch_atomic_inc(&_dispatch_queue_serial_numbers) - 1;
dq->dq_manually_drained = 0;
dq->dq_is_manually_draining = false;
}
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 = calloc(1ul, sizeof(struct dispatch_queue_s) - DISPATCH_QUEUE_MIN_LABEL_SIZE + label_len + 1);
if (slowpath(!dq)) {
return dq;
}
_dispatch_queue_init(dq);
strcpy(dq->dq_label, label);
#ifndef DISPATCH_NO_LEGACY
if (slowpath(attr)) {
dq->do_targetq = _dispatch_get_root_queue(attr->qa_priority, attr->qa_flags & DISPATCH_QUEUE_OVERCOMMIT);
dq->dq_finalizer_ctxt = attr->finalizer_ctxt;
dq->dq_finalizer_func = attr->finalizer_func;
#ifdef __BLOCKS__
if (attr->finalizer_func == (void*)_dispatch_call_block_and_release2) {
// if finalizer_ctxt is a Block, retain it.
dq->dq_finalizer_ctxt = Block_copy(dq->dq_finalizer_ctxt);
if (!(dq->dq_finalizer_ctxt)) {
goto out_bad;
}
}
#endif
}
#else
(void)attr;
#endif
return dq;
#if !defined(DISPATCH_NO_LEGACY) && defined(__BLOCKS__)
out_bad:
free(dq);
return NULL;
#endif
}
// 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");
}
#ifndef DISPATCH_NO_LEGACY
if (dq->dq_finalizer_func) {
dq->dq_finalizer_func(dq->dq_finalizer_ctxt, dq);
}
#endif
// trash the tail queue so that use after free will crash
dq->dq_items_tail = (void *)(uintptr_t)0x200;
_dispatch_dispose(as_do(dq));
}
DISPATCH_NOINLINE
void
_dispatch_queue_push_list_slow(dispatch_queue_t dq, struct dispatch_object_s *obj)
{
// The queue must be retained before dq_items_head is written in order
// to ensure that the reference is still valid when _dispatch_wakeup is
// called. Otherwise, if preempted between the assignment to
// dq_items_head and _dispatch_wakeup, the blocks submitted to the
// queue may release the last reference to the queue when invoked by
// _dispatch_queue_drain. <rdar://problem/6932776>
_dispatch_retain(as_do(dq));
dq->dq_items_head = obj;
_dispatch_wakeup(as_do(dq));
_dispatch_release(as_do(dq));
}
DISPATCH_NOINLINE
static void
_dispatch_barrier_async_f_slow(dispatch_queue_t dq, void *context, dispatch_function_t func)
{
dispatch_continuation_t dc = fastpath(_dispatch_continuation_alloc_from_heap());
dc->do_vtable = (void *)(uintptr_t)(DISPATCH_OBJ_ASYNC_BIT | DISPATCH_OBJ_BARRIER_BIT);
dc->dc_func = func;
dc->dc_ctxt = context;
_dispatch_queue_push(dq, as_do(dc));
}
#ifdef __BLOCKS__
void
dispatch_barrier_async(dispatch_queue_t dq, void (^work)(void))
{
dispatch_barrier_async_f(dq, _dispatch_Block_copy(work), _dispatch_call_block_and_release);
}
#endif
DISPATCH_NOINLINE
void
dispatch_barrier_async_f(dispatch_queue_t dq, void *context, dispatch_function_t func)
{
dispatch_continuation_t dc = fastpath(_dispatch_continuation_alloc_cacheonly());
if (!dc) {
_dispatch_barrier_async_f_slow(dq, context, func);
return;
}
dc->do_vtable = (void *)(uintptr_t)(DISPATCH_OBJ_ASYNC_BIT | DISPATCH_OBJ_BARRIER_BIT);
dc->dc_func = func;
dc->dc_ctxt = context;
_dispatch_queue_push(dq, as_do(dc));
}
DISPATCH_NOINLINE
static void
_dispatch_async_f_slow(dispatch_queue_t dq, void *context, dispatch_function_t func)
{
dispatch_continuation_t dc = fastpath(_dispatch_continuation_alloc_from_heap());
dc->do_vtable = (void *)(uintptr_t)DISPATCH_OBJ_ASYNC_BIT;
dc->dc_func = func;
dc->dc_ctxt = context;
_dispatch_queue_push(dq, as_do(dc));
}
#ifdef __BLOCKS__
void
dispatch_async(dispatch_queue_t dq, void (^work)(void))
{
dispatch_async_f(dq, _dispatch_Block_copy(work), _dispatch_call_block_and_release);
}
#endif
DISPATCH_NOINLINE
void
dispatch_async_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
{
dispatch_continuation_t dc = fastpath(_dispatch_continuation_alloc_cacheonly());
// unlike dispatch_sync_f(), we do NOT need to check the queue width,
// the "drain" function will do this test
if (!dc) {
_dispatch_async_f_slow(dq, ctxt, func);
return;
}
dc->do_vtable = (void *)(uintptr_t)DISPATCH_OBJ_ASYNC_BIT;
dc->dc_func = func;
dc->dc_ctxt = ctxt;
_dispatch_queue_push(dq, as_do(dc));
}
struct dispatch_barrier_sync_slow2_s {
dispatch_queue_t dbss2_dq;
dispatch_function_t dbss2_func;
dispatch_function_t dbss2_ctxt;
dispatch_semaphore_t dbss2_sema;
};
static void
_dispatch_barrier_sync_f_slow_invoke(void *ctxt)
{
struct dispatch_barrier_sync_slow2_s *dbss2 = ctxt;
dispatch_assert(dbss2->dbss2_dq == dispatch_get_current_queue());
// ALL blocks on thread-affinitized queues drain in those threads
if (dbss2->dbss2_dq->dq_manually_drained) {
dbss2->dbss2_func(dbss2->dbss2_ctxt);
} else {
dispatch_suspend(as_do(dbss2->dbss2_dq));
}
dispatch_semaphore_signal(dbss2->dbss2_sema);
}
DISPATCH_NOINLINE
static void
_dispatch_barrier_sync_f_slow(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
{
// It's preferred to execute synchronous blocks on the current thread
// due to thread-local side effects, garbage collection, etc. However,
// blocks submitted to the main thread MUST be run on the main thread
struct dispatch_barrier_sync_slow2_s dbss2 = {
/*.dbss2_dq = */ dq,
/*.dbss2_func = */ func,
/*.dbss2_ctxt = */ ctxt,
/*.dbss2_sema = */ _dispatch_get_thread_semaphore(),
};
struct dispatch_barrier_sync_slow_s {
DISPATCH_CONTINUATION_HEADER(dispatch_barrier_sync_slow_s);
} dbss = {
/*.do_vtable = */ (void *)(uintptr_t)DISPATCH_OBJ_BARRIER_BIT,
/*.do_next = */ 0,
/*.dc_func = */ _dispatch_barrier_sync_f_slow_invoke,
/*.dc_ctxt = */ &dbss2,
};
dispatch_queue_t old_dq = _dispatch_thread_getspecific(dispatch_queue_key);
_dispatch_queue_push(dq, as_do((void *)&dbss));
dispatch_semaphore_wait(dbss2.dbss2_sema, DISPATCH_TIME_FOREVER);
if (!dq->dq_manually_drained) {
_dispatch_thread_setspecific(dispatch_queue_key, dq);
func(ctxt);
_dispatch_workitem_inc();
_dispatch_thread_setspecific(dispatch_queue_key, old_dq);
dispatch_resume(as_do(dq));
}
_dispatch_put_thread_semaphore(dbss2.dbss2_sema);
}
#ifdef __BLOCKS__
void
dispatch_barrier_sync(dispatch_queue_t dq, void (^work)(void))
{
// Blocks submitted to the main queue MUST be run on the main thread,
// therefore we must Block_copy in order to notify the thread-local
// garbage collector that the objects are transferring to the main thread
if (dq->dq_manually_drained) {
dispatch_block_t block = Block_copy(work);
return dispatch_barrier_sync_f(dq, block, _dispatch_call_block_and_release);
}
struct Block_basic *bb = (void *)work;
dispatch_barrier_sync_f(dq, work, (dispatch_function_t)bb->Block_invoke);
}
#endif
DISPATCH_NOINLINE
void
dispatch_barrier_sync_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
{
dispatch_queue_t old_dq = _dispatch_thread_getspecific(dispatch_queue_key);
// 1) ensure that this thread hasn't enqueued anything ahead of this call
// 2) the queue is not suspended
// 3) the queue is not weird
if (slowpath(dq->dq_items_tail)
|| slowpath(DISPATCH_OBJECT_SUSPENDED(dq))
|| slowpath(!_dispatch_queue_trylock(dq))) {
_dispatch_barrier_sync_f_slow(dq, ctxt, func);
return;
}
_dispatch_thread_setspecific(dispatch_queue_key, dq);
func(ctxt);
_dispatch_workitem_inc();
_dispatch_thread_setspecific(dispatch_queue_key, old_dq);
_dispatch_queue_unlock(dq);
}
static void
_dispatch_sync_f_slow2(void *ctxt)
{
dispatch_queue_t dq = _dispatch_queue_get_current();
dispatch_atomic_add(&dq->dq_running, 2);
dispatch_semaphore_signal(ctxt);
}
DISPATCH_NOINLINE
static void
_dispatch_sync_f_slow(dispatch_queue_t dq)
{
// the global root queues do not need strict ordering
if (dq->do_targetq == NULL) {
dispatch_atomic_add(&dq->dq_running, 2);
return;
}
else
{
struct dispatch_sync_slow_s {
DISPATCH_CONTINUATION_HEADER(dispatch_sync_slow_s);
} dss = {
.do_vtable = NULL,
.do_next = 0,
.dc_func = _dispatch_sync_f_slow2,
.dc_ctxt = _dispatch_get_thread_semaphore(),
};
// XXX FIXME -- concurrent queues can be come serial again
_dispatch_queue_push(dq, as_do((void *)&dss));
dispatch_semaphore_wait((dispatch_semaphore_t)dss.dc_ctxt, DISPATCH_TIME_FOREVER);
_dispatch_put_thread_semaphore((dispatch_semaphore_t)dss.dc_ctxt);
}
}
#ifdef __BLOCKS__
void
dispatch_sync(dispatch_queue_t dq, void (^work)(void))
{
struct Block_basic *bb = (void *)work;
dispatch_sync_f(dq, work, (dispatch_function_t)bb->Block_invoke);
}
#endif
DISPATCH_NOINLINE
void
dispatch_sync_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
{
uintptr_t prev_cnt;
dispatch_queue_t old_dq;
if (dq->dq_width == 1) {
dispatch_barrier_sync_f(dq, ctxt, func);
return;
}
// 1) ensure that this thread hasn't enqueued anything ahead of this call
// 2) the queue is not suspended
if (slowpath(dq->dq_items_tail) || slowpath(DISPATCH_OBJECT_SUSPENDED(dq))) {
_dispatch_sync_f_slow(dq);
} else {
prev_cnt = dispatch_atomic_add(&dq->dq_running, 2) - 2;
if (slowpath(prev_cnt & 1)) {
if (dispatch_atomic_sub(&dq->dq_running, 2) == 0) {
_dispatch_wakeup(as_do(dq));
}
_dispatch_sync_f_slow(dq);
}
}
old_dq = _dispatch_thread_getspecific(dispatch_queue_key);
_dispatch_thread_setspecific(dispatch_queue_key, dq);
func(ctxt);
_dispatch_workitem_inc();
_dispatch_thread_setspecific(dispatch_queue_key, old_dq);
if (slowpath(dispatch_atomic_sub(&dq->dq_running, 2) == 0)) {
_dispatch_wakeup(as_do(dq));
}
}
const char *
dispatch_queue_get_label(dispatch_queue_t dq)
{
return dq->dq_label;
}
DISPATCH_NOINLINE
static void
_dispatch_queue_cleanup2(dispatch_queue_t q)
{
dispatch_atomic_dec(&q->dq_running);
if (dispatch_atomic_sub(&(q->do_suspend_cnt), DISPATCH_OBJECT_SUSPEND_LOCK) == 0) {
_dispatch_wakeup(as_do(q));
}
_dispatch_queue_cleanup_manual(q);
}
#ifndef DISPATCH_NO_LEGACY
dispatch_queue_t
dispatch_get_concurrent_queue(long pri)
{
if (pri > 0) {
pri = DISPATCH_QUEUE_PRIORITY_HIGH;
} else if (pri < 0) {
pri = DISPATCH_QUEUE_PRIORITY_LOW;
}
return _dispatch_get_root_queue(pri, false);
}