]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libkse/thread/thr_barrier.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libkse / thread / thr_barrier.c
1 /*-
2  * Copyright (c) 2003 David Xu <davidxu@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 <errno.h>
30 #include <stdlib.h>
31 #include "namespace.h"
32 #include <pthread.h>
33 #include "un-namespace.h"
34 #include "thr_private.h"
35
36 LT10_COMPAT_PRIVATE(_pthread_barrier_init);
37 LT10_COMPAT_DEFAULT(pthread_barrier_init);
38 LT10_COMPAT_PRIVATE(_pthread_barrier_wait);
39 LT10_COMPAT_DEFAULT(pthread_barrier_wait);
40 LT10_COMPAT_PRIVATE(_pthread_barrier_destroy);
41 LT10_COMPAT_DEFAULT(pthread_barrier_destroy);
42
43 __weak_reference(_pthread_barrier_init,         pthread_barrier_init);
44 __weak_reference(_pthread_barrier_wait,         pthread_barrier_wait);
45 __weak_reference(_pthread_barrier_destroy,      pthread_barrier_destroy);
46
47 int
48 _pthread_barrier_destroy(pthread_barrier_t *barrier)
49 {
50         pthread_barrier_t       bar;
51         int                     ret, ret2;
52
53         if (barrier == NULL || *barrier == NULL)
54                 return (EINVAL);
55
56         bar = *barrier;
57         if (bar->b_waiters > 0)
58                 return (EBUSY);
59         *barrier = NULL;
60         ret  = _pthread_mutex_destroy(&bar->b_lock);
61         ret2 = _pthread_cond_destroy(&bar->b_cond);
62         free(bar);
63         return (ret ? ret : ret2);
64 }
65
66 int
67 _pthread_barrier_init(pthread_barrier_t *barrier,
68                       const pthread_barrierattr_t *attr, unsigned count)
69 {
70         pthread_barrier_t       bar;
71         int                     ret;
72
73         if (barrier == NULL || count <= 0)
74                 return (EINVAL);
75
76         bar = malloc(sizeof(struct pthread_barrier));
77         if (bar == NULL)
78                 return (ENOMEM);
79
80         if ((ret = _pthread_mutex_init(&bar->b_lock, NULL)) != 0) {
81                 free(bar);
82                 return (ret);
83         }
84
85         if ((ret = _pthread_cond_init(&bar->b_cond, NULL)) != 0) {
86                 _pthread_mutex_destroy(&bar->b_lock);
87                 free(bar);
88                 return (ret);
89         }
90
91         bar->b_waiters          = 0;
92         bar->b_count            = count;
93         bar->b_generation       = 0;
94         *barrier                = bar;
95
96         return (0);
97 }
98
99 int
100 _pthread_barrier_wait(pthread_barrier_t *barrier)
101 {
102         int ret, gen;
103         pthread_barrier_t bar;
104
105         if (barrier == NULL || *barrier == NULL)
106                 return (EINVAL);
107
108         bar = *barrier;
109         if ((ret = _pthread_mutex_lock(&bar->b_lock)) != 0)
110                 return (ret);
111
112         if (++bar->b_waiters == bar->b_count) {
113                 /* Current thread is lastest thread */
114                 bar->b_generation++;
115                 bar->b_waiters = 0;
116                 ret = _pthread_cond_broadcast(&bar->b_cond);
117                 if (ret == 0)
118                         ret = PTHREAD_BARRIER_SERIAL_THREAD;
119         } else {
120                 gen = bar->b_generation;
121                 do {
122                         ret = _pthread_cond_wait(
123                                 &bar->b_cond, &bar->b_lock);
124                 /* test generation to avoid bogus wakeup */
125                 } while (ret == 0 && gen == bar->b_generation);
126         }
127         _pthread_mutex_unlock(&bar->b_lock);
128         return (ret);
129 }