This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
forked from nevali/opencflite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFRunLoop.c
3215 lines (2951 loc) · 116 KB
/
CFRunLoop.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 Brent Fulgham <[email protected]>. All rights reserved.
* Copyright (c) 2009 Grant Erickson <[email protected]>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2008 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFRunLoop.c
Copyright 1998-2002, Apple, Inc. All rights reserved.
Responsibility: Christopher Kane
*/
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
#include <CoreFoundation/CFRunLoop.h>
#include <CoreFoundation/CFSet.h>
#include <CoreFoundation/CFBag.h>
#include "CFInternal.h"
#include <math.h>
#include <stdio.h>
#include <limits.h>
#if DEPLOYMENT_TARGET_MACOSX
#include <mach/mach.h>
#include <mach/clock_types.h>
#include <mach/clock.h>
#include <unistd.h>
#include <dlfcn.h>
#elif DEPLOYMENT_TARGET_WINDOWS
#if !defined(__MINGW32__) && !defined(__CYGWIN__)
// With the MS headers, turning off Standard-C gets you macros for stat vs _stat.
// Strictly speaking, this is supposed to control traditional vs ANSI C features.
#undef __STDC__
#endif
#include <windows.h>
#include <process.h>
#if !defined(__MINGW32__) && !defined(__CYGWIN__)
#define __STDC__
#endif
#elif DEPLOYMENT_TARGET_LINUX
#include <dlfcn.h>
#include <pthread.h>
#include <semaphore.h>
#endif
static int _LogCFRunLoop = 0;
#if 0 || 0
static pthread_t kNilThreadT = { nil, nil };
#define pthreadPointer(a) a.p
#define lockCount(a) a.LockCount
#define NativeThread pthread_t
#elif DEPLOYMENT_TARGET_WINDOWS
DWORD GetInternalThreadId(HANDLE t);
static HANDLE kNilThreadT = 0;
static DWORD __kCFMainThread = 0;
#define pthreadPointer(a) ((void*)GetInternalThreadId(a))
// Note: Windows RTL_CRITICAL_SECTION LockCount is initialized to -1
// indicating no lock. So add one so lock count shows a valid 'count'.
#define lockCount(a) (a.LockCount + 1)
#define NativeThread HANDLE
#elif DEPLOYMENT_TARGET_LINUX
static pthread_t kNilThreadT = (pthread_t)0;
static pthread_t __kCFMainThread = (pthread_t)0;
#define pthreadPointer(a) (void *)a
#define lockCount(a) a.lock
#define NativeThread pthread_t
#else
static pthread_t kNilThreadT = (pthread_t)0;
#define pthreadPointer(a) a
#define lockCount(a) a
#define NativeThread pthread_t
#endif
#if DEPLOYMENT_TARGET_MACOSX
#include <sys/types.h>
#include <sys/event.h>
typedef struct {
CFIndex version;
void * info;
const void *(*retain)(const void *info);
void (*release)(const void *info);
CFStringRef (*copyDescription)(const void *info);
Boolean (*equal)(const void *info1, const void *info2);
CFHashCode (*hash)(const void *info);
void (*perform)(const struct kevent *kev, void *info);
struct kevent event;
} CFRunLoopSourceContext2;
// The bits in the flags field in the kevent structure are cleared except for EV_ONESHOT and EV_CLEAR.
// Do not use the udata field of the kevent structure -- that field is smashed by CFRunLoop.
// There is no way to EV_ENABLE or EV_DISABLE a kevent.
// The "autoinvalidation" of EV_ONESHOT is not handled properly by CFRunLoop yet.
// The "autoinvalidation" of EV_DELETE on the last close of a file descriptor is not handled properly by CFRunLoop yet.
// There is no way to reset the state in a kevent (such as clearing the EV_EOF state for fifos).
#endif
extern bool CFDictionaryGetKeyIfPresent(CFDictionaryRef dict, const void *key, const void **actualkey);
// In order to reuse most of the code across Mach and Windows v1 RunLoopSources, we define a
// simple abstraction layer spanning Mach ports and Windows HANDLES
#if DEPLOYMENT_TARGET_MACOSX
typedef mach_port_t __CFPort;
#define CFPORT_NULL MACH_PORT_NULL
#define CFPORTSET_NULL MACH_PORT_NULL
typedef mach_port_t __CFPortSet;
static __CFPort __CFPortAllocate(void) {
__CFPort result;
kern_return_t ret;
ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &result);
if (KERN_SUCCESS == ret) {
ret = mach_port_insert_right(mach_task_self(), result, result, MACH_MSG_TYPE_MAKE_SEND);
}
if (KERN_SUCCESS == ret) {
mach_port_limits_t limits;
limits.mpl_qlimit = 1;
ret = mach_port_set_attributes(mach_task_self(), result, MACH_PORT_LIMITS_INFO, (mach_port_info_t)&limits, MACH_PORT_LIMITS_INFO_COUNT);
}
return (KERN_SUCCESS == ret) ? result : CFPORT_NULL;
}
CF_INLINE void __CFPortFree(__CFPort port) {
mach_port_destroy(mach_task_self(), port);
}
CF_INLINE __CFPortSet __CFPortSetAllocate(void) {
__CFPortSet result;
kern_return_t ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &result);
return (KERN_SUCCESS == ret) ? result : CFPORT_NULL;
}
CF_INLINE Boolean __CFPortSetInsert(__CFPort port, __CFPortSet portSet) {
kern_return_t ret = mach_port_insert_member(mach_task_self(), port, portSet);
return (KERN_SUCCESS == ret);
}
CF_INLINE Boolean __CFPortSetRemove(__CFPort port, __CFPortSet portSet) {
kern_return_t ret = mach_port_extract_member(mach_task_self(), port, portSet);
return (KERN_SUCCESS == ret);
}
CF_INLINE void __CFPortSetFree(__CFPortSet portSet) {
kern_return_t ret;
mach_port_name_array_t array;
mach_msg_type_number_t idx, number;
ret = mach_port_get_set_status(mach_task_self(), portSet, &array, &number);
if (KERN_SUCCESS == ret) {
for (idx = 0; idx < number; idx++) {
mach_port_extract_member(mach_task_self(), array[idx], portSet);
}
vm_deallocate(mach_task_self(), (vm_address_t)array, number * sizeof(mach_port_name_t));
}
mach_port_destroy(mach_task_self(), portSet);
}
#elif DEPLOYMENT_TARGET_WINDOWS
typedef HANDLE __CFPort;
#define CFPORT_NULL NULL
#define CFPORTSET_NULL NULL
#define MAX_PORTS MAXIMUM_WAIT_OBJECTS
CF_INLINE __CFPort __CFPortAllocate(void) {
return CreateEvent(NULL, true, false, NULL);
}
CF_INLINE void __CFPortFree(__CFPort port) {
CloseHandle(port);
}
#elif DEPLOYMENT_TARGET_LINUX
typedef sem_t * __CFPort;
#define CFPORT_NULL NULL
#define CFPORTSET_NULL NULL
#define MAX_PORTS 16
CF_INLINE __CFPort __CFPortAllocate(void) {
__CFPort port = CFPORT_NULL;
int status;
port = (__CFPort)CFAllocatorAllocate(kCFAllocatorSystemDefault,
sizeof(sem_t), 0);
CFAssert1(port != CFPORT_NULL, __kCFLogAssertion,
"%s(): Could not allocate port memory.", __PRETTY_FUNCTION__);
if (port != CFPORT_NULL) {
status = sem_init(port, false, 0);
CFAssert1(status == 0, __kCFLogAssertion,
"%s(): Could not initialize port.", __PRETTY_FUNCTION__);
}
return port;
}
CF_INLINE void __CFPortFree(__CFPort port) {
int status;
CFAssert1(port != CFPORT_NULL, __kCFLogAssertion,
"%s(): Attemping to free an invalid port.", __PRETTY_FUNCTION__);
status = sem_destroy(port);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, port);
}
#endif /* DEPLOYMENT_TARGET_MACOSX */
#if !DEPLOYMENT_TARGET_MACOSX
// A simple dynamic array of __CFPorts, which grows to a high-water mark
typedef struct ___CFPortSet {
uint16_t used;
uint16_t size;
__CFPort *ports;
CFSpinLock_t lock; // insert and remove must be thread safe, like the Mach calls
} *__CFPortSet;
static __CFPortSet __CFPortSetAllocate(void) {
__CFPortSet result = (__CFPortSet)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(struct ___CFPortSet), 0);
result->used = 0;
result->size = 4;
result->ports = (__CFPort *)CFAllocatorAllocate(kCFAllocatorSystemDefault, result->size * sizeof(__CFPort), 0);
CF_SPINLOCK_INIT_FOR_STRUCTS(result->lock);
return result;
}
static void __CFPortSetFree(__CFPortSet portSet) {
CFAllocatorDeallocate(kCFAllocatorSystemDefault, portSet->ports);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, portSet);
}
// Returns portBuf if ports fit in that space, else returns another ptr that must be freed
static __CFPort *__CFPortSetGetPorts(__CFPortSet portSet, __CFPort *portBuf, uint32_t bufSize, uint32_t *portsUsed) {
__CFSpinLock(&(portSet->lock));
__CFPort *result = portBuf;
if (bufSize > portSet->used)
result = (__CFPort *)CFAllocatorAllocate(kCFAllocatorSystemDefault, portSet->used * sizeof(__CFPort), 0);
memmove(result, portSet->ports, portSet->used * sizeof(__CFPort));
*portsUsed = portSet->used;
__CFSpinUnlock(&(portSet->lock));
return result;
}
static Boolean __CFPortSetInsert(__CFPort port, __CFPortSet portSet) {
__CFSpinLock(&(portSet->lock));
if (portSet->used >= portSet->size) {
portSet->size += 4;
portSet->ports = (__CFPort*)CFAllocatorReallocate(kCFAllocatorSystemDefault, portSet->ports, portSet->size * sizeof(__CFPort), 0);
}
if (portSet->used >= MAX_PORTS)
CFLog(kCFLogLevelWarning, CFSTR("*** More than %d ports added to a port set. The last ones will be ignored."), MAX_PORTS);
portSet->ports[portSet->used++] = port;
__CFSpinUnlock(&(portSet->lock));
return true;
}
static Boolean __CFPortSetRemove(__CFPort port, __CFPortSet portSet) {
int i, j;
__CFSpinLock(&(portSet->lock));
for (i = 0; i < portSet->used; i++) {
if (portSet->ports[i] == port) {
for (j = i+1; j < portSet->used; j++) {
portSet->ports[j-1] = portSet->ports[j];
}
portSet->used--;
__CFSpinUnlock(&(portSet->lock));
return true;
}
}
__CFSpinUnlock(&(portSet->lock));
return false;
}
#endif
#if DEPLOYMENT_TARGET_MACOSX
extern mach_port_name_t mk_timer_create(void);
extern kern_return_t mk_timer_destroy(mach_port_name_t name);
extern kern_return_t mk_timer_arm(mach_port_name_t name, AbsoluteTime expire_time);
extern kern_return_t mk_timer_cancel(mach_port_name_t name, AbsoluteTime *result_time);
CF_INLINE AbsoluteTime __CFUInt64ToAbsoluteTime(int64_t x) {
AbsoluteTime a;
a.hi = x >> 32;
a.lo = x & (int64_t)0xFFFFFFFF;
return a;
}
static uint32_t __CFSendTrivialMachMessage(mach_port_t port, uint32_t msg_id, CFOptionFlags options, uint32_t timeout) {
kern_return_t result;
mach_msg_header_t header;
header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
header.msgh_size = sizeof(mach_msg_header_t);
header.msgh_remote_port = port;
header.msgh_local_port = MACH_PORT_NULL;
header.msgh_id = msg_id;
result = mach_msg(&header, MACH_SEND_MSG|options, header.msgh_size, 0, MACH_PORT_NULL, timeout, MACH_PORT_NULL);
if (result == MACH_SEND_TIMED_OUT) mach_msg_destroy(&header);
return result;
}
#endif
/* unlock a run loop and modes before doing callouts/sleeping */
/* never try to take the run loop lock with a mode locked */
/* be very careful of common subexpression elimination and compacting code, particular across locks and unlocks! */
/* run loop mode structures should never be deallocated, even if they become empty */
static CFTypeID __kCFRunLoopModeTypeID = _kCFRuntimeNotATypeID;
static CFTypeID __kCFRunLoopTypeID = _kCFRuntimeNotATypeID;
static CFTypeID __kCFRunLoopSourceTypeID = _kCFRuntimeNotATypeID;
static CFTypeID __kCFRunLoopObserverTypeID = _kCFRuntimeNotATypeID;
static CFTypeID __kCFRunLoopTimerTypeID = _kCFRuntimeNotATypeID;
typedef struct __CFRunLoopMode *CFRunLoopModeRef;
struct __CFRunLoopMode {
CFRuntimeBase _base;
CFSpinLock_t _lock; /* must have the run loop locked before locking this */
CFStringRef _name;
Boolean _stopped;
char _padding[3];
CFMutableSetRef _sources;
CFMutableSetRef _observers;
CFMutableSetRef _timers;
CFMutableArrayRef _submodes; // names of the submodes
__CFPortSet _portSet;
#if DEPLOYMENT_TARGET_MACOSX
int _kq;
#elif DEPLOYMENT_TARGET_WINDOWS
DWORD _msgQMask;
#endif
};
static int64_t __CFRunLoopGetNextTimerFireTSR(CFRunLoopRef rl, CFRunLoopModeRef rlm);
CF_INLINE void __CFRunLoopModeLock(CFRunLoopModeRef rlm) {
__CFSpinLock(&(rlm->_lock));
}
CF_INLINE void __CFRunLoopModeUnlock(CFRunLoopModeRef rlm) {
__CFSpinUnlock(&(rlm->_lock));
}
static Boolean __CFRunLoopModeEqual(CFTypeRef cf1, CFTypeRef cf2) {
CFRunLoopModeRef rlm1 = (CFRunLoopModeRef)cf1;
CFRunLoopModeRef rlm2 = (CFRunLoopModeRef)cf2;
return CFEqual(rlm1->_name, rlm2->_name);
}
static CFHashCode __CFRunLoopModeHash(CFTypeRef cf) {
CFRunLoopModeRef rlm = (CFRunLoopModeRef)cf;
return CFHash(rlm->_name);
}
static CFStringRef __CFRunLoopModeCopyDescription(CFTypeRef cf) {
CFRunLoopModeRef rlm = (CFRunLoopModeRef)cf;
CFMutableStringRef result;
result = CFStringCreateMutable(kCFAllocatorSystemDefault, 0);
CFStringAppendFormat(result, NULL, CFSTR("<CFRunLoopMode %p [%p]>{name = %@, locked = %s, "), rlm, CFGetAllocator(rlm), rlm->_name, lockCount(rlm->_lock) ? "true" : "false");
#if DEPLOYMENT_TARGET_MACOSX
CFStringAppendFormat(result, NULL, CFSTR("port set = %p,"), rlm->_portSet);
#elif DEPLOYMENT_TARGET_WINDOWS
CFStringAppendFormat(result, NULL, CFSTR("MSGQ mask = %p,"), rlm->_msgQMask);
#endif
CFStringAppendFormat(result, NULL, CFSTR("\n\tsources = %@,\n\tobservers == %@,\n\ttimers = %@\n},\n"), rlm->_sources, rlm->_observers, rlm->_timers);
return result;
}
static void __CFRunLoopModeDeallocate(CFTypeRef cf) {
CFRunLoopModeRef rlm = (CFRunLoopModeRef)cf;
if (NULL != rlm->_sources) CFRelease(rlm->_sources);
if (NULL != rlm->_observers) CFRelease(rlm->_observers);
if (NULL != rlm->_timers) CFRelease(rlm->_timers);
if (NULL != rlm->_submodes) CFRelease(rlm->_submodes);
CFRelease(rlm->_name);
__CFPortSetFree(rlm->_portSet);
#if DEPLOYMENT_TARGET_MACOSX
if (-1 != rlm->_kq) close(rlm->_kq);
#endif
}
struct __CFRunLoop {
CFRuntimeBase _base;
CFSpinLock_t _lock; /* locked for accessing mode list */
__CFPort _wakeUpPort; // used for CFRunLoopWakeUp
volatile uint32_t *_stopped;
CFMutableSetRef _commonModes;
CFMutableSetRef _commonModeItems;
CFRunLoopModeRef _currentMode;
CFMutableSetRef _modes;
void *_counterpart;
};
/* Bit 0 of the base reserved bits is used for stopped state */
/* Bit 1 of the base reserved bits is used for sleeping state */
/* Bit 2 of the base reserved bits is used for deallocating state */
CF_INLINE Boolean __CFRunLoopIsStopped(CFRunLoopRef rl) {
return (rl->_stopped && rl->_stopped[2]) ? true : false;
}
CF_INLINE void __CFRunLoopSetStopped(CFRunLoopRef rl) {
if (rl->_stopped) rl->_stopped[2] = 0x53544F50; // 'STOP'
}
CF_INLINE void __CFRunLoopUnsetStopped(CFRunLoopRef rl) {
if (rl->_stopped) rl->_stopped[2] = 0x0;
}
CF_INLINE Boolean __CFRunLoopIsSleeping(CFRunLoopRef rl) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)rl)->_cfinfo[CF_INFO_BITS], 1, 1);
}
CF_INLINE void __CFRunLoopSetSleeping(CFRunLoopRef rl) {
__CFBitfieldSetValue(((CFRuntimeBase *)rl)->_cfinfo[CF_INFO_BITS], 1, 1, 1);
}
CF_INLINE void __CFRunLoopUnsetSleeping(CFRunLoopRef rl) {
__CFBitfieldSetValue(((CFRuntimeBase *)rl)->_cfinfo[CF_INFO_BITS], 1, 1, 0);
}
CF_INLINE Boolean __CFRunLoopIsDeallocating(CFRunLoopRef rl) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)rl)->_cfinfo[CF_INFO_BITS], 2, 2);
}
CF_INLINE void __CFRunLoopSetDeallocating(CFRunLoopRef rl) {
__CFBitfieldSetValue(((CFRuntimeBase *)rl)->_cfinfo[CF_INFO_BITS], 2, 2, 1);
}
CF_INLINE void __CFRunLoopLock(CFRunLoopRef rl) {
__CFSpinLock(&(((CFRunLoopRef)rl)->_lock));
}
CF_INLINE void __CFRunLoopUnlock(CFRunLoopRef rl) {
__CFSpinUnlock(&(((CFRunLoopRef)rl)->_lock));
}
static CFStringRef __CFRunLoopCopyDescription(CFTypeRef cf) {
CFRunLoopRef rl = (CFRunLoopRef)cf;
CFMutableStringRef result;
result = CFStringCreateMutable(kCFAllocatorSystemDefault, 0);
CFStringAppendFormat(result, NULL, CFSTR("<CFRunLoop %p [%p]>{locked = %s, wait port = 0x%x, stopped = %s,\ncurrent mode = %@,\n"), cf, CFGetAllocator(cf), lockCount(rl->_lock) ? "true" : "false", rl->_wakeUpPort, (rl->_stopped && (rl->_stopped[2] == 0x53544F50)) ? "true" : "false", rl->_currentMode ? rl->_currentMode->_name : CFSTR("(none)"));
CFStringAppendFormat(result, NULL, CFSTR("common modes = %@,\ncommon mode items = %@,\nmodes = %@}\n"), rl->_commonModes, rl->_commonModeItems, rl->_modes);
return result;
}
/* call with rl locked */
static CFRunLoopModeRef __CFRunLoopFindMode(CFRunLoopRef rl, CFStringRef modeName, Boolean create) {
CHECK_FOR_FORK();
CFRunLoopModeRef rlm;
struct __CFRunLoopMode srlm;
srlm._base._cfisa = __CFISAForTypeID(__kCFRunLoopModeTypeID);
srlm._base._cfinfo[CF_INFO_BITS] = 0;
_CFRuntimeSetInstanceTypeID(&srlm, __kCFRunLoopModeTypeID);
srlm._name = modeName;
rlm = (CFRunLoopModeRef)CFSetGetValue(rl->_modes, &srlm);
if (NULL != rlm) {
__CFRunLoopModeLock(rlm);
return rlm;
}
if (!create) {
return NULL;
}
rlm = (CFRunLoopModeRef)_CFRuntimeCreateInstance(CFGetAllocator(rl), __kCFRunLoopModeTypeID, sizeof(struct __CFRunLoopMode) - sizeof(CFRuntimeBase), NULL);
if (NULL == rlm) {
return NULL;
}
CF_SPINLOCK_INIT_FOR_STRUCTS(rlm->_lock);
rlm->_name = CFStringCreateCopy(CFGetAllocator(rlm), modeName);
rlm->_stopped = false;
rlm->_sources = NULL;
rlm->_observers = NULL;
rlm->_timers = NULL;
rlm->_submodes = NULL;
rlm->_portSet = __CFPortSetAllocate();
if (CFPORTSET_NULL == rlm->_portSet) HALT;
if (!__CFPortSetInsert(rl->_wakeUpPort, rlm->_portSet)) HALT;
#if DEPLOYMENT_TARGET_MACOSX
rlm->_kq = -1;
#elif DELPOYMENT_TARGET_WIN32
rlm->_msgQMask = 0;
#endif
CFSetAddValue(rl->_modes, rlm);
CFRelease(rlm);
__CFRunLoopModeLock(rlm); /* return mode locked */
return rlm;
}
// expects rl and rlm locked
static Boolean __CFRunLoopModeIsEmpty(CFRunLoopRef rl, CFRunLoopModeRef rlm) {
CHECK_FOR_FORK();
if (NULL == rlm) return true;
#if DEPLOYMENT_TARGET_WINDOWS
if (0 != rlm->_msgQMask) return false;
#endif
if (NULL != rlm->_sources && 0 < CFSetGetCount(rlm->_sources)) return false;
if (NULL != rlm->_timers && 0 < CFSetGetCount(rlm->_timers)) return false;
if (NULL != rlm->_submodes) {
CFIndex idx, cnt;
for (idx = 0, cnt = CFArrayGetCount(rlm->_submodes); idx < cnt; idx++) {
CFStringRef modeName = (CFStringRef)CFArrayGetValueAtIndex(rlm->_submodes, idx);
CFRunLoopModeRef subrlm;
Boolean subIsEmpty;
subrlm = __CFRunLoopFindMode(rl, modeName, false);
subIsEmpty = (NULL != subrlm) ? __CFRunLoopModeIsEmpty(rl, subrlm) : true;
if (NULL != subrlm) __CFRunLoopModeUnlock(subrlm);
if (!subIsEmpty) return false;
}
}
return true;
}
#if DEPLOYMENT_TARGET_WINDOWS
DWORD __CFRunLoopGetWindowsMessageQueueMask(CFRunLoopRef rl, CFStringRef modeName) {
CFRunLoopModeRef rlm;
DWORD result = 0;
__CFRunLoopLock(rl);
rlm = __CFRunLoopFindMode(rl, modeName, false);
if (rlm) {
result = rlm->_msgQMask;
__CFRunLoopModeUnlock(rlm);
}
__CFRunLoopUnlock(rl);
return result;
}
void __CFRunLoopSetWindowsMessageQueueMask(CFRunLoopRef rl, DWORD mask, CFStringRef modeName) {
CFRunLoopModeRef rlm;
__CFRunLoopLock(rl);
rlm = __CFRunLoopFindMode(rl, modeName, true);
rlm->_msgQMask = mask;
__CFRunLoopModeUnlock(rlm);
__CFRunLoopUnlock(rl);
}
#endif
/* Bit 3 in the base reserved bits is used for invalid state in run loop objects */
CF_INLINE Boolean __CFIsValid(const void *cf) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 3, 3);
}
CF_INLINE void __CFSetValid(void *cf) {
__CFBitfieldSetValue(((CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 3, 3, 1);
}
CF_INLINE void __CFUnsetValid(void *cf) {
__CFBitfieldSetValue(((CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 3, 3, 0);
}
struct __CFRunLoopSource {
CFRuntimeBase _base;
uint32_t _bits;
CFSpinLock_t _lock;
CFIndex _order; /* immutable */
CFMutableBagRef _runLoops;
union {
CFRunLoopSourceContext version0; /* immutable, except invalidation */
CFRunLoopSourceContext1 version1; /* immutable, except invalidation */
} _context;
};
/* Bit 1 of the base reserved bits is used for signalled state */
CF_INLINE Boolean __CFRunLoopSourceIsSignaled(CFRunLoopSourceRef rls) {
return (Boolean)__CFBitfieldGetValue(rls->_bits, 1, 1);
}
CF_INLINE void __CFRunLoopSourceSetSignaled(CFRunLoopSourceRef rls) {
__CFBitfieldSetValue(rls->_bits, 1, 1, 1);
}
CF_INLINE void __CFRunLoopSourceUnsetSignaled(CFRunLoopSourceRef rls) {
__CFBitfieldSetValue(rls->_bits, 1, 1, 0);
}
CF_INLINE void __CFRunLoopSourceLock(CFRunLoopSourceRef rls) {
__CFSpinLock(&(rls->_lock));
}
CF_INLINE void __CFRunLoopSourceUnlock(CFRunLoopSourceRef rls) {
__CFSpinUnlock(&(rls->_lock));
}
/* rlm is not locked */
static void __CFRunLoopSourceSchedule(CFRunLoopSourceRef rls, CFRunLoopRef rl, CFRunLoopModeRef rlm) { /* DOES CALLOUT */
__CFRunLoopSourceLock(rls);
if (NULL == rls->_runLoops) {
rls->_runLoops = CFBagCreateMutable(CFGetAllocator(rls), 0, NULL);
}
CFBagAddValue(rls->_runLoops, rl);
__CFRunLoopSourceUnlock(rls); // have to unlock before the callout -- cannot help clients with safety
if (0 == rls->_context.version0.version) {
if (NULL != rls->_context.version0.schedule) {
rls->_context.version0.schedule(rls->_context.version0.info, rl, rlm->_name);
}
} else if (1 == rls->_context.version0.version) {
__CFPort port = rls->_context.version1.getPort(rls->_context.version1.info); /* CALLOUT */
if (CFPORT_NULL != port) {
__CFPortSetInsert(port, rlm->_portSet);
}
}
}
/* rlm is not locked */
static void __CFRunLoopSourceCancel(CFRunLoopSourceRef rls, CFRunLoopRef rl, CFRunLoopModeRef rlm) { /* DOES CALLOUT */
if (0 == rls->_context.version0.version) {
if (NULL != rls->_context.version0.cancel) {
rls->_context.version0.cancel(rls->_context.version0.info, rl, rlm->_name); /* CALLOUT */
}
} else if (1 == rls->_context.version0.version) {
__CFPort port = rls->_context.version1.getPort(rls->_context.version1.info); /* CALLOUT */
if (CFPORT_NULL != port) {
__CFPortSetRemove(port, rlm->_portSet);
}
}
__CFRunLoopSourceLock(rls);
if (NULL != rls->_runLoops) {
CFBagRemoveValue(rls->_runLoops, rl);
}
__CFRunLoopSourceUnlock(rls);
}
struct __CFRunLoopObserver {
CFRuntimeBase _base;
CFSpinLock_t _lock;
CFRunLoopRef _runLoop;
CFIndex _rlCount;
CFOptionFlags _activities; /* immutable */
CFIndex _order; /* immutable */
CFRunLoopObserverCallBack _callout; /* immutable */
CFRunLoopObserverContext _context; /* immutable, except invalidation */
};
/* Bit 0 of the base reserved bits is used for firing state */
/* Bit 1 of the base reserved bits is used for repeats state */
CF_INLINE Boolean __CFRunLoopObserverIsFiring(CFRunLoopObserverRef rlo) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)rlo)->_cfinfo[CF_INFO_BITS], 0, 0);
}
CF_INLINE void __CFRunLoopObserverSetFiring(CFRunLoopObserverRef rlo) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlo)->_cfinfo[CF_INFO_BITS], 0, 0, 1);
}
CF_INLINE void __CFRunLoopObserverUnsetFiring(CFRunLoopObserverRef rlo) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlo)->_cfinfo[CF_INFO_BITS], 0, 0, 0);
}
CF_INLINE Boolean __CFRunLoopObserverRepeats(CFRunLoopObserverRef rlo) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)rlo)->_cfinfo[CF_INFO_BITS], 1, 1);
}
CF_INLINE void __CFRunLoopObserverSetRepeats(CFRunLoopObserverRef rlo) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlo)->_cfinfo[CF_INFO_BITS], 1, 1, 1);
}
CF_INLINE void __CFRunLoopObserverUnsetRepeats(CFRunLoopObserverRef rlo) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlo)->_cfinfo[CF_INFO_BITS], 1, 1, 0);
}
CF_INLINE void __CFRunLoopObserverLock(CFRunLoopObserverRef rlo) {
__CFSpinLock(&(rlo->_lock));
}
CF_INLINE void __CFRunLoopObserverUnlock(CFRunLoopObserverRef rlo) {
__CFSpinUnlock(&(rlo->_lock));
}
static void __CFRunLoopObserverSchedule(CFRunLoopObserverRef rlo, CFRunLoopRef rl, CFRunLoopModeRef rlm) {
__CFRunLoopObserverLock(rlo);
if (0 == rlo->_rlCount) {
rlo->_runLoop = rl;
}
rlo->_rlCount++;
__CFRunLoopObserverUnlock(rlo);
}
static void __CFRunLoopObserverCancel(CFRunLoopObserverRef rlo, CFRunLoopRef rl, CFRunLoopModeRef rlm) {
__CFRunLoopObserverLock(rlo);
rlo->_rlCount--;
if (0 == rlo->_rlCount) {
rlo->_runLoop = NULL;
}
__CFRunLoopObserverUnlock(rlo);
}
struct __CFRunLoopTimer {
CFRuntimeBase _base;
CFSpinLock_t _lock;
CFRunLoopRef _runLoop;
CFIndex _rlCount;
#if DEPLOYMENT_TARGET_MACOSX
mach_port_name_t _port;
#endif
CFIndex _order; /* immutable */
int64_t _fireTSR; /* TSR units */
int64_t _intervalTSR; /* immutable; 0 means non-repeating; TSR units */
CFRunLoopTimerCallBack _callout; /* immutable */
CFRunLoopTimerContext _context; /* immutable, except invalidation */
};
/* Bit 0 of the base reserved bits is used for firing state */
/* Bit 1 of the base reserved bits is used for fired-during-callout state */
CF_INLINE Boolean __CFRunLoopTimerIsFiring(CFRunLoopTimerRef rlt) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)rlt)->_cfinfo[CF_INFO_BITS], 0, 0);
}
CF_INLINE void __CFRunLoopTimerSetFiring(CFRunLoopTimerRef rlt) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlt)->_cfinfo[CF_INFO_BITS], 0, 0, 1);
}
CF_INLINE void __CFRunLoopTimerUnsetFiring(CFRunLoopTimerRef rlt) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlt)->_cfinfo[CF_INFO_BITS], 0, 0, 0);
}
CF_INLINE Boolean __CFRunLoopTimerDidFire(CFRunLoopTimerRef rlt) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)rlt)->_cfinfo[CF_INFO_BITS], 1, 1);
}
CF_INLINE void __CFRunLoopTimerSetDidFire(CFRunLoopTimerRef rlt) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlt)->_cfinfo[CF_INFO_BITS], 1, 1, 1);
}
CF_INLINE void __CFRunLoopTimerUnsetDidFire(CFRunLoopTimerRef rlt) {
__CFBitfieldSetValue(((CFRuntimeBase *)rlt)->_cfinfo[CF_INFO_BITS], 1, 1, 0);
}
CF_INLINE void __CFRunLoopTimerLock(CFRunLoopTimerRef rlt) {
__CFSpinLock(&(rlt->_lock));
}
CF_INLINE void __CFRunLoopTimerUnlock(CFRunLoopTimerRef rlt) {
__CFSpinUnlock(&(rlt->_lock));
}
static CFSpinLock_t __CFRLTFireTSRLock = CFSpinLockInit;
CF_INLINE void __CFRunLoopTimerFireTSRLock(void) {
__CFSpinLock(&__CFRLTFireTSRLock);
}
CF_INLINE void __CFRunLoopTimerFireTSRUnlock(void) {
__CFSpinUnlock(&__CFRLTFireTSRLock);
}
#if DEPLOYMENT_TARGET_MACOSX
static CFMutableDictionaryRef __CFRLTPortMap = NULL;
static CFSpinLock_t __CFRLTPortMapLock = CFSpinLockInit;
CF_INLINE void __CFRunLoopTimerPortMapLock(void) {
__CFSpinLock(&__CFRLTPortMapLock);
}
CF_INLINE void __CFRunLoopTimerPortMapUnlock(void) {
__CFSpinUnlock(&__CFRLTPortMapLock);
}
#endif
static void __CFRunLoopTimerSchedule(CFRunLoopTimerRef rlt, CFRunLoopRef rl, CFRunLoopModeRef rlm) {
#if DEPLOYMENT_TARGET_MACOSX
__CFRunLoopTimerLock(rlt);
if (0 == rlt->_rlCount) {
rlt->_runLoop = rl;
if (MACH_PORT_NULL == rlt->_port) {
rlt->_port = mk_timer_create();
}
__CFRunLoopTimerPortMapLock();
if (NULL == __CFRLTPortMap) {
__CFRLTPortMap = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, NULL, NULL);
}
CFDictionarySetValue(__CFRLTPortMap, (void *)(uintptr_t)rlt->_port, rlt);
__CFRunLoopTimerPortMapUnlock();
}
rlt->_rlCount++;
mach_port_insert_member(mach_task_self(), rlt->_port, rlm->_portSet);
mk_timer_arm(rlt->_port, __CFUInt64ToAbsoluteTime(rlt->_fireTSR));
__CFRunLoopTimerUnlock(rlt);
#endif
}
static void __CFRunLoopTimerCancel(CFRunLoopTimerRef rlt, CFRunLoopRef rl, CFRunLoopModeRef rlm) {
#if DEPLOYMENT_TARGET_MACOSX
__CFRunLoopTimerLock(rlt);
__CFPortSetRemove(rlt->_port, rlm->_portSet);
rlt->_rlCount--;
if (0 == rlt->_rlCount) {
__CFRunLoopTimerPortMapLock();
if (NULL != __CFRLTPortMap) {
CFDictionaryRemoveValue(__CFRLTPortMap, (void *)(uintptr_t)rlt->_port);
}
__CFRunLoopTimerPortMapUnlock();
rlt->_runLoop = NULL;
mk_timer_cancel(rlt->_port, NULL);
}
__CFRunLoopTimerUnlock(rlt);
#endif
}
// Caller must hold the Timer lock for safety
static void __CFRunLoopTimerRescheduleWithAllModes(CFRunLoopTimerRef rlt, CFRunLoopRef rl) {
#if DEPLOYMENT_TARGET_MACOSX
mk_timer_arm(rlt->_port, __CFUInt64ToAbsoluteTime(rlt->_fireTSR));
#endif
}
#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
typedef struct _collectTimersContext {
CFMutableArrayRef results;
int64_t cutoffTSR;
};
static void __CFRunLoopCollectTimers(const void *value, void *ctx) {
CFRunLoopTimerRef rlt = (CFRunLoopTimerRef)value;
struct _collectTimersContext *context = (struct _collectTimersContext*)ctx;
if (rlt->_fireTSR <= context->cutoffTSR) {
if (NULL == context->results)
context->results = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
CFArrayAppendValue(context->results, rlt);
}
}
// RunLoop and RunLoopMode must be locked
static void __CFRunLoopTimersToFireRecursive(CFRunLoopRef rl, CFRunLoopModeRef rlm, struct _collectTimersContext *ctxt) {
if (NULL != rlm->_timers && 0 < CFSetGetCount(rlm->_timers)) {
__CFRunLoopTimerFireTSRLock();
CFSetApplyFunction(rlm->_timers, __CFRunLoopCollectTimers, ctxt);
__CFRunLoopTimerFireTSRUnlock();
}
if (NULL != rlm->_submodes) {
CFIndex idx, cnt;
for (idx = 0, cnt = CFArrayGetCount(rlm->_submodes); idx < cnt; idx++) {
CFStringRef modeName = (CFStringRef)CFArrayGetValueAtIndex(rlm->_submodes, idx);
CFRunLoopModeRef subrlm;
subrlm = __CFRunLoopFindMode(rl, modeName, false);
if (NULL != subrlm) {
__CFRunLoopTimersToFireRecursive(rl, subrlm, ctxt);
__CFRunLoopModeUnlock(subrlm);
}
}
}
}
// RunLoop and RunLoopMode must be locked
static CFArrayRef __CFRunLoopTimersToFire(CFRunLoopRef rl, CFRunLoopModeRef rlm) {
struct _collectTimersContext ctxt = {NULL, __CFReadTSR()};
__CFRunLoopTimersToFireRecursive(rl, rlm, &ctxt);
return ctxt.results;
}
#endif // DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
/* CFRunLoop */
CONST_STRING_DECL(kCFRunLoopDefaultMode, "kCFRunLoopDefaultMode")
CONST_STRING_DECL(kCFRunLoopCommonModes, "kCFRunLoopCommonModes")
struct _findsource {
__CFPort port;
CFRunLoopSourceRef result;
};
static void __CFRunLoopFindSource(const void *value, void *ctx) {
CFRunLoopSourceRef rls = (CFRunLoopSourceRef)value;
struct _findsource *context = (struct _findsource *)ctx;
__CFPort port;
if (NULL != context->result) return;
if (1 != rls->_context.version0.version) return;
__CFRunLoopSourceLock(rls);
port = rls->_context.version1.getPort(rls->_context.version1.info);
if (port == context->port) {
context->result = rls;
}
__CFRunLoopSourceUnlock(rls);
}
// call with rl and rlm locked
static CFRunLoopSourceRef __CFRunLoopModeFindSourceForMachPort(CFRunLoopRef rl, CFRunLoopModeRef rlm, __CFPort port) { /* DOES CALLOUT */
CHECK_FOR_FORK();
struct _findsource context = {port, NULL};
if (NULL != rlm->_sources) {
CFSetApplyFunction(rlm->_sources, (__CFRunLoopFindSource), &context);
}
if (NULL == context.result && NULL != rlm->_submodes) {
CFIndex idx, cnt;
for (idx = 0, cnt = CFArrayGetCount(rlm->_submodes); idx < cnt; idx++) {
CFRunLoopSourceRef source = NULL;
CFStringRef modeName = (CFStringRef)CFArrayGetValueAtIndex(rlm->_submodes, idx);
CFRunLoopModeRef subrlm;
subrlm = __CFRunLoopFindMode(rl, modeName, false);
if (NULL != subrlm) {
source = __CFRunLoopModeFindSourceForMachPort(rl, subrlm, port);
__CFRunLoopModeUnlock(subrlm);
}
if (NULL != source) {
context.result = source;
break;
}
}
}
return context.result;
}
#if DEPLOYMENT_TARGET_MACOSX
// call with rl and rlm locked
static CFRunLoopTimerRef __CFRunLoopModeFindTimerForMachPort(CFRunLoopModeRef rlm, __CFPort port) {
CHECK_FOR_FORK();
CFRunLoopTimerRef result = NULL;
__CFRunLoopTimerPortMapLock();
if (NULL != __CFRLTPortMap) {
result = (CFRunLoopTimerRef)CFDictionaryGetValue(__CFRLTPortMap, (void *)(uintptr_t)port);
}
__CFRunLoopTimerPortMapUnlock();
return result;
}
#endif
static void __CFRunLoopDeallocateSources(const void *value, void *context) {
CFRunLoopModeRef rlm = (CFRunLoopModeRef)value;
CFRunLoopRef rl = (CFRunLoopRef)context;
CFIndex idx, cnt;
const void **list, *buffer[256];
if (NULL == rlm->_sources) return;
cnt = CFSetGetCount(rlm->_sources);
list = (cnt <= 256) ? buffer : (const void**)CFAllocatorAllocate(kCFAllocatorSystemDefault, cnt * sizeof(void *), 0);
CFSetGetValues(rlm->_sources, list);
for (idx = 0; idx < cnt; idx++) {
CFRetain(list[idx]);
}
CFSetRemoveAllValues(rlm->_sources);
for (idx = 0; idx < cnt; idx++) {
__CFRunLoopSourceCancel((CFRunLoopSourceRef)list[idx], rl, rlm);
CFRelease(list[idx]);
}
if (list != buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefault, list);
}
static void __CFRunLoopDeallocateObservers(const void *value, void *context) {
CFRunLoopModeRef rlm = (CFRunLoopModeRef)value;
CFRunLoopRef rl = (CFRunLoopRef)context;
CFIndex idx, cnt;
const void **list, *buffer[256];
if (NULL == rlm->_observers) return;
cnt = CFSetGetCount(rlm->_observers);
list = (cnt <= 256) ? buffer : (const void**)CFAllocatorAllocate(kCFAllocatorSystemDefault, cnt * sizeof(void *), 0);
CFSetGetValues(rlm->_observers, list);
for (idx = 0; idx < cnt; idx++) {
CFRetain(list[idx]);
}
CFSetRemoveAllValues(rlm->_observers);
for (idx = 0; idx < cnt; idx++) {
__CFRunLoopObserverCancel((CFRunLoopObserverRef)list[idx], rl, rlm);
CFRelease(list[idx]);
}
if (list != buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefault, list);
}
static void __CFRunLoopDeallocateTimers(const void *value, void *context) {
CFRunLoopModeRef rlm = (CFRunLoopModeRef)value;
CFRunLoopRef rl = (CFRunLoopRef)context;
CFIndex idx, cnt;
const void **list, *buffer[256];
if (NULL == rlm->_timers) return;
cnt = CFSetGetCount(rlm->_timers);
list = (cnt <= 256) ? buffer : (const void**)CFAllocatorAllocate(kCFAllocatorSystemDefault, cnt * sizeof(void *), 0);
CFSetGetValues(rlm->_timers, list);
for (idx = 0; idx < cnt; idx++) {
CFRetain(list[idx]);
}
CFSetRemoveAllValues(rlm->_timers);
for (idx = 0; idx < cnt; idx++) {
__CFRunLoopTimerCancel((CFRunLoopTimerRef)list[idx], rl, rlm);