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) && !defined(__DISPATCH_BUILDING_DISPATCH__)
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
104 * @typedef dispatch_block_t
107 * The type of blocks submitted to dispatch queues, which take no arguments
108 * and have no return value.
111 * When not building with Objective-C ARC, a block object allocated on or
112 * copied to the heap must be released with a -[release] message or the
113 * Block_release() function.
115 * The declaration of a block literal allocates storage on the stack.
116 * Therefore, this is an invalid construct:
118 * dispatch_block_t block;
120 * block = ^{ printf("true\n"); };
122 * block = ^{ printf("false\n"); };
124 * block(); // unsafe!!!
127 * What is happening behind the scenes:
130 * struct Block __tmp_1 = ...; // setup details
133 * struct Block __tmp_2 = ...; // setup details
138 * As the example demonstrates, the address of a stack variable is escaping the
139 * scope in which it is allocated. That is a classic C bug.
141 * Instead, the block literal must be copied to the heap with the Block_copy()
142 * function or by sending it a -[copy] message.
144 typedef void (^dispatch_block_t
)(void);
150 * @function dispatch_retain
153 * Increment the reference count of a dispatch object.
156 * Calls to dispatch_retain() must be balanced with calls to
157 * dispatch_release().
160 * The object to retain.
161 * The result of passing NULL in this parameter is undefined.
163 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
164 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
166 dispatch_retain(dispatch_object_t object
);
167 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
168 #undef dispatch_retain
169 #define dispatch_retain(object) ({ dispatch_object_t _o = (object); \
170 _dispatch_object_validate(_o); (void)[_o retain]; })
174 * @function dispatch_release
177 * Decrement the reference count of a dispatch object.
180 * A dispatch object is asynchronously deallocated once all references are
181 * released (i.e. the reference count becomes zero). The system does not
182 * guarantee that a given client is the last or only reference to a given
186 * The object to release.
187 * The result of passing NULL in this parameter is undefined.
189 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
190 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
192 dispatch_release(dispatch_object_t object
);
193 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
194 #undef dispatch_release
195 #define dispatch_release(object) ({ dispatch_object_t _o = (object); \
196 _dispatch_object_validate(_o); [_o release]; })
200 * @function dispatch_get_context
203 * Returns the application defined context of the object.
206 * The result of passing NULL in this parameter is undefined.
209 * The context of the object; may be NULL.
211 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
212 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
215 dispatch_get_context(dispatch_object_t object
);
218 * @function dispatch_set_context
221 * Associates an application defined context with the object.
224 * The result of passing NULL in this parameter is undefined.
227 * The new client defined context for the object. This may be NULL.
230 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
231 DISPATCH_EXPORT DISPATCH_NOTHROW
//DISPATCH_NONNULL1
233 dispatch_set_context(dispatch_object_t object
, void *context
);
236 * @function dispatch_set_finalizer_f
239 * Set the finalizer function for a dispatch object.
242 * The dispatch object to modify.
243 * The result of passing NULL in this parameter is undefined.
246 * The finalizer function pointer.
249 * A dispatch object's finalizer will be invoked on the object's target queue
250 * after all references to the object have been released. This finalizer may be
251 * used by the application to release any resources associated with the object,
252 * such as freeing the object's context.
253 * The context parameter passed to the finalizer function is the current
254 * context of the dispatch object at the time the finalizer call is made.
256 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
257 DISPATCH_EXPORT DISPATCH_NOTHROW
//DISPATCH_NONNULL1
259 dispatch_set_finalizer_f(dispatch_object_t object
,
260 dispatch_function_t finalizer
);
263 * @function dispatch_suspend
266 * Suspends the invocation of blocks on a dispatch object.
269 * A suspended object will not invoke any blocks associated with it. The
270 * suspension of an object will occur after any running block associated with
271 * the object completes.
273 * Calls to dispatch_suspend() must be balanced with calls
274 * to dispatch_resume().
277 * The object to be suspended.
278 * The result of passing NULL in this parameter is undefined.
280 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
281 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
283 dispatch_suspend(dispatch_object_t object
);
286 * @function dispatch_resume
289 * Resumes the invocation of blocks on a dispatch object.
292 * The object to be resumed.
293 * The result of passing NULL in this parameter is undefined.
295 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
296 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
298 dispatch_resume(dispatch_object_t object
);
302 * @function dispatch_wait
305 * Wait synchronously for an object or until the specified timeout has elapsed.
308 * Type-generic macro that maps to dispatch_block_wait, dispatch_group_wait or
309 * dispatch_semaphore_wait, depending on the type of the first argument.
310 * See documentation for these functions for more details.
311 * This function is unavailable for any other object type.
314 * The object to wait on.
315 * The result of passing NULL in this parameter is undefined.
318 * When to timeout (see dispatch_time). As a convenience, there are the
319 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
322 * Returns zero on success or non-zero on error (i.e. timed out).
325 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
327 dispatch_wait(void *object
, dispatch_time_t timeout
);
328 #if __has_extension(c_generic_selections)
329 #define dispatch_wait(object, timeout) \
331 dispatch_block_t:dispatch_block_wait, \
332 dispatch_group_t:dispatch_group_wait, \
333 dispatch_semaphore_t:dispatch_semaphore_wait \
334 )((object),(timeout))
338 * @function dispatch_notify
341 * Schedule a notification block to be submitted to a queue when the execution
342 * of a specified object has completed.
345 * Type-generic macro that maps to dispatch_block_notify or
346 * dispatch_group_notify, depending on the type of the first argument.
347 * See documentation for these functions for more details.
348 * This function is unavailable for any other object type.
351 * The object to observe.
352 * The result of passing NULL in this parameter is undefined.
355 * The queue to which the supplied notification block will be submitted when
356 * the observed object completes.
358 * @param notification_block
359 * The block to submit when the observed object completes.
362 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
364 dispatch_notify(void *object
, dispatch_object_t queue
,
365 dispatch_block_t notification_block
);
366 #if __has_extension(c_generic_selections)
367 #define dispatch_notify(object, queue, notification_block) \
369 dispatch_block_t:dispatch_block_notify, \
370 dispatch_group_t:dispatch_group_notify \
371 )((object),(queue), (notification_block))
375 * @function dispatch_cancel
378 * Cancel the specified object.
381 * Type-generic macro that maps to dispatch_block_cancel or
382 * dispatch_source_cancel, depending on the type of the first argument.
383 * See documentation for these functions for more details.
384 * This function is unavailable for any other object type.
387 * The object to cancel.
388 * The result of passing NULL in this parameter is undefined.
391 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
393 dispatch_cancel(void *object
);
394 #if __has_extension(c_generic_selections)
395 #define dispatch_cancel(object) \
397 dispatch_block_t:dispatch_block_cancel, \
398 dispatch_source_t:dispatch_source_cancel \
403 * @function dispatch_testcancel
406 * Test whether the specified object has been canceled
409 * Type-generic macro that maps to dispatch_block_testcancel or
410 * dispatch_source_testcancel, depending on the type of the first argument.
411 * See documentation for these functions for more details.
412 * This function is unavailable for any other object type.
415 * The object to test.
416 * The result of passing NULL in this parameter is undefined.
419 * Non-zero if canceled and zero if not canceled.
422 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
425 dispatch_testcancel(void *object
);
426 #if __has_extension(c_generic_selections)
427 #define dispatch_testcancel(object) \
429 dispatch_block_t:dispatch_block_testcancel, \
430 dispatch_source_t:dispatch_source_testcancel \
436 * @function dispatch_debug
439 * Programmatically log debug information about a dispatch object.
442 * Programmatically log debug information about a dispatch object. By default,
443 * the log output is sent to syslog at notice level. In the debug version of
444 * the library, the log output is sent to a file in /var/tmp.
445 * The log output destination can be configured via the LIBDISPATCH_LOG
446 * environment variable, valid values are: YES, NO, syslog, stderr, file.
448 * This function is deprecated and will be removed in a future release.
449 * Objective-C callers may use -debugDescription instead.
452 * The object to introspect.
455 * The message to log above and beyond the introspection.
457 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6
,__MAC_10_9
,__IPHONE_4_0
,__IPHONE_6_0
)
458 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
459 __attribute__((__format__(printf
,2,3)))
461 dispatch_debug(dispatch_object_t object
, const char *message
, ...);
463 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6
,__MAC_10_9
,__IPHONE_4_0
,__IPHONE_6_0
)
464 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
465 __attribute__((__format__(printf
,2,0)))
467 dispatch_debugv(dispatch_object_t object
, const char *message
, va_list ap
);