summaryrefslogtreecommitdiff
path: root/linuxthreads/man/sem_init.man
blob: e3a1a63e3691594643f544ffd690de9a351e81c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
.TH SEMAPHORES 3 LinuxThreads

.XREF sem_wait
.XREF sem_trywait
.XREF sem_post
.XREF sem_getvalue
.XREF sem_destroy

.SH NAME
sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, sem_destroy \- operations on semaphores

.SH SYNOPSIS
#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

int sem_wait(sem_t * sem);

int sem_trywait(sem_t * sem);

int sem_post(sem_t * sem);

int sem_getvalue(sem_t * sem, int * sval);

int sem_destroy(sem_t * sem);

.SH DESCRIPTION
This manual page documents POSIX 1003.1b semaphores, not to be
confused with SystemV semaphores as described in !ipc!(5), !semctl!(2)
and !semop!(2).

Semaphores are counters for resources shared between threads. The
basic operations on semaphores are: increment the counter atomically,
and wait until the counter is non-null and decrement it atomically.

!sem_init! initializes the semaphore object pointed to by |sem|. The
count associated with the semaphore is set initially to |value|. The
|pshared| argument indicates whether the semaphore is local to the
current process (|pshared| is zero) or is to be shared between several
processes (|pshared| is not zero). LinuxThreads currently does not
support process-shared semaphores, thus !sem_init! always returns with
error !ENOSYS! if |pshared| is not zero.

!sem_wait! suspends the calling thread until the semaphore pointed to
by |sem| has non-zero count. It then atomically decreases the
semaphore count.

!sem_trywait! is a non-blocking variant of !sem_wait!. If the
semaphore pointed to by |sem| has non-zero count, the count is
atomically decreased and !sem_trywait! immediately returns 0.
If the semaphore count is zero, !sem_trywait! immediately returns with
error !EAGAIN!.

!sem_post! atomically increases the count of the semaphore pointed to
by |sem|. This function never blocks and can safely be used in
asynchronous signal handlers.

!sem_getvalue! stores in the location pointed to by |sval| the current
count of the semaphore |sem|.

!sem_destroy! destroys a semaphore object, freeing the resources it
might hold. No threads should be waiting on the semaphore at the time
!sem_destroy! is called. In the LinuxThreads implementation, no
resources are associated with semaphore objects, thus !sem_destroy!
actually does nothing except checking that no thread is waiting on the
semaphore.

.SH CANCELLATION

!sem_wait! is a cancellation point.

.SH "ASYNC-SIGNAL SAFETY"

On processors supporting atomic compare-and-swap (Intel 486, Pentium
and later, Alpha, PowerPC, MIPS II, Motorola 68k), the !sem_post!
function is async-signal safe and can therefore be
called from signal handlers. This is the only thread synchronization
function provided by POSIX threads that is async-signal safe.

On the Intel 386 and the Sparc, the current LinuxThreads
implementation of !sem_post! is not async-signal safe by lack of the
required atomic operations.

.SH "RETURN VALUE"

The !sem_wait! and !sem_getvalue! functions always return 0.
All other semaphore functions return 0 on success and -1 on error, in
addition to writing an error code in !errno!.

.SH ERRORS

The !sem_init! function sets !errno! to the following codes on error:
.RS
.TP
!EINVAL!
|value| exceeds the maximal counter value !SEM_VALUE_MAX!
.TP
!ENOSYS!
|pshared| is not zero
.RE

The !sem_trywait! function sets !errno! to the following error code on error:
.RS
.TP
!EAGAIN!
the semaphore count is currently 0
.RE

The !sem_post! function sets !errno! to the following error code on error:
.RS
.TP
!ERANGE!
after incrementation, the semaphore value would exceed !SEM_VALUE_MAX!
(the semaphore count is left unchanged in this case)
.RE

The !sem_destroy! function sets !errno! to the following error code on error:
.RS
.TP
!EBUSY!
some threads are currently blocked waiting on the semaphore.
.RE

.SH AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr>

.SH "SEE ALSO"
!pthread_mutex_init!(3),
!pthread_cond_init!(3),
!pthread_cancel!(3),
!ipc!(5).