xref: /netbsd-src/lib/libpthread/pthread_barrier.3 (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1.\" $NetBSD: pthread_barrier.3,v 1.6 2016/07/05 10:04:17 wiz Exp $
2.\"
3.\" Copyright (c) 2002, 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\" POSSIBILITY OF SUCH DAMAGE.
26.\"
27.\" ----------------------------------------------------------------------------
28.Dd June 12, 2016
29.Dt PTHREAD_BARRIER 3
30.Os
31.Sh NAME
32.Nm pthread_barrier
33.Nd barrier interface
34.Sh LIBRARY
35.Lb libpthread
36.Sh SYNOPSIS
37.In pthread.h
38.Ft int
39.Fn pthread_barrier_init "pthread_barrier_t * restrict barrier" \
40"const pthread_barrierattr_t * restrict attr" "unsigned int count"
41.Ft int
42.Fn pthread_barrier_destroy "pthread_barrier_t *barrier"
43.Ft int
44.Fn pthread_barrier_wait "pthread_barrier_t *barrier"
45.Ft int
46.Fn pthread_barrierattr_getpshared "const pthread_barrierattr_t * __restrict attr" \
47"int * __restrict pshared"
48.Ft int
49.Fn pthread_barrierattr_setpshared "pthread_barrierattr_t * attr" \
50"int pshared"
51.\" ----------------------------------------------------------------------------
52.Sh DESCRIPTION
53The
54.Fn pthread_barrier_init
55function creates a new barrier with attributes
56.Fa attr
57and
58.Fa count .
59The
60.Fa count
61parameter indicates the number of threads
62which will participate in the barrier.
63The
64.Xr pthread_barrierattr_init 3
65function may be used to specify the attributes supplied in
66.Fa attr .
67If
68.Fa attr
69is
70.Dv NULL ,
71the default attributes are used.
72Barriers are most commonly used in the decomposition of parallel loops.
73.Pp
74.\" -----
75The
76.Fn pthread_barrier_destroy
77function causes the resources allocated to
78.Fa barrier
79to be released.
80No threads should be blocked on
81.Fa barrier .
82.Pp
83.\" -----
84The
85.Fn pthread_barrier_wait
86function causes the current thread to wait on the barrier specified.
87Once as many threads as specified by the
88.Fa count
89parameter to the corresponding
90.Fn pthread_barrier_init
91call have called
92.Fn pthread_barrier_wait ,
93all threads will wake up, return from their respective
94.Fn pthread_barrier_wait
95calls and continue execution.
96.Pp
97.\" -----
98The
99.Fn pthread_barrierattr_getpshared
100function shall obtain the value of the process-shared attribute from the
101attributes object referenced by attr.
102The
103.Fn pthread_barrierattr_setpshared
104function shall set the process-shared attribute in an initialized attributes
105object referenced by attr.
106.\" ----------------------------------------------------------------------------
107.Sh RETURN VALUES
108If successful,
109.Fn pthread_barrier_init
110will return zero and put the new barrier id into
111.Fa barrier ,
112otherwise an error number will be returned to indicate the error.
113.Pp
114.\" -----
115If successful,
116.Fn pthread_barrier_destroy
117will return zero.
118Otherwise an error value will be returned.
119.Pp
120.\" -----
121If successful,
122.Fn pthread_barrier_wait
123will return zero for all waiting threads except for one.
124One thread will receive status
125.Dv PTHREAD_BARRIER_SERIAL_THREAD ,
126which is intended to indicate that this thread may be used to update
127shared data.
128It is the responsibility of this thread to insure the visibility
129and atomicity of any updates to shared data with respect to the
130other threads participating in the barrier.
131In the case of failure, an error value will be returned.
132.Pp
133.\" -----
134If successful,
135.Fn pthread_barrierattr_getpshared
136shall return zero and store the value of the process-shared attribute of attr
137into the object referenced by the
138.Fa pshared
139parameter.
140Otherwise, an error number shall be returned to indicate the error.
141.Pp
142.\" -----
143If successful,
144.Fn pthread_barrierattr_setpshared
145shall return zero;
146Otherwise, an error number shall be returned to indicate the error.
147.\" ----------------------------------------------------------------------------
148.Sh ERRORS
149The
150.Fn pthread_barrier_init
151function may fail if:
152.Bl -tag -width Er
153.It Bq Er EINVAL
154The value specified by
155.Fa count
156is zero or
157.Fa attr
158is invalid.
159.El
160.Pp
161.\" -----
162The
163.Fn pthread_barrier_destroy
164function may fail if:
165.Bl -tag -width Er
166.It Bq Er EBUSY
167The
168.Fa barrier
169still has active threads associated with it.
170.It Bq Er EINVAL
171The value specified by
172.Fa barrier
173is invalid.
174.El
175.Pp
176.\" -----
177The
178.Fn pthread_barrier_wait
179function may fail if:
180.Bl -tag -width Er
181.It Bq Er EINVAL
182The value specified by
183.Fa barrier
184is invalid.
185.El
186.Pp
187.\" -----
188The
189.Fn pthread_barrierattr_setpshared
190function and
191the
192.Fn pthread_barrierattr_getpshared
193function may fail if:
194.Bl -tag -width Er
195.It Bq Er EINVAL
196The value specified by
197.Fa attr
198is invalid.
199.El
200.\" ---------------------------------------------------------------------------
201.Sh SEE ALSO
202.Xr pthread_barrierattr 3 ,
203.Xr pthread_cond 3 ,
204.Xr pthread_mutex 3
205.Sh STANDARDS
206These functions conform to
207.St -p1003.1-2001 .
208.Sh BUGS
209The
210.Fn pthread_barrierattr_getpshared
211and
212.Fn pthread_barrierattr_setpshared
213functions are hidden by default since only thread shared attributes
214are supported.
215