xref: /freebsd/lib/libthr/thread/thr_barrier.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*-
2  * Copyright (c) 2004 Michael Telahun Makonnen <mtm@FreeBSD.Org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <pthread.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "thr_private.h"
34 
35 __weak_reference(_pthread_barrier_destroy, pthread_barrier_destroy);
36 __weak_reference(_pthread_barrier_init, pthread_barrier_init);
37 __weak_reference(_pthread_barrier_wait, pthread_barrier_wait);
38 
39 int
40 _pthread_barrier_destroy(pthread_barrier_t *barrier)
41 {
42 	if (*barrier == NULL)
43 		return (EINVAL);
44 	if ((*barrier)->b_subtotal > 0)
45 		return (EBUSY);
46 	PTHREAD_ASSERT((*barrier)->b_subtotal == 0,
47 	    "barrier count must be zero when destroyed");
48 	free(*barrier);
49 	*barrier = NULL;
50 	return (0);
51 }
52 
53 int
54 _pthread_barrier_init(pthread_barrier_t *barrier,
55     const pthread_barrierattr_t attr, unsigned int count)
56 {
57 	if (count < 1)
58 		return (EINVAL);
59 	*barrier =
60 	    (struct pthread_barrier *)malloc(sizeof(struct pthread_barrier));
61 	if (*barrier == NULL)
62 		return (ENOMEM);
63 	memset((void *)*barrier, 0, sizeof(struct pthread_barrier));
64 	(*barrier)->b_total = count;
65 	TAILQ_INIT(&(*barrier)->b_barrq);
66 	return (0);
67 }
68 
69 int
70 _pthread_barrier_wait(pthread_barrier_t *barrier)
71 {
72 	struct pthread_barrier *b;
73 	struct pthread	       *ptd;
74 	int error;
75 
76 	if (*barrier == NULL)
77 		return (EINVAL);
78 
79 	/*
80 	 * Check if threads waiting on the barrier can be released. If
81 	 * so, release them and make this last thread the special thread.
82 	 */
83 	error = 0;
84 	b = *barrier;
85 	UMTX_LOCK(&b->b_lock);
86 	if (b->b_subtotal == (b->b_total - 1)) {
87 		TAILQ_FOREACH(ptd, &b->b_barrq, sqe) {
88 			_thread_critical_enter(ptd);
89 			PTHREAD_NEW_STATE(ptd, PS_RUNNING);
90 			TAILQ_REMOVE(&b->b_barrq, ptd, sqe);
91 			ptd->flags |= PTHREAD_FLAGS_BARR_REL;
92 			_thread_critical_exit(ptd);
93 		}
94 		b->b_subtotal = 0;
95 		UMTX_UNLOCK(&b->b_lock);
96 		return (PTHREAD_BARRIER_SERIAL_THREAD);
97 	}
98 
99 	/*
100 	 * More threads need to reach the barrier. Suspend this thread.
101 	 */
102 	_thread_critical_enter(curthread);
103 	TAILQ_INSERT_HEAD(&b->b_barrq, curthread, sqe);
104 	PTHREAD_NEW_STATE(curthread, PS_BARRIER_WAIT);
105 	_thread_critical_exit(curthread);
106 	b->b_subtotal++;
107 	PTHREAD_ASSERT(b->b_subtotal < b->b_total,
108 	    "the number of threads waiting at a barrier is too large");
109 	UMTX_UNLOCK(&b->b_lock);
110 	do {
111 		error = _thread_suspend(curthread, NULL);
112 		if (error == EINTR) {
113 			/*
114 			 * Make sure this thread wasn't released from
115 			 * the barrier while it was handling the signal.
116 			 */
117 			_thread_critical_enter(curthread);
118 			if ((curthread->flags & PTHREAD_FLAGS_BARR_REL) != 0) {
119 				curthread->flags &= ~PTHREAD_FLAGS_BARR_REL;
120 				_thread_critical_exit(curthread);
121 				error = 0;
122 				break;
123 			}
124 			PTHREAD_NEW_STATE(curthread, PS_BARRIER_WAIT);
125 			_thread_critical_exit(curthread);
126 		}
127 	} while (error == EINTR);
128 	return (error);
129 }
130