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
29 DISPATCH_ASSUME_NONNULL_BEGIN
32 * @typedef dispatch_object_t
35 * Abstract base type for all dispatch objects.
36 * The details of the type definition are language-specific.
39 * Dispatch objects are reference counted via calls to dispatch_retain() and
43 #if OS_OBJECT_USE_OBJC
45 * By default, dispatch objects are declared as Objective-C types when building
46 * with an Objective-C compiler. This allows them to participate in ARC, in RR
47 * management by the Blocks runtime and in leaks checking by the static
48 * analyzer, and enables them to be added to Cocoa collections.
49 * See <os/object.h> for details.
51 OS_OBJECT_DECL_CLASS(dispatch_object
);
54 #define DISPATCH_DECL(name) OS_OBJECT_DECL_SUBCLASS_SWIFT(name, dispatch_object)
55 #else // OS_OBJECT_SWIFT3
56 #define DISPATCH_DECL(name) OS_OBJECT_DECL_SUBCLASS(name, dispatch_object)
58 DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
60 _dispatch_object_validate(dispatch_object_t object
) {
61 void *isa
= *(void* volatile*)(OS_OBJECT_BRIDGE
void*)object
;
64 #endif // OS_OBJECT_SWIFT3
66 #define DISPATCH_GLOBAL_OBJECT(type, object) ((OS_OBJECT_BRIDGE type)&(object))
67 #define DISPATCH_RETURNS_RETAINED OS_OBJECT_RETURNS_RETAINED
68 #elif defined(__cplusplus) && !defined(__DISPATCH_BUILDING_DISPATCH__)
70 * Dispatch objects are NOT C++ objects. Nevertheless, we can at least keep C++
71 * aware of type compatibility.
73 typedef struct dispatch_object_s
{
77 dispatch_object_s(const dispatch_object_s
&);
78 void operator=(const dispatch_object_s
&);
80 #define DISPATCH_DECL(name) \
81 typedef struct name##_s : public dispatch_object_s {} *name##_t
82 #define DISPATCH_GLOBAL_OBJECT(type, object) (&(object))
83 #define DISPATCH_RETURNS_RETAINED
86 struct _os_object_s
*_os_obj
;
87 struct dispatch_object_s
*_do
;
88 struct dispatch_continuation_s
*_dc
;
89 struct dispatch_queue_s
*_dq
;
90 struct dispatch_queue_attr_s
*_dqa
;
91 struct dispatch_group_s
*_dg
;
92 struct dispatch_source_s
*_ds
;
93 struct dispatch_mach_s
*_dm
;
94 struct dispatch_mach_msg_s
*_dmsg
;
95 struct dispatch_source_attr_s
*_dsa
;
96 struct dispatch_semaphore_s
*_dsema
;
97 struct dispatch_data_s
*_ddata
;
98 struct dispatch_io_s
*_dchannel
;
99 struct dispatch_operation_s
*_doperation
;
100 struct dispatch_disk_s
*_ddisk
;
101 } dispatch_object_t DISPATCH_TRANSPARENT_UNION
;
103 #define DISPATCH_DECL(name) typedef struct name##_s *name##_t
105 #define DISPATCH_GLOBAL_OBJECT(t, x) (&(x))
107 #define DISPATCH_RETURNS_RETAINED
110 #if OS_OBJECT_SWIFT3 && OS_OBJECT_USE_OBJC
111 #define DISPATCH_SOURCE_TYPE_DECL(name) \
112 DISPATCH_EXPORT struct dispatch_source_type_s \
113 _dispatch_source_type_##name; \
114 OS_OBJECT_DECL_PROTOCOL(dispatch_source_##name, <OS_dispatch_source>); \
115 OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL( \
116 dispatch_source, dispatch_source_##name)
117 #define DISPATCH_SOURCE_DECL(name) \
118 DISPATCH_DECL(name); \
119 OS_OBJECT_DECL_PROTOCOL(name, <NSObject>); \
120 OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(name, name)
121 #ifndef DISPATCH_DATA_DECL
122 #define DISPATCH_DATA_DECL(name) OS_OBJECT_DECL_SWIFT(name)
123 #endif // DISPATCH_DATA_DECL
124 #elif !TARGET_OS_WIN32
126 #define DISPATCH_SOURCE_DECL(name) \
129 #define DISPATCH_DATA_DECL(name) DISPATCH_DECL(name)
131 #define DISPATCH_SOURCE_TYPE_DECL(name) \
132 DISPATCH_EXPORT const struct dispatch_source_type_s \
133 _dispatch_source_type_##name
135 #define DISPATCH_SOURCE_DECL(name) \
137 #define DISPATCH_SOURCE_TYPE_DECL(name) \
138 DISPATCH_EXPORT struct dispatch_source_type_s _dispatch_source_type_##name
139 #define DISPATCH_DATA_DECL(name) DISPATCH_DECL(name)
144 * @typedef dispatch_block_t
147 * The type of blocks submitted to dispatch queues, which take no arguments
148 * and have no return value.
151 * When not building with Objective-C ARC, a block object allocated on or
152 * copied to the heap must be released with a -[release] message or the
153 * Block_release() function.
155 * The declaration of a block literal allocates storage on the stack.
156 * Therefore, this is an invalid construct:
158 * dispatch_block_t block;
160 * block = ^{ printf("true\n"); };
162 * block = ^{ printf("false\n"); };
164 * block(); // unsafe!!!
167 * What is happening behind the scenes:
170 * struct Block __tmp_1 = ...; // setup details
173 * struct Block __tmp_2 = ...; // setup details
178 * As the example demonstrates, the address of a stack variable is escaping the
179 * scope in which it is allocated. That is a classic C bug.
181 * Instead, the block literal must be copied to the heap with the Block_copy()
182 * function or by sending it a -[copy] message.
184 typedef void (^dispatch_block_t
)(void);
190 * @function dispatch_retain
193 * Increment the reference count of a dispatch object.
196 * Calls to dispatch_retain() must be balanced with calls to
197 * dispatch_release().
200 * The object to retain.
201 * The result of passing NULL in this parameter is undefined.
203 API_AVAILABLE(macos(10.6), ios(4.0))
204 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
205 DISPATCH_SWIFT_UNAVAILABLE("Can't be used with ARC")
207 dispatch_retain(dispatch_object_t object
);
208 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
209 #undef dispatch_retain
210 #define dispatch_retain(object) \
211 __extension__({ dispatch_object_t _o = (object); \
212 _dispatch_object_validate(_o); (void)[_o retain]; })
216 * @function dispatch_release
219 * Decrement the reference count of a dispatch object.
222 * A dispatch object is asynchronously deallocated once all references are
223 * released (i.e. the reference count becomes zero). The system does not
224 * guarantee that a given client is the last or only reference to a given
228 * The object to release.
229 * The result of passing NULL in this parameter is undefined.
231 API_AVAILABLE(macos(10.6), ios(4.0))
232 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
233 DISPATCH_SWIFT_UNAVAILABLE("Can't be used with ARC")
235 dispatch_release(dispatch_object_t object
);
236 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
237 #undef dispatch_release
238 #define dispatch_release(object) \
239 __extension__({ dispatch_object_t _o = (object); \
240 _dispatch_object_validate(_o); [_o release]; })
244 * @function dispatch_get_context
247 * Returns the application defined context of the object.
250 * The result of passing NULL in this parameter is undefined.
253 * The context of the object; may be NULL.
255 API_AVAILABLE(macos(10.6), ios(4.0))
256 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
259 dispatch_get_context(dispatch_object_t object
);
262 * @function dispatch_set_context
265 * Associates an application defined context with the object.
268 * The result of passing NULL in this parameter is undefined.
271 * The new client defined context for the object. This may be NULL.
274 API_AVAILABLE(macos(10.6), ios(4.0))
275 DISPATCH_EXPORT DISPATCH_NOTHROW
277 dispatch_set_context(dispatch_object_t object
, void *_Nullable context
);
280 * @function dispatch_set_finalizer_f
283 * Set the finalizer function for a dispatch object.
286 * The dispatch object to modify.
287 * The result of passing NULL in this parameter is undefined.
290 * The finalizer function pointer.
293 * A dispatch object's finalizer will be invoked on the object's target queue
294 * after all references to the object have been released. This finalizer may be
295 * used by the application to release any resources associated with the object,
296 * such as freeing the object's context.
297 * The context parameter passed to the finalizer function is the current
298 * context of the dispatch object at the time the finalizer call is made.
300 API_AVAILABLE(macos(10.6), ios(4.0))
301 DISPATCH_EXPORT DISPATCH_NOTHROW
303 dispatch_set_finalizer_f(dispatch_object_t object
,
304 dispatch_function_t _Nullable finalizer
);
307 * @function dispatch_activate
310 * Activates the specified dispatch object.
313 * Dispatch objects such as queues and sources may be created in an inactive
314 * state. Objects in this state have to be activated before any blocks
315 * associated with them will be invoked.
317 * The target queue of inactive objects can be changed using
318 * dispatch_set_target_queue(). Change of target queue is no longer permitted
319 * once an initially inactive object has been activated.
321 * Calling dispatch_activate() on an active object has no effect.
322 * Releasing the last reference count on an inactive object is undefined.
325 * The object to be activated.
326 * The result of passing NULL in this parameter is undefined.
328 API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
329 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
331 dispatch_activate(dispatch_object_t object
);
334 * @function dispatch_suspend
337 * Suspends the invocation of blocks on a dispatch object.
340 * A suspended object will not invoke any blocks associated with it. The
341 * suspension of an object will occur after any running block associated with
342 * the object completes.
344 * Calls to dispatch_suspend() must be balanced with calls
345 * to dispatch_resume().
348 * The object to be suspended.
349 * The result of passing NULL in this parameter is undefined.
351 API_AVAILABLE(macos(10.6), ios(4.0))
352 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
354 dispatch_suspend(dispatch_object_t object
);
357 * @function dispatch_resume
360 * Resumes the invocation of blocks on a dispatch object.
363 * Dispatch objects can be suspended with dispatch_suspend(), which increments
364 * an internal suspension count. dispatch_resume() is the inverse operation,
365 * and consumes suspension counts. When the last suspension count is consumed,
366 * blocks associated with the object will be invoked again.
368 * For backward compatibility reasons, dispatch_resume() on an inactive and not
369 * otherwise suspended dispatch source object has the same effect as calling
370 * dispatch_activate(). For new code, using dispatch_activate() is preferred.
372 * If the specified object has zero suspension count and is not an inactive
373 * source, this function will result in an assertion and the process being
377 * The object to be resumed.
378 * The result of passing NULL in this parameter is undefined.
380 API_AVAILABLE(macos(10.6), ios(4.0))
381 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
383 dispatch_resume(dispatch_object_t object
);
387 * @function dispatch_wait
390 * Wait synchronously for an object or until the specified timeout has elapsed.
393 * Type-generic macro that maps to dispatch_block_wait, dispatch_group_wait or
394 * dispatch_semaphore_wait, depending on the type of the first argument.
395 * See documentation for these functions for more details.
396 * This function is unavailable for any other object type.
399 * The object to wait on.
400 * The result of passing NULL in this parameter is undefined.
403 * When to timeout (see dispatch_time). As a convenience, there are the
404 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
407 * Returns zero on success or non-zero on error (i.e. timed out).
410 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
412 dispatch_wait(void *object
, dispatch_time_t timeout
);
413 #if __has_extension(c_generic_selections)
414 #define dispatch_wait(object, timeout) \
416 dispatch_block_t:dispatch_block_wait, \
417 dispatch_group_t:dispatch_group_wait, \
418 dispatch_semaphore_t:dispatch_semaphore_wait \
419 )((object),(timeout))
423 * @function dispatch_notify
426 * Schedule a notification block to be submitted to a queue when the execution
427 * of a specified object has completed.
430 * Type-generic macro that maps to dispatch_block_notify or
431 * dispatch_group_notify, depending on the type of the first argument.
432 * See documentation for these functions for more details.
433 * This function is unavailable for any other object type.
436 * The object to observe.
437 * The result of passing NULL in this parameter is undefined.
440 * The queue to which the supplied notification block will be submitted when
441 * the observed object completes.
443 * @param notification_block
444 * The block to submit when the observed object completes.
447 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
449 dispatch_notify(void *object
, dispatch_object_t queue
,
450 dispatch_block_t notification_block
);
451 #if __has_extension(c_generic_selections)
452 #define dispatch_notify(object, queue, notification_block) \
454 dispatch_block_t:dispatch_block_notify, \
455 dispatch_group_t:dispatch_group_notify \
456 )((object),(queue), (notification_block))
460 * @function dispatch_cancel
463 * Cancel the specified object.
466 * Type-generic macro that maps to dispatch_block_cancel or
467 * dispatch_source_cancel, depending on the type of the first argument.
468 * See documentation for these functions for more details.
469 * This function is unavailable for any other object type.
472 * The object to cancel.
473 * The result of passing NULL in this parameter is undefined.
476 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
478 dispatch_cancel(void *object
);
479 #if __has_extension(c_generic_selections)
480 #define dispatch_cancel(object) \
482 dispatch_block_t:dispatch_block_cancel, \
483 dispatch_source_t:dispatch_source_cancel \
488 * @function dispatch_testcancel
491 * Test whether the specified object has been canceled
494 * Type-generic macro that maps to dispatch_block_testcancel or
495 * dispatch_source_testcancel, depending on the type of the first argument.
496 * See documentation for these functions for more details.
497 * This function is unavailable for any other object type.
500 * The object to test.
501 * The result of passing NULL in this parameter is undefined.
504 * Non-zero if canceled and zero if not canceled.
507 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
510 dispatch_testcancel(void *object
);
511 #if __has_extension(c_generic_selections)
512 #define dispatch_testcancel(object) \
514 dispatch_block_t:dispatch_block_testcancel, \
515 dispatch_source_t:dispatch_source_testcancel \
521 * @function dispatch_debug
524 * Programmatically log debug information about a dispatch object.
527 * Programmatically log debug information about a dispatch object. By default,
528 * the log output is sent to syslog at notice level. In the debug version of
529 * the library, the log output is sent to a file in /var/tmp.
530 * The log output destination can be configured via the LIBDISPATCH_LOG
531 * environment variable, valid values are: YES, NO, syslog, stderr, file.
533 * This function is deprecated and will be removed in a future release.
534 * Objective-C callers may use -debugDescription instead.
537 * The object to introspect.
540 * The message to log above and beyond the introspection.
542 API_DEPRECATED("unsupported interface", macos(10.6,10.9), ios(4.0,6.0))
543 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
544 __attribute__((__format__(printf
,2,3)))
546 dispatch_debug(dispatch_object_t object
, const char *message
, ...);
548 API_DEPRECATED("unsupported interface", macos(10.6,10.9), ios(4.0,6.0))
549 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
550 __attribute__((__format__(printf
,2,0)))
552 dispatch_debugv(dispatch_object_t object
, const char *message
, va_list ap
);
556 DISPATCH_ASSUME_NONNULL_END