]> git.saurik.com Git - apple/libdispatch.git/blame - man/dispatch_semaphore_create.3
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / man / dispatch_semaphore_create.3
CommitLineData
0ab74447
A
1.\" Copyright (c) 2008-2009 Apple Inc. All rights reserved.
2.Dd May 1, 2009
3.Dt dispatch_semaphore_create 3
4.Os Darwin
5.Sh NAME
6.Nm dispatch_semaphore_create ,
7.Nm dispatch_semaphore_signal ,
8.Nm dispatch_semaphore_wait
9.Nd synchronized counting semaphore
10.Sh SYNOPSIS
11.Fd #include <dispatch/dispatch.h>
12.Ft dispatch_semaphore_t
13.Fo dispatch_semaphore_create
14.Fa "long count"
15.Fc
16.Ft long
17.Fo dispatch_semaphore_signal
18.Fa "dispatch_semaphore_t semaphore"
19.Fc
20.Ft long
21.Fo dispatch_semaphore_wait
22.Fa "dispatch_semaphore_t semaphore" "dispatch_time_t timeout"
23.Fc
24.Sh DESCRIPTION
25Dispatch semaphores are used to synchronize threads.
26The
27.Fa timeout
28parameter is creatable with the
29.Xr dispatch_time 3
30or
31.Xr dispatch_walltime 3
32functions.
33.Sh COMPLETION SYNCHRONIZATION
34If the
35.Fa count
36parameter is equal to zero, then the semaphore is useful for synchronizing completion of work.
37For example:
38.Bd -literal -offset indent
39sema = dispatch_semaphore_create(0);
40
41dispatch_async(queue, ^{
42 foo();
43 dispatch_semaphore_signal(sema);
44});
45
46bar();
47
48dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
49.Ed
50.Sh FINITE RESOURCE POOL
51If the
52.Fa count
53parameter is greater than zero, then the semaphore is useful for managing a finite pool of resources.
54For example, a library that wants to limit Unix descriptor usage:
55.Bd -literal -offset indent
56sema = dispatch_semaphore_create(getdtablesize() / 4);
57.Ed
58.Pp
59At each Unix FD allocation:
60.Bd -literal -offset indent
61dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
62fd = open("/etc/services", O_RDONLY);
63.Ed
64.Pp
65When each FD is closed:
66.Bd -literal -offset indent
67close(fd);
68dispatch_semaphore_signal(sema);
69.Ed
70.Sh RETURN VALUES
71The
72.Fn dispatch_semaphore_create
73function returns NULL if no memory is available or if the
74.Fa count
75parameter is less than zero.
76.Pp
77The
78.Fn dispatch_semaphore_signal
79function returns non-zero when a thread is woken.
80Otherwise, zero is returned.
81.Pp
82The
83.Fn dispatch_semaphore_wait
84function returns zero upon success and non-zero after the timeout expires. If the timeout is DISPATCH_TIME_FOREVER, then
85.Fn dispatch_semaphore_wait
86waits forever and always returns zero.
87.Sh MEMORY MODEL
88Dispatch semaphores are retained and released via calls to
89.Fn dispatch_retain
90and
91.Fn dispatch_release .
92.Sh CAVEATS
93Dispatch semaphores are strict counting semaphores.
94In other words, dispatch semaphores do not saturate at any particular value.
95Saturation can be achieved through atomic compare-and-swap logic.
96What follows is a saturating binary semaphore:
97.Bd -literal
98void
99saturating_semaphore_signal(dispatch_semaphore_t dsema, int *sent)
100{
101 if (__sync_bool_compare_and_swap(sent, 0, 1)) {
102 dispatch_semaphore_signal(dsema);
103 }
104}
105
106void
107saturating_semaphore_wait(dispatch_semaphore_t dsema, int *sent)
108{
109 *sent = 0;
110 dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
111}
112.Ed
113.Sh SEE ALSO
114.Xr dispatch_object 3