]> git.saurik.com Git - apple/libdispatch.git/blame - man/dispatch_semaphore_create.3
libdispatch-703.30.5.tar.gz
[apple/libdispatch.git] / man / dispatch_semaphore_create.3
CommitLineData
517da941 1.\" Copyright (c) 2008-2012 Apple Inc. All rights reserved.
0ab74447
A
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
e85f4437
A
36parameter is equal to zero, then the semaphore is useful for synchronizing
37completion of work.
0ab74447
A
38For example:
39.Bd -literal -offset indent
40sema = dispatch_semaphore_create(0);
41
42dispatch_async(queue, ^{
43 foo();
44 dispatch_semaphore_signal(sema);
45});
46
47bar();
48
49dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
50.Ed
51.Sh FINITE RESOURCE POOL
52If the
53.Fa count
e85f4437
A
54parameter is greater than zero, then the semaphore is useful for managing a
55finite pool of resources.
0ab74447
A
56For example, a library that wants to limit Unix descriptor usage:
57.Bd -literal -offset indent
58sema = dispatch_semaphore_create(getdtablesize() / 4);
59.Ed
60.Pp
61At each Unix FD allocation:
62.Bd -literal -offset indent
63dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
64fd = open("/etc/services", O_RDONLY);
65.Ed
66.Pp
67When each FD is closed:
68.Bd -literal -offset indent
69close(fd);
70dispatch_semaphore_signal(sema);
71.Ed
72.Sh RETURN VALUES
73The
74.Fn dispatch_semaphore_create
75function returns NULL if no memory is available or if the
76.Fa count
77parameter is less than zero.
78.Pp
79The
80.Fn dispatch_semaphore_signal
81function returns non-zero when a thread is woken.
82Otherwise, zero is returned.
83.Pp
84The
85.Fn dispatch_semaphore_wait
e85f4437
A
86function returns zero upon success and non-zero after the timeout expires. If
87the timeout is DISPATCH_TIME_FOREVER, then
0ab74447
A
88.Fn dispatch_semaphore_wait
89waits forever and always returns zero.
90.Sh MEMORY MODEL
91Dispatch semaphores are retained and released via calls to
92.Fn dispatch_retain
93and
94.Fn dispatch_release .
95.Sh CAVEATS
e85f4437
A
96Unbalanced dispatch semaphores cannot be released.
97For a given semaphore, calls to
98.Fn dispatch_semaphore_signal
99and
100.Fn dispatch_semaphore_wait
101must be balanced before
102.Fn dispatch_release
103is called on it.
0ab74447 104.Sh SEE ALSO
e85f4437 105.Xr dispatch 3 ,
0ab74447 106.Xr dispatch_object 3