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_source_attr_s
*_dsa
;
85 struct dispatch_semaphore_s
*_dsema
;
86 struct dispatch_data_s
*_ddata
;
87 struct dispatch_io_s
*_dchannel
;
88 struct dispatch_operation_s
*_doperation
;
89 struct dispatch_disk_s
*_ddisk
;
90 } dispatch_object_t
__attribute__((__transparent_union__
));
92 #define DISPATCH_DECL(name) typedef struct name##_s *name##_t
94 #define DISPATCH_GLOBAL_OBJECT(t, x) (&(x))
96 #define DISPATCH_RETURNS_RETAINED
102 * @function dispatch_debug
105 * Programmatically log debug information about a dispatch object.
108 * Programmatically log debug information about a dispatch object. By default,
109 * the log output is sent to syslog at notice level. In the debug version of
110 * the library, the log output is sent to a file in /var/tmp.
111 * The log output destination can be configured via the LIBDISPATCH_LOG
112 * environment variable, valid values are: YES, NO, syslog, stderr, file.
115 * The object to introspect.
118 * The message to log above and beyond the introspection.
120 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
121 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
122 __attribute__((__format__(printf
,2,3)))
124 dispatch_debug(dispatch_object_t object
, const char *message
, ...);
126 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
127 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
128 __attribute__((__format__(printf
,2,0)))
130 dispatch_debugv(dispatch_object_t object
, const char *message
, va_list ap
);
133 * @function dispatch_retain
136 * Increment the reference count of a dispatch object.
139 * Calls to dispatch_retain() must be balanced with calls to
140 * dispatch_release().
143 * The object to retain.
144 * The result of passing NULL in this parameter is undefined.
146 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
147 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
149 dispatch_retain(dispatch_object_t object
);
150 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
151 #undef dispatch_retain
152 #define dispatch_retain(object) ({ dispatch_object_t _o = (object); \
153 _dispatch_object_validate(_o); (void)[_o retain]; })
157 * @function dispatch_release
160 * Decrement the reference count of a dispatch object.
163 * A dispatch object is asynchronously deallocated once all references are
164 * released (i.e. the reference count becomes zero). The system does not
165 * guarantee that a given client is the last or only reference to a given
169 * The object to release.
170 * The result of passing NULL in this parameter is undefined.
172 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
173 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
175 dispatch_release(dispatch_object_t object
);
176 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
177 #undef dispatch_release
178 #define dispatch_release(object) ({ dispatch_object_t _o = (object); \
179 _dispatch_object_validate(_o); [_o release]; })
183 * @function dispatch_get_context
186 * Returns the application defined context of the object.
189 * The result of passing NULL in this parameter is undefined.
192 * The context of the object; may be NULL.
194 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
195 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
198 dispatch_get_context(dispatch_object_t object
);
201 * @function dispatch_set_context
204 * Associates an application defined context with the object.
207 * The result of passing NULL in this parameter is undefined.
210 * The new client defined context for the object. This may be NULL.
213 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
214 DISPATCH_EXPORT DISPATCH_NOTHROW
//DISPATCH_NONNULL1
216 dispatch_set_context(dispatch_object_t object
, void *context
);
219 * @function dispatch_set_finalizer_f
222 * Set the finalizer function for a dispatch object.
225 * The dispatch object to modify.
226 * The result of passing NULL in this parameter is undefined.
229 * The finalizer function pointer.
232 * A dispatch object's finalizer will be invoked on the object's target queue
233 * after all references to the object have been released. This finalizer may be
234 * used by the application to release any resources associated with the object,
235 * such as freeing the object's context.
236 * The context parameter passed to the finalizer function is the current
237 * context of the dispatch object at the time the finalizer call is made.
239 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
240 DISPATCH_EXPORT DISPATCH_NOTHROW
//DISPATCH_NONNULL1
242 dispatch_set_finalizer_f(dispatch_object_t object
,
243 dispatch_function_t finalizer
);
246 * @function dispatch_suspend
249 * Suspends the invocation of blocks on a dispatch object.
252 * A suspended object will not invoke any blocks associated with it. The
253 * suspension of an object will occur after any running block associated with
254 * the object completes.
256 * Calls to dispatch_suspend() must be balanced with calls
257 * to dispatch_resume().
260 * The object to be suspended.
261 * The result of passing NULL in this parameter is undefined.
263 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
264 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
266 dispatch_suspend(dispatch_object_t object
);
269 * @function dispatch_resume
272 * Resumes the invocation of blocks on a dispatch object.
275 * The object to be resumed.
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_resume(dispatch_object_t object
);