2 * Copyright (c) 2008-2012 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@
21 #ifndef __DISPATCH_OBJECT__
22 #define __DISPATCH_OBJECT__
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
30 * @typedef dispatch_object_t
33 * Abstract base type for all dispatch objects.
34 * The details of the type definition are language-specific.
37 * Dispatch objects are reference counted via calls to dispatch_retain() and
41 #if OS_OBJECT_USE_OBJC
43 * By default, dispatch objects are declared as Objective-C types when building
44 * with an Objective-C compiler. This allows them to participate in ARC, in RR
45 * management by the Blocks runtime and in leaks checking by the static
46 * analyzer, and enables them to be added to Cocoa collections.
47 * See <os/object.h> for details.
49 OS_OBJECT_DECL(dispatch_object
);
50 #define DISPATCH_DECL(name) OS_OBJECT_DECL_SUBCLASS(name, dispatch_object)
51 #define DISPATCH_GLOBAL_OBJECT(type, object) ((OS_OBJECT_BRIDGE type)&(object))
52 #define DISPATCH_RETURNS_RETAINED OS_OBJECT_RETURNS_RETAINED
53 DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
55 _dispatch_object_validate(dispatch_object_t object
) {
56 void *isa
= *(void* volatile*)(OS_OBJECT_BRIDGE
void*)object
;
59 #elif defined(__cplusplus)
61 * Dispatch objects are NOT C++ objects. Nevertheless, we can at least keep C++
62 * aware of type compatibility.
64 typedef struct dispatch_object_s
{
68 dispatch_object_s(const dispatch_object_s
&);
69 void operator=(const dispatch_object_s
&);
71 #define DISPATCH_DECL(name) \
72 typedef struct name##_s : public dispatch_object_s {} *name##_t
73 #define DISPATCH_GLOBAL_OBJECT(type, object) (&(object))
74 #define DISPATCH_RETURNS_RETAINED
77 struct _os_object_s
*_os_obj
;
78 struct dispatch_object_s
*_do
;
79 struct dispatch_continuation_s
*_dc
;
80 struct dispatch_queue_s
*_dq
;
81 struct dispatch_queue_attr_s
*_dqa
;
82 struct dispatch_group_s
*_dg
;
83 struct dispatch_source_s
*_ds
;
84 struct dispatch_mach_s
*_dm
;
85 struct dispatch_mach_msg_s
*_dmsg
;
86 struct dispatch_timer_aggregate_s
*_dta
;
87 struct dispatch_source_attr_s
*_dsa
;
88 struct dispatch_semaphore_s
*_dsema
;
89 struct dispatch_data_s
*_ddata
;
90 struct dispatch_io_s
*_dchannel
;
91 struct dispatch_operation_s
*_doperation
;
92 struct dispatch_disk_s
*_ddisk
;
93 } dispatch_object_t
__attribute__((__transparent_union__
));
95 #define DISPATCH_DECL(name) typedef struct name##_s *name##_t
97 #define DISPATCH_GLOBAL_OBJECT(t, x) (&(x))
99 #define DISPATCH_RETURNS_RETAINED
103 * @typedef dispatch_block_t
106 * The type of blocks submitted to dispatch queues, which take no arguments
107 * and have no return value.
110 * When not building with Objective-C ARC, a block object allocated on or
111 * copied to the heap must be released with a -[release] message or the
112 * Block_release() function.
114 * The declaration of a block literal allocates storage on the stack.
115 * Therefore, this is an invalid construct:
117 * dispatch_block_t block;
119 * block = ^{ printf("true\n"); };
121 * block = ^{ printf("false\n"); };
123 * block(); // unsafe!!!
126 * What is happening behind the scenes:
129 * struct Block __tmp_1 = ...; // setup details
132 * struct Block __tmp_2 = ...; // setup details
137 * As the example demonstrates, the address of a stack variable is escaping the
138 * scope in which it is allocated. That is a classic C bug.
140 * Instead, the block literal must be copied to the heap with the Block_copy()
141 * function or by sending it a -[copy] message.
143 typedef void (^dispatch_block_t
)(void);
148 * @function dispatch_retain
151 * Increment the reference count of a dispatch object.
154 * Calls to dispatch_retain() must be balanced with calls to
155 * dispatch_release().
158 * The object to retain.
159 * The result of passing NULL in this parameter is undefined.
161 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
162 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
164 dispatch_retain(dispatch_object_t object
);
165 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
166 #undef dispatch_retain
167 #define dispatch_retain(object) ({ dispatch_object_t _o = (object); \
168 _dispatch_object_validate(_o); (void)[_o retain]; })
172 * @function dispatch_release
175 * Decrement the reference count of a dispatch object.
178 * A dispatch object is asynchronously deallocated once all references are
179 * released (i.e. the reference count becomes zero). The system does not
180 * guarantee that a given client is the last or only reference to a given
184 * The object to release.
185 * The result of passing NULL in this parameter is undefined.
187 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
188 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
190 dispatch_release(dispatch_object_t object
);
191 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
192 #undef dispatch_release
193 #define dispatch_release(object) ({ dispatch_object_t _o = (object); \
194 _dispatch_object_validate(_o); [_o release]; })
198 * @function dispatch_get_context
201 * Returns the application defined context of the object.
204 * The result of passing NULL in this parameter is undefined.
207 * The context of the object; may be NULL.
209 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
210 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
213 dispatch_get_context(dispatch_object_t object
);
216 * @function dispatch_set_context
219 * Associates an application defined context with the object.
222 * The result of passing NULL in this parameter is undefined.
225 * The new client defined context for the object. This may be NULL.
228 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
229 DISPATCH_EXPORT DISPATCH_NOTHROW
//DISPATCH_NONNULL1
231 dispatch_set_context(dispatch_object_t object
, void *context
);
234 * @function dispatch_set_finalizer_f
237 * Set the finalizer function for a dispatch object.
240 * The dispatch object to modify.
241 * The result of passing NULL in this parameter is undefined.
244 * The finalizer function pointer.
247 * A dispatch object's finalizer will be invoked on the object's target queue
248 * after all references to the object have been released. This finalizer may be
249 * used by the application to release any resources associated with the object,
250 * such as freeing the object's context.
251 * The context parameter passed to the finalizer function is the current
252 * context of the dispatch object at the time the finalizer call is made.
254 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
255 DISPATCH_EXPORT DISPATCH_NOTHROW
//DISPATCH_NONNULL1
257 dispatch_set_finalizer_f(dispatch_object_t object
,
258 dispatch_function_t finalizer
);
261 * @function dispatch_suspend
264 * Suspends the invocation of blocks on a dispatch object.
267 * A suspended object will not invoke any blocks associated with it. The
268 * suspension of an object will occur after any running block associated with
269 * the object completes.
271 * Calls to dispatch_suspend() must be balanced with calls
272 * to dispatch_resume().
275 * The object to be suspended.
276 * The result of passing NULL in this parameter is undefined.
278 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
279 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
281 dispatch_suspend(dispatch_object_t object
);
284 * @function dispatch_resume
287 * Resumes the invocation of blocks on a dispatch object.
290 * The object to be resumed.
291 * The result of passing NULL in this parameter is undefined.
293 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
294 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
296 dispatch_resume(dispatch_object_t object
);
299 * @function dispatch_wait
302 * Wait synchronously for an object or until the specified timeout has elapsed.
305 * Type-generic macro that maps to dispatch_block_wait, dispatch_group_wait or
306 * dispatch_semaphore_wait, depending on the type of the first argument.
307 * See documentation for these functions for more details.
308 * This function is unavailable for any other object type.
311 * The object to wait on.
312 * The result of passing NULL in this parameter is undefined.
315 * When to timeout (see dispatch_time). As a convenience, there are the
316 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
319 * Returns zero on success or non-zero on error (i.e. timed out).
322 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
324 dispatch_wait(void *object
, dispatch_time_t timeout
);
325 #if __has_extension(c_generic_selections)
326 #define dispatch_wait(object, timeout) \
328 dispatch_block_t:dispatch_block_wait, \
329 dispatch_group_t:dispatch_group_wait, \
330 dispatch_semaphore_t:dispatch_semaphore_wait \
331 )((object),(timeout))
335 * @function dispatch_notify
338 * Schedule a notification block to be submitted to a queue when the execution
339 * of a specified object has completed.
342 * Type-generic macro that maps to dispatch_block_notify or
343 * dispatch_group_notify, depending on the type of the first argument.
344 * See documentation for these functions for more details.
345 * This function is unavailable for any other object type.
348 * The object to observe.
349 * The result of passing NULL in this parameter is undefined.
352 * The queue to which the supplied notification block will be submitted when
353 * the observed object completes.
355 * @param notification_block
356 * The block to submit when the observed object completes.
359 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
361 dispatch_notify(void *object
, dispatch_object_t queue
,
362 dispatch_block_t notification_block
);
363 #if __has_extension(c_generic_selections)
364 #define dispatch_notify(object, queue, notification_block) \
366 dispatch_block_t:dispatch_block_notify, \
367 dispatch_group_t:dispatch_group_notify \
368 )((object),(queue), (notification_block))
372 * @function dispatch_cancel
375 * Cancel the specified object.
378 * Type-generic macro that maps to dispatch_block_cancel or
379 * dispatch_source_cancel, depending on the type of the first argument.
380 * See documentation for these functions for more details.
381 * This function is unavailable for any other object type.
384 * The object to cancel.
385 * The result of passing NULL in this parameter is undefined.
388 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
390 dispatch_cancel(void *object
);
391 #if __has_extension(c_generic_selections)
392 #define dispatch_cancel(object) \
394 dispatch_block_t:dispatch_block_cancel, \
395 dispatch_source_t:dispatch_source_cancel \
400 * @function dispatch_testcancel
403 * Test whether the specified object has been canceled
406 * Type-generic macro that maps to dispatch_block_testcancel or
407 * dispatch_source_testcancel, depending on the type of the first argument.
408 * See documentation for these functions for more details.
409 * This function is unavailable for any other object type.
412 * The object to test.
413 * The result of passing NULL in this parameter is undefined.
416 * Non-zero if canceled and zero if not canceled.
419 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
422 dispatch_testcancel(void *object
);
423 #if __has_extension(c_generic_selections)
424 #define dispatch_testcancel(object) \
426 dispatch_block_t:dispatch_block_testcancel, \
427 dispatch_source_t:dispatch_source_testcancel \
432 * @function dispatch_debug
435 * Programmatically log debug information about a dispatch object.
438 * Programmatically log debug information about a dispatch object. By default,
439 * the log output is sent to syslog at notice level. In the debug version of
440 * the library, the log output is sent to a file in /var/tmp.
441 * The log output destination can be configured via the LIBDISPATCH_LOG
442 * environment variable, valid values are: YES, NO, syslog, stderr, file.
444 * This function is deprecated and will be removed in a future release.
445 * Objective-C callers may use -debugDescription instead.
448 * The object to introspect.
451 * The message to log above and beyond the introspection.
453 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6
,__MAC_10_9
,__IPHONE_4_0
,__IPHONE_6_0
)
454 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
455 __attribute__((__format__(printf
,2,3)))
457 dispatch_debug(dispatch_object_t object
, const char *message
, ...);
459 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6
,__MAC_10_9
,__IPHONE_4_0
,__IPHONE_6_0
)
460 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
461 __attribute__((__format__(printf
,2,0)))
463 dispatch_debugv(dispatch_object_t object
, const char *message
, va_list ap
);