-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathsource.c
1441 lines (1297 loc) · 44 KB
/
source.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-2016 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"
static void _dispatch_source_handler_free(dispatch_source_refs_t ds, long kind);
#pragma mark -
#pragma mark dispatch_source_t
DISPATCH_ALWAYS_INLINE
static inline dispatch_continuation_t
_dispatch_source_get_handler(dispatch_source_refs_t dr, long kind)
{
return os_atomic_load(&dr->ds_handler[kind], relaxed);
}
#define _dispatch_source_get_event_handler(dr) \
_dispatch_source_get_handler(dr, DS_EVENT_HANDLER)
#define _dispatch_source_get_cancel_handler(dr) \
_dispatch_source_get_handler(dr, DS_CANCEL_HANDLER)
#define _dispatch_source_get_registration_handler(dr) \
_dispatch_source_get_handler(dr, DS_REGISTN_HANDLER)
dispatch_source_t
dispatch_source_create(dispatch_source_type_t dst, uintptr_t handle,
uintptr_t mask, dispatch_queue_t dq)
{
dispatch_source_refs_t dr;
dispatch_source_t ds;
dr = dux_create(dst, handle, mask)._dr;
if (unlikely(!dr)) {
return DISPATCH_BAD_INPUT;
}
ds = _dispatch_queue_alloc(source,
dux_type(dr)->dst_strict ? DSF_STRICT : DQF_MUTABLE, 1,
DISPATCH_QUEUE_INACTIVE | DISPATCH_QUEUE_ROLE_INNER)._ds;
ds->dq_label = "source";
ds->ds_refs = dr;
dr->du_owner_wref = _dispatch_ptr2wref(ds);
if (unlikely(!dq)) {
dq = _dispatch_get_default_queue(true);
} else {
_dispatch_retain((dispatch_queue_t _Nonnull)dq);
}
ds->do_targetq = dq;
if (dr->du_is_timer && (dr->du_timer_flags & DISPATCH_TIMER_INTERVAL)) {
dispatch_source_set_timer(ds, DISPATCH_TIME_NOW, handle, UINT64_MAX);
}
_dispatch_object_debug(ds, "%s", __func__);
return ds;
}
void
_dispatch_source_dispose(dispatch_source_t ds, bool *allow_free)
{
_dispatch_object_debug(ds, "%s", __func__);
_dispatch_trace_source_dispose(ds);
_dispatch_source_handler_free(ds->ds_refs, DS_REGISTN_HANDLER);
_dispatch_source_handler_free(ds->ds_refs, DS_EVENT_HANDLER);
_dispatch_source_handler_free(ds->ds_refs, DS_CANCEL_HANDLER);
_dispatch_unote_dispose(ds->ds_refs);
ds->ds_refs = NULL;
_dispatch_lane_class_dispose(ds, allow_free);
}
void
_dispatch_source_xref_dispose(dispatch_source_t ds)
{
dispatch_queue_flags_t dqf = _dispatch_queue_atomic_flags(ds);
if (unlikely((dqf & DSF_STRICT) && !(dqf & DSF_CANCELED) &&
_dispatch_source_get_cancel_handler(ds->ds_refs))) {
DISPATCH_CLIENT_CRASH(ds, "Release of a source that has not been "
"cancelled, but has a mandatory cancel handler");
}
dx_wakeup(ds, 0, DISPATCH_WAKEUP_MAKE_DIRTY);
}
intptr_t
dispatch_source_testcancel(dispatch_source_t ds)
{
return (bool)(ds->dq_atomic_flags & DSF_CANCELED);
}
uintptr_t
dispatch_source_get_mask(dispatch_source_t ds)
{
dispatch_source_refs_t dr = ds->ds_refs;
if (ds->dq_atomic_flags & DSF_CANCELED) {
return 0;
}
#if DISPATCH_USE_MEMORYSTATUS
if (dr->du_vmpressure_override) {
return NOTE_VM_PRESSURE;
}
#if TARGET_OS_SIMULATOR
if (dr->du_memorypressure_override) {
return NOTE_MEMORYSTATUS_PRESSURE_WARN;
}
#endif
#endif // DISPATCH_USE_MEMORYSTATUS
if (dr->du_is_timer) {
return dr->du_timer_flags;
}
return dr->du_fflags;
}
uintptr_t
dispatch_source_get_handle(dispatch_source_t ds)
{
dispatch_source_refs_t dr = ds->ds_refs;
#if TARGET_OS_SIMULATOR
if (dr->du_memorypressure_override) {
return 0;
}
#endif
if (dr->du_filter == DISPATCH_EVFILT_TIMER_WITH_CLOCK) {
switch (_dispatch_timer_flags_to_clock(dr->du_timer_flags)) {
case DISPATCH_CLOCK_UPTIME: return DISPATCH_CLOCKID_UPTIME;
case DISPATCH_CLOCK_MONOTONIC: return DISPATCH_CLOCKID_MONOTONIC;
case DISPATCH_CLOCK_WALL: return DISPATCH_CLOCKID_WALLTIME;
}
}
return dr->du_ident;
}
uintptr_t
dispatch_source_get_data(dispatch_source_t ds)
{
dispatch_source_refs_t dr = ds->ds_refs;
#if DISPATCH_USE_MEMORYSTATUS
if (dr->du_vmpressure_override) {
return NOTE_VM_PRESSURE;
}
#if TARGET_OS_SIMULATOR
if (dr->du_memorypressure_override) {
return NOTE_MEMORYSTATUS_PRESSURE_WARN;
}
#endif
#endif // DISPATCH_USE_MEMORYSTATUS
uint64_t value = os_atomic_load2o(dr, ds_data, relaxed);
return (unsigned long)(dr->du_has_extended_status ?
DISPATCH_SOURCE_GET_DATA(value) : value);
}
size_t
dispatch_source_get_extended_data(dispatch_source_t ds,
dispatch_source_extended_data_t edata, size_t size)
{
dispatch_source_refs_t dr = ds->ds_refs;
size_t target_size = MIN(size,
sizeof(struct dispatch_source_extended_data_s));
if (size > 0) {
unsigned long data, status = 0;
if (dr->du_has_extended_status) {
uint64_t combined = os_atomic_load(&dr->ds_data, relaxed);
data = DISPATCH_SOURCE_GET_DATA(combined);
status = DISPATCH_SOURCE_GET_STATUS(combined);
} else {
data = dispatch_source_get_data(ds);
}
if (size >= offsetof(struct dispatch_source_extended_data_s, data)
+ sizeof(edata->data)) {
edata->data = data;
}
if (size >= offsetof(struct dispatch_source_extended_data_s, status)
+ sizeof(edata->status)) {
edata->status = status;
}
if (size > sizeof(struct dispatch_source_extended_data_s)) {
memset(
(char *)edata + sizeof(struct dispatch_source_extended_data_s),
0, size - sizeof(struct dispatch_source_extended_data_s));
}
}
return target_size;
}
void
dispatch_source_merge_data(dispatch_source_t ds, uintptr_t val)
{
dispatch_queue_flags_t dqf = _dispatch_queue_atomic_flags(ds);
dispatch_source_refs_t dr = ds->ds_refs;
if (unlikely(dqf & (DSF_CANCELED | DQF_RELEASED))) {
return;
}
switch (dr->du_filter) {
case DISPATCH_EVFILT_CUSTOM_ADD:
os_atomic_add2o(dr, ds_pending_data, val, relaxed);
break;
case DISPATCH_EVFILT_CUSTOM_OR:
os_atomic_or2o(dr, ds_pending_data, val, relaxed);
break;
case DISPATCH_EVFILT_CUSTOM_REPLACE:
os_atomic_store2o(dr, ds_pending_data, val, relaxed);
break;
default:
DISPATCH_CLIENT_CRASH(dr->du_filter, "Invalid source type");
}
dx_wakeup(ds, 0, DISPATCH_WAKEUP_MAKE_DIRTY);
}
#pragma mark -
#pragma mark dispatch_source_handler
DISPATCH_ALWAYS_INLINE
static inline dispatch_continuation_t
_dispatch_source_handler_alloc(dispatch_source_t ds, void *func, uintptr_t kind,
bool is_block)
{
// sources don't propagate priority by default
const dispatch_block_flags_t flags =
DISPATCH_BLOCK_HAS_PRIORITY | DISPATCH_BLOCK_NO_VOUCHER;
dispatch_continuation_t dc = _dispatch_continuation_alloc();
if (func) {
uintptr_t dc_flags = 0;
if (kind != DS_EVENT_HANDLER) {
dc_flags |= DC_FLAG_CONSUME;
}
if (is_block) {
#ifdef __BLOCKS__
_dispatch_continuation_init(dc, ds, func, flags, dc_flags);
#endif /* __BLOCKS__ */
} else {
dc_flags |= DC_FLAG_FETCH_CONTEXT;
_dispatch_continuation_init_f(dc, ds, ds->do_ctxt, func, flags,
dc_flags);
}
} else {
dc->dc_flags = DC_FLAG_ALLOCATED;
dc->dc_func = NULL;
}
return dc;
}
DISPATCH_NOINLINE
static void
_dispatch_source_handler_dispose(dispatch_continuation_t dc)
{
#ifdef __BLOCKS__
if (dc->dc_flags & DC_FLAG_BLOCK) {
Block_release(dc->dc_ctxt);
}
#endif /* __BLOCKS__ */
if (dc->dc_voucher) {
_voucher_release(dc->dc_voucher);
dc->dc_voucher = VOUCHER_INVALID;
}
_dispatch_continuation_free(dc);
}
DISPATCH_ALWAYS_INLINE
static inline dispatch_continuation_t
_dispatch_source_handler_take(dispatch_source_refs_t dr, long kind)
{
return os_atomic_xchg(&dr->ds_handler[kind], NULL, relaxed);
}
DISPATCH_ALWAYS_INLINE
static inline void
_dispatch_source_handler_free(dispatch_source_refs_t dr, long kind)
{
dispatch_continuation_t dc = _dispatch_source_handler_take(dr, kind);
if (dc) _dispatch_source_handler_dispose(dc);
}
DISPATCH_ALWAYS_INLINE
static inline void
_dispatch_source_handler_replace(dispatch_source_t ds, uintptr_t kind,
dispatch_continuation_t dc)
{
if (!dc->dc_func) {
_dispatch_continuation_free(dc);
dc = NULL;
} else if (dc->dc_flags & DC_FLAG_FETCH_CONTEXT) {
dc->dc_ctxt = ds->do_ctxt;
}
dc = os_atomic_xchg(&ds->ds_refs->ds_handler[kind], dc, release);
if (dc) _dispatch_source_handler_dispose(dc);
}
DISPATCH_NOINLINE
static void
_dispatch_source_set_handler_slow(void *context)
{
dispatch_source_t ds = upcast(_dispatch_queue_get_current())._ds;
dispatch_assert(dx_type(ds) == DISPATCH_SOURCE_KEVENT_TYPE);
dispatch_continuation_t dc = context;
uintptr_t kind = (uintptr_t)dc->dc_data;
dc->dc_data = NULL;
_dispatch_source_handler_replace(ds, kind, dc);
}
DISPATCH_NOINLINE
static void
_dispatch_source_set_handler(dispatch_source_t ds, void *func,
uintptr_t kind, bool is_block)
{
dispatch_continuation_t dc;
dc = _dispatch_source_handler_alloc(ds, func, kind, is_block);
if (_dispatch_lane_try_inactive_suspend(ds)) {
_dispatch_source_handler_replace(ds, kind, dc);
return _dispatch_lane_resume(ds, false);
}
dispatch_queue_flags_t dqf = _dispatch_queue_atomic_flags(ds);
if (unlikely(dqf & DSF_STRICT)) {
DISPATCH_CLIENT_CRASH(kind, "Cannot change a handler of this source "
"after it has been activated");
}
// Ignore handlers mutations past cancelation, it's harmless
if ((dqf & DSF_CANCELED) == 0) {
_dispatch_ktrace1(DISPATCH_PERF_post_activate_mutation, ds);
if (kind == DS_REGISTN_HANDLER) {
_dispatch_bug_deprecated("Setting registration handler after "
"the source has been activated");
} else if (func == NULL) {
_dispatch_bug_deprecated("Clearing handler after "
"the source has been activated");
}
}
dc->dc_data = (void *)kind;
_dispatch_barrier_trysync_or_async_f(ds, dc,
_dispatch_source_set_handler_slow, 0);
}
#ifdef __BLOCKS__
void
dispatch_source_set_event_handler(dispatch_source_t ds,
dispatch_block_t handler)
{
_dispatch_source_set_handler(ds, handler, DS_EVENT_HANDLER, true);
}
#endif /* __BLOCKS__ */
void
dispatch_source_set_event_handler_f(dispatch_source_t ds,
dispatch_function_t handler)
{
_dispatch_source_set_handler(ds, handler, DS_EVENT_HANDLER, false);
}
#ifdef __BLOCKS__
void
dispatch_source_set_cancel_handler(dispatch_source_t ds,
dispatch_block_t handler)
{
_dispatch_source_set_handler(ds, handler, DS_CANCEL_HANDLER, true);
}
void
dispatch_source_set_mandatory_cancel_handler(dispatch_source_t ds,
dispatch_block_t handler)
{
_dispatch_queue_atomic_flags_set_and_clear(ds, DSF_STRICT, DQF_MUTABLE);
dispatch_source_set_cancel_handler(ds, handler);
}
#endif /* __BLOCKS__ */
void
dispatch_source_set_cancel_handler_f(dispatch_source_t ds,
dispatch_function_t handler)
{
_dispatch_source_set_handler(ds, handler, DS_CANCEL_HANDLER, false);
}
void
dispatch_source_set_mandatory_cancel_handler_f(dispatch_source_t ds,
dispatch_function_t handler)
{
_dispatch_queue_atomic_flags_set_and_clear(ds, DSF_STRICT, DQF_MUTABLE);
dispatch_source_set_cancel_handler_f(ds, handler);
}
#ifdef __BLOCKS__
void
dispatch_source_set_registration_handler(dispatch_source_t ds,
dispatch_block_t handler)
{
_dispatch_source_set_handler(ds, handler, DS_REGISTN_HANDLER, true);
}
#endif /* __BLOCKS__ */
void
dispatch_source_set_registration_handler_f(dispatch_source_t ds,
dispatch_function_t handler)
{
_dispatch_source_set_handler(ds, handler, DS_REGISTN_HANDLER, false);
}
#pragma mark -
#pragma mark dispatch_source_invoke
#if TARGET_OS_MAC
bool
_dispatch_source_will_reenable_kevent_4NW(dispatch_source_t ds)
{
uint64_t dq_state = os_atomic_load2o(ds, dq_state, relaxed);
if (unlikely(!_dq_state_drain_locked_by_self(dq_state))) {
DISPATCH_CLIENT_CRASH(0, "_dispatch_source_will_reenable_kevent_4NW "
"not called from within the event handler");
}
return _dispatch_unote_needs_rearm(ds->ds_refs);
}
#endif // TARGET_OS_MAC
static void
_dispatch_source_registration_callout(dispatch_source_t ds, dispatch_queue_t cq,
dispatch_invoke_flags_t flags)
{
dispatch_continuation_t dc;
dc = _dispatch_source_handler_take(ds->ds_refs, DS_REGISTN_HANDLER);
if (ds->dq_atomic_flags & (DSF_CANCELED | DQF_RELEASED)) {
// no registration callout if source is canceled rdar://problem/8955246
return _dispatch_source_handler_dispose(dc);
}
if (dc->dc_flags & DC_FLAG_FETCH_CONTEXT) {
dc->dc_ctxt = ds->do_ctxt;
}
_dispatch_trace_source_callout_entry(ds, DS_REGISTN_HANDLER, cq, dc);
_dispatch_continuation_pop(dc, NULL, flags, cq);
}
static void
_dispatch_source_cancel_callout(dispatch_source_t ds, dispatch_queue_t cq,
dispatch_invoke_flags_t flags)
{
dispatch_source_refs_t dr = ds->ds_refs;
dispatch_continuation_t dc;
dc = _dispatch_source_handler_take(dr, DS_CANCEL_HANDLER);
dr->ds_pending_data = 0;
dr->ds_data = 0;
_dispatch_source_handler_free(dr, DS_EVENT_HANDLER);
_dispatch_source_handler_free(dr, DS_REGISTN_HANDLER);
if (!dc) {
return;
}
if (!(ds->dq_atomic_flags & DSF_CANCELED)) {
return _dispatch_source_handler_dispose(dc);
}
if (dc->dc_flags & DC_FLAG_FETCH_CONTEXT) {
dc->dc_ctxt = ds->do_ctxt;
}
_dispatch_trace_source_callout_entry(ds, DS_CANCEL_HANDLER, cq, dc);
_dispatch_continuation_pop(dc, NULL, flags, cq);
}
DISPATCH_ALWAYS_INLINE
static inline bool
_dispatch_source_refs_needs_configuration(dispatch_unote_t du)
{
return du._du->du_is_timer &&
os_atomic_load2o(du._dt, dt_pending_config, relaxed);
}
DISPATCH_ALWAYS_INLINE
static inline bool
_dispatch_source_refs_needs_rearm(dispatch_unote_t du)
{
if (!du._du->du_is_timer) {
return _dispatch_unote_needs_rearm(du);
}
if (os_atomic_load2o(du._dt, dt_pending_config, relaxed)) {
return true;
}
if (_dispatch_unote_needs_rearm(du)) {
return du._dt->dt_timer.target < INT64_MAX;
}
return false;
}
DISPATCH_ALWAYS_INLINE
static inline unsigned long
_dispatch_source_timer_data(dispatch_timer_source_refs_t dr, uint64_t prev)
{
unsigned long data = (unsigned long)prev >> 1;
// The timer may be in _dispatch_source_invoke2() already for other
// reasons such as running the registration handler when ds_pending_data
// is changed by _dispatch_timers_run2() without holding the drain lock.
//
// We hence need dependency ordering to pair with the release barrier
// done by _dispatch_timers_run2() when setting the DISARMED_MARKER bit.
os_atomic_thread_fence(dependency);
dr = os_atomic_force_dependency_on(dr, data);
if (dr->dt_timer.target < INT64_MAX) {
uint64_t now = _dispatch_time_now(DISPATCH_TIMER_CLOCK(dr->du_ident));
if (now >= dr->dt_timer.target) {
data = _dispatch_timer_unote_compute_missed(dr, now, data);
}
}
return data;
}
static void
_dispatch_source_latch_and_call(dispatch_source_t ds, dispatch_queue_t cq,
dispatch_invoke_flags_t flags)
{
dispatch_source_refs_t dr = ds->ds_refs;
dispatch_continuation_t dc = _dispatch_source_get_handler(dr, DS_EVENT_HANDLER);
uint64_t prev = os_atomic_xchg2o(dr, ds_pending_data, 0, relaxed);
if (dr->du_is_timer && (dr->du_timer_flags & DISPATCH_TIMER_AFTER)) {
_dispatch_trace_item_pop(cq, dc); // see _dispatch_after
}
switch (dux_type(dr)->dst_action) {
case DISPATCH_UNOTE_ACTION_SOURCE_TIMER:
if (prev & DISPATCH_TIMER_DISARMED_MARKER) {
dr->ds_data = _dispatch_source_timer_data(ds->ds_timer_refs, prev);
} else {
dr->ds_data = prev >> 1;
}
break;
case DISPATCH_UNOTE_ACTION_SOURCE_SET_DATA:
dr->ds_data = ~prev;
break;
default:
if (prev == 0 && dr->du_filter == DISPATCH_EVFILT_CUSTOM_REPLACE) {
return;
}
dr->ds_data = prev;
break;
}
if (unlikely(!dc)) {
return _dispatch_ktrace1(DISPATCH_PERF_handlerless_source_fire, ds);
}
if (!dispatch_assume(prev != 0)) {
return;
}
_dispatch_trace_source_callout_entry(ds, DS_EVENT_HANDLER, cq, dc);
#ifdef DBG_BSD_MEMSTAT
if (unlikely(dr->du_filter == EVFILT_MEMORYSTATUS)) {
_dispatch_ktrace2(KDBG_CODE(DBG_BSD, DBG_BSD_MEMSTAT, 0x100) | DBG_FUNC_START,
prev, _dispatch_continuation_get_function_symbol(dc));
}
#endif
_dispatch_continuation_pop(dc, NULL, flags, cq);
#ifdef DBG_BSD_MEMSTAT
if (unlikely(dr->du_filter == EVFILT_MEMORYSTATUS)) {
_dispatch_ktrace0(KDBG_CODE(DBG_BSD, DBG_BSD_MEMSTAT, 0x100) | DBG_FUNC_END);
}
#endif
if (dr->du_is_timer) {
if ((prev & DISPATCH_TIMER_DISARMED_MARKER) &&
_dispatch_source_refs_needs_configuration(dr)) {
_dispatch_timer_unote_configure(ds->ds_timer_refs);
}
if (dr->du_timer_flags & DISPATCH_TIMER_AFTER) {
_dispatch_trace_item_complete(dc); // see _dispatch_after
_dispatch_source_handler_free(dr, DS_EVENT_HANDLER);
dispatch_release(ds); // dispatch_after sources are one-shot
}
}
}
DISPATCH_NOINLINE
static void
_dispatch_source_refs_finalize_unregistration(dispatch_source_t ds)
{
dispatch_queue_flags_t dqf;
dqf = _dispatch_queue_atomic_flags_set_and_clear_orig(ds,
DSF_DELETED, DSF_NEEDS_EVENT | DSF_CANCEL_WAITER);
if (dqf & DSF_DELETED) {
DISPATCH_INTERNAL_CRASH(dqf, "Source finalized twice");
}
if (dqf & DSF_CANCEL_WAITER) {
_dispatch_wake_by_address(&ds->dq_atomic_flags);
}
_dispatch_object_debug(ds, "%s", __func__);
return _dispatch_release_tailcall(ds); // see _dispatch_queue_alloc()
}
static void
_dispatch_source_refs_unregister(dispatch_source_t ds, uint32_t options)
{
_dispatch_object_debug(ds, "%s", __func__);
dispatch_source_refs_t dr = ds->ds_refs;
if (_dispatch_unote_unregister(dr, options)) {
return _dispatch_source_refs_finalize_unregistration(ds);
}
// deferred unregistration
dispatch_queue_flags_t oqf, nqf;
os_atomic_rmw_loop2o(ds, dq_atomic_flags, oqf, nqf, relaxed, {
if (oqf & (DSF_NEEDS_EVENT | DSF_DELETED)) {
os_atomic_rmw_loop_give_up(break);
}
nqf = oqf | DSF_NEEDS_EVENT;
});
}
static void
_dispatch_source_install(dispatch_source_t ds, dispatch_wlh_t wlh,
dispatch_priority_t pri)
{
dispatch_source_refs_t dr = ds->ds_refs;
dispatch_assert(!ds->ds_is_installed);
ds->ds_is_installed = true;
_dispatch_object_debug(ds, "%s", __func__);
if (unlikely(!_dispatch_unote_register(dr, wlh, pri))) {
return _dispatch_source_refs_finalize_unregistration(ds);
}
}
void
_dispatch_source_activate(dispatch_source_t ds, bool *allow_resume)
{
dispatch_continuation_t dc;
dispatch_source_refs_t dr = ds->ds_refs;
dispatch_priority_t pri;
dispatch_wlh_t wlh;
if (unlikely(_dispatch_queue_atomic_flags(ds) & DSF_CANCELED)) {
ds->ds_is_installed = true;
return _dispatch_source_refs_finalize_unregistration(ds);
}
dc = _dispatch_source_get_event_handler(dr);
if (dc) {
if (_dispatch_object_is_barrier(dc)) {
_dispatch_queue_atomic_flags_set(ds, DQF_BARRIER_BIT);
}
if ((dc->dc_priority & _PTHREAD_PRIORITY_ENFORCE_FLAG) ||
!_dispatch_queue_priority_manually_selected(ds->dq_priority)) {
ds->dq_priority = _dispatch_priority_from_pp_strip_flags(dc->dc_priority);
}
if (dc->dc_flags & DC_FLAG_FETCH_CONTEXT) {
dc->dc_ctxt = ds->do_ctxt;
}
} else {
_dispatch_bug_deprecated("dispatch source activated "
"with no event handler set");
}
// call "super"
_dispatch_lane_activate(ds, allow_resume);
if ((dr->du_is_direct || dr->du_is_timer) && !ds->ds_is_installed) {
pri = _dispatch_queue_compute_priority_and_wlh(ds, &wlh);
if (pri) {
#if DISPATCH_USE_KEVENT_WORKLOOP
dispatch_workloop_t dwl = _dispatch_wlh_to_workloop(wlh);
if (dwl && dr->du_filter == DISPATCH_EVFILT_TIMER_WITH_CLOCK &&
dr->du_ident < DISPATCH_TIMER_WLH_COUNT) {
if (!dwl->dwl_timer_heap) {
uint32_t count = DISPATCH_TIMER_WLH_COUNT;
dwl->dwl_timer_heap = _dispatch_calloc(count,
sizeof(struct dispatch_timer_heap_s));
}
dr->du_is_direct = true;
_dispatch_wlh_retain(wlh);
_dispatch_unote_state_set(dr, wlh, 0);
}
#endif
_dispatch_source_install(ds, wlh, pri);
}
}
}
DISPATCH_NOINLINE
static void
_dispatch_source_handle_wlh_change(dispatch_source_t ds)
{
dispatch_queue_flags_t dqf;
dqf = _dispatch_queue_atomic_flags_set_orig(ds, DSF_WLH_CHANGED);
if (!(dqf & DQF_MUTABLE)) {
DISPATCH_CLIENT_CRASH(0, "Changing target queue "
"hierarchy after source was activated");
}
if (!(dqf & DSF_WLH_CHANGED)) {
_dispatch_bug_deprecated("Changing target queue "
"hierarchy after source was activated");
}
}
DISPATCH_ALWAYS_INLINE
static inline dispatch_queue_wakeup_target_t
_dispatch_source_invoke2(dispatch_source_t ds, dispatch_invoke_context_t dic,
dispatch_invoke_flags_t flags, uint64_t *owned)
{
dispatch_queue_wakeup_target_t retq = DISPATCH_QUEUE_WAKEUP_NONE;
dispatch_queue_t dq = _dispatch_queue_get_current();
dispatch_source_refs_t dr = ds->ds_refs;
dispatch_queue_flags_t dqf;
if (unlikely(!(flags & DISPATCH_INVOKE_MANAGER_DRAIN) &&
_dispatch_unote_wlh_changed(dr, _dispatch_get_event_wlh()))) {
_dispatch_source_handle_wlh_change(ds);
}
if (_dispatch_queue_class_probe(ds)) {
// Intentionally always drain even when on the manager queue
// and not the source's regular target queue: we need to be able
// to drain timer setting and the like there.
dispatch_with_disabled_narrowing(dic, {
retq = _dispatch_lane_serial_drain(ds, dic, flags, owned);
});
}
// This function performs all source actions. Each action is responsible
// for verifying that it takes place on the appropriate queue. If the
// current queue is not the correct queue for this action, the correct queue
// will be returned and the invoke will be re-driven on that queue.
// The order of tests here in invoke and in wakeup should be consistent.
dispatch_queue_t dkq = _dispatch_mgr_q._as_dq;
bool avoid_starvation = false;
if (dr->du_is_direct) {
dkq = ds->do_targetq;
}
if (!ds->ds_is_installed) {
// The source needs to be installed on the kevent queue.
if (dq != dkq) {
return dkq;
}
dispatch_priority_t pri = DISPATCH_PRIORITY_FLAG_MANAGER;
if (likely(flags & DISPATCH_INVOKE_WORKER_DRAIN)) {
pri = _dispatch_get_basepri();
}
_dispatch_source_install(ds, _dispatch_get_event_wlh(), pri);
}
if (unlikely(DISPATCH_QUEUE_IS_SUSPENDED(ds))) {
// Source suspended by an item drained from the source queue.
return ds->do_targetq;
}
if (_dispatch_source_refs_needs_configuration(dr)) {
dqf = _dispatch_queue_atomic_flags(ds);
if (!(dqf & (DSF_CANCELED | DQF_RELEASED))) {
if (dq != dkq) {
return dkq;
}
_dispatch_timer_unote_configure(ds->ds_timer_refs);
}
}
if (_dispatch_source_get_registration_handler(dr)) {
// The source has been registered and the registration handler needs
// to be delivered on the target queue.
if (dq != ds->do_targetq) {
return ds->do_targetq;
}
// clears ds_registration_handler
_dispatch_source_registration_callout(ds, dq, flags);
}
if (_dispatch_unote_needs_delete(dr)) {
_dispatch_source_refs_unregister(ds, DUU_DELETE_ACK | DUU_MUST_SUCCEED);
}
dqf = _dispatch_queue_atomic_flags(ds);
if (!(dqf & (DSF_CANCELED | DQF_RELEASED)) &&
os_atomic_load2o(dr, ds_pending_data, relaxed)) {
// The source has pending data to deliver via the event handler callback
// on the target queue. Some sources need to be rearmed on the kevent
// queue after event delivery.
if (dq == ds->do_targetq) {
_dispatch_source_latch_and_call(ds, dq, flags);
dqf = _dispatch_queue_atomic_flags(ds);
// starvation avoidance: if the source triggers itself then force a
// re-queue to give other things already queued on the target queue
// a chance to run.
//
// however, if the source is directly targeting an overcommit root
// queue, this would requeue the source and ask for a new overcommit
// thread right away.
if (!(dqf & (DSF_CANCELED | DSF_DELETED))) {
avoid_starvation = dq->do_targetq ||
!(dq->dq_priority & DISPATCH_PRIORITY_FLAG_OVERCOMMIT);
}
if (avoid_starvation &&
os_atomic_load2o(dr, ds_pending_data, relaxed)) {
retq = ds->do_targetq;
}
} else {
// there is no point trying to be eager, the next thing to do is
// to deliver the event
return ds->do_targetq;
}
}
if ((dqf & (DSF_CANCELED | DQF_RELEASED)) && !(dqf & DSF_DELETED)) {
// The source has been cancelled and needs to be uninstalled from the
// kevent queue. After uninstallation, the cancellation handler needs
// to be delivered to the target queue.
if (dr->du_is_timer && !_dispatch_unote_armed(dr)) {
// timers can cheat if not armed because there's nothing left
// to do on the manager queue and unregistration can happen
// on the regular target queue
} else if (dq != dkq) {
return dkq;
}
uint32_t duu_options = DUU_DELETE_ACK;
if (!(dqf & DSF_NEEDS_EVENT)) duu_options |= DUU_PROBE;
_dispatch_source_refs_unregister(ds, duu_options);
dqf = _dispatch_queue_atomic_flags(ds);
if (unlikely(!(dqf & DSF_DELETED))) {
// we need to wait for the EV_DELETE
return retq ? retq : DISPATCH_QUEUE_WAKEUP_WAIT_FOR_EVENT;
}
}
if ((dqf & (DSF_CANCELED | DQF_RELEASED)) && (dqf & DSF_DELETED)) {
if (dq != ds->do_targetq && (_dispatch_source_get_event_handler(dr) ||
_dispatch_source_get_cancel_handler(dr) ||
_dispatch_source_get_registration_handler(dr))) {
retq = ds->do_targetq;
} else {
_dispatch_source_cancel_callout(ds, dq, flags);
dqf = _dispatch_queue_atomic_flags(ds);
}
avoid_starvation = false;
}
if (!(dqf & (DSF_CANCELED | DQF_RELEASED)) &&
_dispatch_source_refs_needs_rearm(dr)) {
// The source needs to be rearmed on the kevent queue.
if (dq != dkq) {
return dkq;
}
if (unlikely(DISPATCH_QUEUE_IS_SUSPENDED(ds))) {
// do not try to rearm the kevent if the source is suspended
// from the source handler
return ds->do_targetq;
}
if (avoid_starvation && _dispatch_unote_wlh(dr) == DISPATCH_WLH_ANON) {
// keep the old behavior to force re-enqueue to our target queue
// for the rearm.
//
// if the handler didn't run, or this is a pending delete
// or our target queue is a global queue, then starvation is
// not a concern and we can rearm right away.
return ds->do_targetq;
}
_dispatch_unote_resume(dr);
if (!avoid_starvation && _dispatch_wlh_should_poll_unote(dr)) {
// try to redrive the drain from under the lock for sources
// targeting an overcommit root queue to avoid parking
// when the next event has already fired
_dispatch_event_loop_drain(KEVENT_FLAG_IMMEDIATE);
}
}
return retq;
}
DISPATCH_NOINLINE
void
_dispatch_source_invoke(dispatch_source_t ds, dispatch_invoke_context_t dic,
dispatch_invoke_flags_t flags)
{
_dispatch_queue_class_invoke(ds, dic, flags,
DISPATCH_INVOKE_DISALLOW_SYNC_WAITERS, _dispatch_source_invoke2);
#if DISPATCH_EVENT_BACKEND_KEVENT
if (flags & DISPATCH_INVOKE_WORKLOOP_DRAIN) {
dispatch_workloop_t dwl = (dispatch_workloop_t)_dispatch_get_wlh();
dispatch_timer_heap_t dth = dwl->dwl_timer_heap;
if (dth && dth[0].dth_dirty_bits) {
_dispatch_event_loop_drain_timers(dwl->dwl_timer_heap,
DISPATCH_TIMER_WLH_COUNT);
}
}
#endif // DISPATCH_EVENT_BACKEND_KEVENT
}
void
_dispatch_source_wakeup(dispatch_source_t ds, dispatch_qos_t qos,
dispatch_wakeup_flags_t flags)
{
// This function determines whether the source needs to be invoked.
// The order of tests here in wakeup and in invoke should be consistent.
dispatch_source_refs_t dr = ds->ds_refs;
dispatch_queue_wakeup_target_t dkq = DISPATCH_QUEUE_WAKEUP_MGR;
dispatch_queue_wakeup_target_t tq = DISPATCH_QUEUE_WAKEUP_NONE;
dispatch_queue_flags_t dqf = _dispatch_queue_atomic_flags(ds);
dispatch_unote_state_t du_state = _dispatch_unote_state(dr);
if (dr->du_is_direct) {
dkq = DISPATCH_QUEUE_WAKEUP_TARGET;
}
if (!ds->ds_is_installed) {
// The source needs to be installed on the kevent queue.
tq = dkq;
} else if (!(dqf & (DSF_CANCELED | DQF_RELEASED)) &&
_dispatch_source_refs_needs_configuration(dr)) {
// timer has to be configured on the kevent queue
tq = dkq;
} else if (_dispatch_source_get_registration_handler(dr)) {
// The registration handler needs to be delivered to the target queue.
tq = DISPATCH_QUEUE_WAKEUP_TARGET;
} else if (_du_state_needs_delete(du_state)) {
// Deferred deletion can be acknowledged which can always be done
// from the target queue
tq = DISPATCH_QUEUE_WAKEUP_TARGET;
} else if (!(dqf & (DSF_CANCELED | DQF_RELEASED)) &&
os_atomic_load2o(dr, ds_pending_data, relaxed)) {
// The source has pending data to deliver to the target queue.
tq = DISPATCH_QUEUE_WAKEUP_TARGET;
} else if ((dqf & (DSF_CANCELED | DQF_RELEASED)) && !(dqf & DSF_DELETED)) {
// The source needs to be uninstalled from the kevent queue, or the
// cancellation handler needs to be delivered to the target queue.
// Note: cancellation assumes installation.
if (dr->du_is_timer && !_dispatch_unote_armed(dr)) {
// timers can cheat if not armed because there's nothing left
// to do on the manager queue and unregistration can happen
// on the regular target queue
tq = DISPATCH_QUEUE_WAKEUP_TARGET;
} else if ((dqf & DSF_NEEDS_EVENT) && !(flags & DISPATCH_WAKEUP_EVENT)){
// we're waiting for an event
} else {
// we need to initialize the deletion sequence
tq = dkq;
}
} else if ((dqf & (DSF_CANCELED | DQF_RELEASED)) && (dqf & DSF_DELETED) &&
(_dispatch_source_get_event_handler(dr) ||
_dispatch_source_get_cancel_handler(dr) ||
_dispatch_source_get_registration_handler(dr))) {
tq = DISPATCH_QUEUE_WAKEUP_TARGET;
} else if (!(dqf & (DSF_CANCELED | DQF_RELEASED)) &&
_dispatch_source_refs_needs_rearm(dr)) {
// The source needs to be rearmed on the kevent queue.
tq = dkq;
}
if (!tq && _dispatch_queue_class_probe(ds)) {
tq = DISPATCH_QUEUE_WAKEUP_TARGET;
}
if ((tq == DISPATCH_QUEUE_WAKEUP_TARGET) &&
ds->do_targetq == _dispatch_mgr_q._as_dq) {
tq = DISPATCH_QUEUE_WAKEUP_MGR;
}
return _dispatch_queue_wakeup(ds, qos, flags, tq);
}
void
dispatch_source_cancel(dispatch_source_t ds)
{
_dispatch_object_debug(ds, "%s", __func__);
// Right after we set the cancel flag, someone else
// could potentially invoke the source, do the cancellation,
// unregister the source, and deallocate it. We would
// need to therefore retain/release before setting the bit
_dispatch_retain_2(ds);
if (_dispatch_queue_atomic_flags_set_orig(ds, DSF_CANCELED) & DSF_CANCELED){
_dispatch_release_2_tailcall(ds);
} else {
dx_wakeup(ds, 0, DISPATCH_WAKEUP_MAKE_DIRTY | DISPATCH_WAKEUP_CONSUME_2);
}
}
void
dispatch_source_cancel_and_wait(dispatch_source_t ds)
{