]> git.saurik.com Git - apple/libdispatch.git/blob - src/source_internal.h
libdispatch-187.9.tar.gz
[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_source_vtable_s {
75 DISPATCH_VTABLE_HEADER(dispatch_source_s);
76 };
77
78 extern const struct dispatch_source_vtable_s _dispatch_source_kevent_vtable;
79
80 struct dispatch_kevent_s {
81 TAILQ_ENTRY(dispatch_kevent_s) dk_list;
82 TAILQ_HEAD(, dispatch_source_refs_s) dk_sources;
83 struct kevent dk_kevent;
84 };
85
86 typedef struct dispatch_kevent_s *dispatch_kevent_t;
87
88 struct dispatch_source_type_s {
89 struct kevent ke;
90 uint64_t mask;
91 void (*init)(dispatch_source_t ds, dispatch_source_type_t type,
92 uintptr_t handle, unsigned long mask, dispatch_queue_t q);
93 };
94
95 struct dispatch_timer_source_s {
96 uint64_t target;
97 uint64_t last_fire;
98 uint64_t interval;
99 uint64_t leeway;
100 uint64_t flags; // dispatch_timer_flags_t
101 unsigned long missed;
102 };
103
104 // Source state which may contain references to the source object
105 // Separately allocated so that 'leaks' can see sources <rdar://problem/9050566>
106 struct dispatch_source_refs_s {
107 TAILQ_ENTRY(dispatch_source_refs_s) dr_list;
108 uintptr_t dr_source_wref; // "weak" backref to dispatch_source_t
109 dispatch_function_t ds_handler_func;
110 void *ds_handler_ctxt;
111 void *ds_cancel_handler;
112 void *ds_registration_handler;
113 };
114
115 typedef struct dispatch_source_refs_s *dispatch_source_refs_t;
116
117 struct dispatch_timer_source_refs_s {
118 struct dispatch_source_refs_s _ds_refs;
119 struct dispatch_timer_source_s _ds_timer;
120 };
121
122 #define _dispatch_ptr2wref(ptr) (~(uintptr_t)(ptr))
123 #define _dispatch_wref2ptr(ref) ((void*)~(ref))
124 #define _dispatch_source_from_refs(dr) \
125 ((dispatch_source_t)_dispatch_wref2ptr((dr)->dr_source_wref))
126 #define ds_timer(dr) \
127 (((struct dispatch_timer_source_refs_s *)(dr))->_ds_timer)
128
129 // ds_atomic_flags bits
130 #define DSF_CANCELED 1u // cancellation has been requested
131 #define DSF_ARMED 2u // source is armed
132
133 struct dispatch_source_s {
134 DISPATCH_STRUCT_HEADER(dispatch_source_s, dispatch_source_vtable_s);
135 DISPATCH_QUEUE_HEADER;
136 // Instruments always copies DISPATCH_QUEUE_MIN_LABEL_SIZE, which is 64,
137 // so the remainder of the structure must be big enough
138 union {
139 char _ds_pad[DISPATCH_QUEUE_MIN_LABEL_SIZE];
140 struct {
141 char dq_label[8];
142 dispatch_kevent_t ds_dkev;
143 dispatch_source_refs_t ds_refs;
144 unsigned int ds_atomic_flags;
145 unsigned int
146 ds_is_level:1,
147 ds_is_adder:1,
148 ds_is_installed:1,
149 ds_needs_rearm:1,
150 ds_is_timer:1,
151 ds_cancel_is_block:1,
152 ds_handler_is_block:1,
153 ds_registration_is_block:1;
154 unsigned long ds_data;
155 unsigned long ds_pending_data;
156 unsigned long ds_pending_data_mask;
157 unsigned long ds_ident_hack;
158 };
159 };
160 };
161
162 void _dispatch_source_xref_release(dispatch_source_t ds);
163 void _dispatch_mach_notify_source_init(void *context);
164
165 #endif /* __DISPATCH_SOURCE_INTERNAL__ */