]> git.saurik.com Git - apple/libdispatch.git/blob - dispatch/semaphore.h
libdispatch-339.1.9.tar.gz
[apple/libdispatch.git] / dispatch / semaphore.h
1 /*
2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #ifndef __DISPATCH_SEMAPHORE__
22 #define __DISPATCH_SEMAPHORE__
23
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
27 #endif
28
29 /*!
30 * @typedef dispatch_semaphore_t
31 *
32 * @abstract
33 * A counting semaphore.
34 */
35 DISPATCH_DECL(dispatch_semaphore);
36
37 __BEGIN_DECLS
38
39 /*!
40 * @function dispatch_semaphore_create
41 *
42 * @abstract
43 * Creates new counting semaphore with an initial value.
44 *
45 * @discussion
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
49 * to the value.
50 *
51 * @param value
52 * The starting value for the semaphore. Passing a value less than zero will
53 * cause NULL to be returned.
54 *
55 * @result
56 * The newly created semaphore, or NULL on failure.
57 */
58 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
59 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
60 DISPATCH_NOTHROW
61 dispatch_semaphore_t
62 dispatch_semaphore_create(long value);
63
64 /*!
65 * @function dispatch_semaphore_wait
66 *
67 * @abstract
68 * Wait (decrement) for a semaphore.
69 *
70 * @discussion
71 * Decrement the counting semaphore. If the resulting value is less than zero,
72 * this function waits for a signal to occur before returning.
73 *
74 * @param dsema
75 * The semaphore. The result of passing NULL in this parameter is undefined.
76 *
77 * @param timeout
78 * When to timeout (see dispatch_time). As a convenience, there are the
79 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
80 *
81 * @result
82 * Returns zero on success, or non-zero if the timeout occurred.
83 */
84 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
85 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
86 long
87 dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);
88
89 /*!
90 * @function dispatch_semaphore_signal
91 *
92 * @abstract
93 * Signal (increment) a semaphore.
94 *
95 * @discussion
96 * Increment the counting semaphore. If the previous value was less than zero,
97 * this function wakes a waiting thread before returning.
98 *
99 * @param dsema The counting semaphore.
100 * The result of passing NULL in this parameter is undefined.
101 *
102 * @result
103 * This function returns non-zero if a thread is woken. Otherwise, zero is
104 * returned.
105 */
106 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
107 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
108 long
109 dispatch_semaphore_signal(dispatch_semaphore_t dsema);
110
111 __END_DECLS
112
113 #endif /* __DISPATCH_SEMAPHORE__ */