2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
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.
27 #ifndef __DISPATCH_SOURCE_INTERNAL__
28 #define __DISPATCH_SOURCE_INTERNAL__
30 #ifndef __DISPATCH_INDIRECT__
31 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
32 #include <dispatch/base.h> // for HeaderDoc
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 !
39 * @enum dispatch_source_mach_send_flags_t
41 * @constant DISPATCH_MACH_SEND_DELETED
42 * Port-deleted notification. Disabled for source registration.
45 DISPATCH_MACH_SEND_DELETED
= 0x4,
48 * @enum dispatch_source_mach_recv_flags_t
50 * @constant DISPATCH_MACH_RECV_MESSAGE
51 * Receive right has pending messages
53 * @constant DISPATCH_MACH_RECV_NO_SENDERS
54 * Receive right has no more senders. TODO <rdar://problem/8132399>
57 DISPATCH_MACH_RECV_MESSAGE
= 0x2,
58 DISPATCH_MACH_RECV_NO_SENDERS
= 0x10,
62 DISPATCH_TIMER_WALL_CLOCK
= 0x4,
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)
70 #define DISPATCH_TIMER_INDEX_WALL 0
71 #define DISPATCH_TIMER_INDEX_MACH 1
72 #define DISPATCH_TIMER_INDEX_DISARM 2
74 struct dispatch_source_vtable_s
{
75 DISPATCH_VTABLE_HEADER(dispatch_source_s
);
78 extern const struct dispatch_source_vtable_s _dispatch_source_kevent_vtable
;
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
;
86 typedef struct dispatch_kevent_s
*dispatch_kevent_t
;
88 struct dispatch_source_type_s
{
91 void (*init
)(dispatch_source_t ds
, dispatch_source_type_t type
,
92 uintptr_t handle
, unsigned long mask
, dispatch_queue_t q
);
95 struct dispatch_timer_source_s
{
100 uint64_t flags
; // dispatch_timer_flags_t
101 unsigned long missed
;
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
;
115 typedef struct dispatch_source_refs_s
*dispatch_source_refs_t
;
117 struct dispatch_timer_source_refs_s
{
118 struct dispatch_source_refs_s _ds_refs
;
119 struct dispatch_timer_source_s _ds_timer
;
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)
129 // ds_atomic_flags bits
130 #define DSF_CANCELED 1u // cancellation has been requested
131 #define DSF_ARMED 2u // source is armed
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
139 char _ds_pad
[DISPATCH_QUEUE_MIN_LABEL_SIZE
];
142 dispatch_kevent_t ds_dkev
;
143 dispatch_source_refs_t ds_refs
;
144 unsigned int ds_atomic_flags
;
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
;
162 void _dispatch_source_xref_release(dispatch_source_t ds
);
163 void _dispatch_mach_notify_source_init(void *context
);
165 #endif /* __DISPATCH_SOURCE_INTERNAL__ */