2 * Copyright (c) 2008-2009 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_GROUP__
22 #define __DISPATCH_GROUP__
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_group_t
32 * A group of blocks submitted to queues for asynchronous invocation.
34 DISPATCH_DECL(dispatch_group
);
39 * @function dispatch_group_create
42 * Creates new group with which blocks may be associated.
45 * This function creates a new group with which blocks may be associated.
46 * The dispatch group may be used to wait for the completion of the blocks it
47 * references. The group object memory is freed with dispatch_release().
50 * The newly created group, or NULL on failure.
52 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
55 dispatch_group_create(void);
58 * @function dispatch_group_async
61 * Submits a block to a dispatch queue and associates the block with the given
65 * Submits a block to a dispatch queue and associates the block with the given
66 * dispatch group. The dispatch group may be used to wait for the completion
67 * of the blocks it references.
70 * A dispatch group to associate with the submitted block.
71 * The result of passing NULL in this parameter is undefined.
74 * The dispatch queue to which the block will be submitted for asynchronous
78 * The block to perform asynchronously.
81 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
84 dispatch_group_async(dispatch_group_t group
,
85 dispatch_queue_t queue
,
86 dispatch_block_t block
);
87 #endif /* __BLOCKS__ */
90 * @function dispatch_group_async_f
93 * Submits a function to a dispatch queue and associates the block with the
94 * given dispatch group.
97 * See dispatch_group_async() for details.
100 * A dispatch group to associate with the submitted function.
101 * The result of passing NULL in this parameter is undefined.
104 * The dispatch queue to which the function will be submitted for asynchronous
108 * The application-defined context parameter to pass to the function.
111 * The application-defined function to invoke on the target queue. The first
112 * parameter passed to this function is the context provided to
113 * dispatch_group_async_f().
115 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
116 DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
118 dispatch_group_async_f(dispatch_group_t group
,
119 dispatch_queue_t queue
,
121 dispatch_function_t work
);
124 * @function dispatch_group_wait
127 * Wait synchronously for the previously submitted blocks to complete;
128 * returns if the blocks have not completed within the specified timeout.
131 * This function waits for the completion of the blocks associated with the
132 * given dispatch group, and returns after all blocks have completed or when
133 * the specified timeout has elapsed. When a timeout occurs, the group is
134 * restored to its original state.
136 * This function will return immediately if there are no blocks associated
137 * with the dispatch group (i.e. the group is empty).
139 * The result of calling this function from mulitple threads simultaneously
140 * with the same dispatch group is undefined.
142 * After the successful return of this function, the dispatch group is empty.
143 * It may either be released with dispatch_release() or re-used for additional
144 * blocks. See dispatch_group_async() for more information.
147 * The dispatch group to wait on.
148 * The result of passing NULL in this parameter is undefined.
151 * When to timeout (see dispatch_time). As a convenience, there are the
152 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
155 * Returns zero on success (all blocks associated with the group completed
156 * within the specified timeout) or non-zero on error (i.e. timed out).
158 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
161 dispatch_group_wait(dispatch_group_t group
, dispatch_time_t timeout
);
164 * @function dispatch_group_notify
167 * Schedule a block to be submitted to a queue when a group of previously
168 * submitted blocks have completed.
171 * This function schedules a notification block to be submitted to the specified
172 * queue once all blocks associated with the dispatch group have completed.
174 * If no blocks are associated with the dispatch group (i.e. the group is empty)
175 * then the notification block will be submitted immediately.
177 * The group will be empty at the time the notification block is submitted to
178 * the target queue. The group may either be released with dispatch_release()
179 * or reused for additional operations.
180 * See dispatch_group_async() for more information.
183 * The dispatch group to observe.
184 * The result of passing NULL in this parameter is undefined.
187 * The queue to which the supplied block will be submitted when the group
191 * The block to submit when the group completes.
194 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
197 dispatch_group_notify(dispatch_group_t group
,
198 dispatch_queue_t queue
,
199 dispatch_block_t block
);
200 #endif /* __BLOCKS__ */
203 * @function dispatch_group_notify_f
206 * Schedule a function to be submitted to a queue when a group of previously
207 * submitted functions have completed.
210 * See dispatch_group_notify() for details.
213 * The dispatch group to observe.
214 * The result of passing NULL in this parameter is undefined.
217 * The application-defined context parameter to pass to the function.
220 * The application-defined function to invoke on the target queue. The first
221 * parameter passed to this function is the context provided to
222 * dispatch_group_notify_f().
224 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
225 DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL4
227 dispatch_group_notify_f(dispatch_group_t group
,
228 dispatch_queue_t queue
,
230 dispatch_function_t work
);
233 * @function dispatch_group_enter
236 * Manually indicate a block has entered the group
239 * Calling this function indicates another block has joined the group through
240 * a means other than dispatch_group_async(). Calls to this function must be
241 * balanced with dispatch_group_leave().
244 * The dispatch group to update.
245 * The result of passing NULL in this parameter is undefined.
247 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
248 DISPATCH_NOTHROW DISPATCH_NONNULL_ALL
250 dispatch_group_enter(dispatch_group_t group
);
253 * @function dispatch_group_leave
256 * Manually indicate a block in the group has completed
259 * Calling this function indicates block has completed and left the dispatch
260 * groupJ by a means other than dispatch_group_async().
263 * The dispatch group to update.
264 * The result of passing NULL in this parameter is undefined.
266 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
267 DISPATCH_NOTHROW DISPATCH_NONNULL_ALL
269 dispatch_group_leave(dispatch_group_t group
);