]> git.saurik.com Git - apple/libdispatch.git/blob - src/source_internal.h
c2c706f847f9c6dee4088f4002db4dd0ec245689
[apple/libdispatch.git] / src / source_internal.h
1 /*
2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 /*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27 #ifndef __DISPATCH_SOURCE_INTERNAL__
28 #define __DISPATCH_SOURCE_INTERNAL__
29
30 #ifndef __DISPATCH_INDIRECT__
31 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
32 #include <dispatch/base.h> // for HeaderDoc
33 #endif
34
35 // NOTE: dispatch_source_mach_send_flags_t and dispatch_source_mach_recv_flags_t
36 // bit values must not overlap as they share the same kevent fflags !
37
38 /*!
39 * @enum dispatch_source_mach_send_flags_t
40 *
41 * @constant DISPATCH_MACH_SEND_DELETED
42 * Port-deleted notification. Disabled for source registration.
43 */
44 enum {
45 DISPATCH_MACH_SEND_DELETED = 0x4,
46 };
47 /*!
48 * @enum dispatch_source_mach_recv_flags_t
49 *
50 * @constant DISPATCH_MACH_RECV_MESSAGE
51 * Receive right has pending messages
52 *
53 * @constant DISPATCH_MACH_RECV_NO_SENDERS
54 * Receive right has no more senders. TODO <rdar://problem/8132399>
55 */
56 enum {
57 DISPATCH_MACH_RECV_MESSAGE = 0x2,
58 DISPATCH_MACH_RECV_NO_SENDERS = 0x10,
59 };
60
61 enum {
62 DISPATCH_TIMER_WALL_CLOCK = 0x4,
63 };
64
65 #define DISPATCH_EVFILT_TIMER (-EVFILT_SYSCOUNT - 1)
66 #define DISPATCH_EVFILT_CUSTOM_ADD (-EVFILT_SYSCOUNT - 2)
67 #define DISPATCH_EVFILT_CUSTOM_OR (-EVFILT_SYSCOUNT - 3)
68 #define DISPATCH_EVFILT_SYSCOUNT ( EVFILT_SYSCOUNT + 3)
69
70 #define DISPATCH_TIMER_INDEX_WALL 0
71 #define DISPATCH_TIMER_INDEX_MACH 1
72 #define DISPATCH_TIMER_INDEX_DISARM 2
73
74 struct dispatch_kevent_s {
75 TAILQ_ENTRY(dispatch_kevent_s) dk_list;
76 TAILQ_HEAD(, dispatch_source_refs_s) dk_sources;
77 struct kevent dk_kevent;
78 };
79
80 typedef struct dispatch_kevent_s *dispatch_kevent_t;
81
82 struct dispatch_source_type_s {
83 struct kevent ke;
84 uint64_t mask;
85 void (*init)(dispatch_source_t ds, dispatch_source_type_t type,
86 uintptr_t handle, unsigned long mask, dispatch_queue_t q);
87 };
88
89 struct dispatch_timer_source_s {
90 uint64_t target;
91 uint64_t last_fire;
92 uint64_t interval;
93 uint64_t leeway;
94 uint64_t flags; // dispatch_timer_flags_t
95 unsigned long missed;
96 };
97
98 // Source state which may contain references to the source object
99 // Separately allocated so that 'leaks' can see sources <rdar://problem/9050566>
100 struct dispatch_source_refs_s {
101 TAILQ_ENTRY(dispatch_source_refs_s) dr_list;
102 uintptr_t dr_source_wref; // "weak" backref to dispatch_source_t
103 dispatch_function_t ds_handler_func;
104 void *ds_handler_ctxt;
105 void *ds_cancel_handler;
106 void *ds_registration_handler;
107 };
108
109 typedef struct dispatch_source_refs_s *dispatch_source_refs_t;
110
111 struct dispatch_timer_source_refs_s {
112 struct dispatch_source_refs_s _ds_refs;
113 struct dispatch_timer_source_s _ds_timer;
114 };
115
116 #define _dispatch_ptr2wref(ptr) (~(uintptr_t)(ptr))
117 #define _dispatch_wref2ptr(ref) ((void*)~(ref))
118 #define _dispatch_source_from_refs(dr) \
119 ((dispatch_source_t)_dispatch_wref2ptr((dr)->dr_source_wref))
120 #define ds_timer(dr) \
121 (((struct dispatch_timer_source_refs_s *)(dr))->_ds_timer)
122
123 // ds_atomic_flags bits
124 #define DSF_CANCELED 1u // cancellation has been requested
125 #define DSF_ARMED 2u // source is armed
126
127 DISPATCH_CLASS_DECL(source);
128 struct dispatch_source_s {
129 DISPATCH_STRUCT_HEADER(source);
130 DISPATCH_QUEUE_HEADER;
131 // Instruments always copies DISPATCH_QUEUE_MIN_LABEL_SIZE, which is 64,
132 // so the remainder of the structure must be big enough
133 union {
134 char _ds_pad[DISPATCH_QUEUE_MIN_LABEL_SIZE];
135 struct {
136 char dq_label[8];
137 dispatch_kevent_t ds_dkev;
138 dispatch_source_refs_t ds_refs;
139 unsigned int ds_atomic_flags;
140 unsigned int
141 ds_is_level:1,
142 ds_is_adder:1,
143 ds_is_installed:1,
144 ds_needs_rearm:1,
145 ds_is_timer:1,
146 ds_cancel_is_block:1,
147 ds_handler_is_block:1,
148 ds_registration_is_block:1;
149 unsigned long ds_data;
150 unsigned long ds_pending_data;
151 unsigned long ds_pending_data_mask;
152 unsigned long ds_ident_hack;
153 };
154 };
155 };
156
157 void _dispatch_source_xref_dispose(dispatch_source_t ds);
158 void _dispatch_mach_notify_source_init(void *context);
159 dispatch_queue_t _dispatch_source_invoke(dispatch_source_t ds);
160 void _dispatch_source_dispose(dispatch_source_t ds);
161 bool _dispatch_source_probe(dispatch_source_t ds);
162 size_t _dispatch_source_debug(dispatch_source_t ds, char* buf, size_t bufsiz);
163
164 #endif /* __DISPATCH_SOURCE_INTERNAL__ */