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_SEMAPHORE__
22 #define __DISPATCH_SEMAPHORE__
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_semaphore_t
33 * A counting semaphore.
35 DISPATCH_DECL(dispatch_semaphore
);
40 * @function dispatch_semaphore_create
43 * Creates new counting semaphore with an initial value.
46 * Passing zero for the value is useful for when two threads need to reconcile
47 * the completion of a particular event. Passing a value greather than zero is
48 * useful for managing a finite pool of resources, where the pool size is equal
52 * The starting value for the semaphore. Passing a value less than zero will
53 * cause NULL to be returned.
56 * The newly created semaphore, or NULL on failure.
58 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
59 DISPATCH_MALLOC DISPATCH_NOTHROW
61 dispatch_semaphore_create(long value
);
64 * @function dispatch_semaphore_wait
67 * Wait (decrement) for a semaphore.
70 * Decrement the counting semaphore. If the resulting value is less than zero,
71 * this function waits in FIFO order for a signal to occur before returning.
74 * The semaphore. The result of passing NULL in this parameter is undefined.
77 * When to timeout (see dispatch_time). As a convenience, there are the
78 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
81 * Returns zero on success, or non-zero if the timeout occurred.
83 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
84 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
86 dispatch_semaphore_wait(dispatch_semaphore_t dsema
, dispatch_time_t timeout
);
89 * @function dispatch_semaphore_signal
92 * Signal (increment) a semaphore.
95 * Increment the counting semaphore. If the previous value was less than zero,
96 * this function wakes a waiting thread before returning.
98 * @param dsema The counting semaphore.
99 * The result of passing NULL in this parameter is undefined.
102 * This function returns non-zero if a thread is woken. Otherwise, zero is
105 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
106 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
108 dispatch_semaphore_signal(dispatch_semaphore_t dsema
);
112 #endif /* __DISPATCH_SEMAPHORE__ */