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_kevent_s
{
75 TAILQ_ENTRY(dispatch_kevent_s
) dk_list
;
76 TAILQ_HEAD(, dispatch_source_refs_s
) dk_sources
;
77 struct kevent dk_kevent
;
80 typedef struct dispatch_kevent_s
*dispatch_kevent_t
;
82 struct dispatch_source_type_s
{
85 void (*init
)(dispatch_source_t ds
, dispatch_source_type_t type
,
86 uintptr_t handle
, unsigned long mask
, dispatch_queue_t q
);
89 struct dispatch_timer_source_s
{
94 uint64_t flags
; // dispatch_timer_flags_t
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
;
109 typedef struct dispatch_source_refs_s
*dispatch_source_refs_t
;
111 struct dispatch_timer_source_refs_s
{
112 struct dispatch_source_refs_s _ds_refs
;
113 struct dispatch_timer_source_s _ds_timer
;
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)
123 // ds_atomic_flags bits
124 #define DSF_CANCELED 1u // cancellation has been requested
125 #define DSF_ARMED 2u // source is armed
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
134 char _ds_pad
[DISPATCH_QUEUE_MIN_LABEL_SIZE
];
137 dispatch_kevent_t ds_dkev
;
138 dispatch_source_refs_t ds_refs
;
139 unsigned int ds_atomic_flags
;
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
;
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
);
164 #endif /* __DISPATCH_SOURCE_INTERNAL__ */