1a864dc36Sdarran /*
2a864dc36Sdarran * CDDL HEADER START
3a864dc36Sdarran *
4a864dc36Sdarran * The contents of this file are subject to the terms of the
5a864dc36Sdarran * Common Development and Distribution License, Version 1.0 only
6a864dc36Sdarran * (the "License"). You may not use this file except in compliance
7a864dc36Sdarran * with the License.
8a864dc36Sdarran *
9a864dc36Sdarran * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10a864dc36Sdarran * or http://www.opensolaris.org/os/licensing.
11a864dc36Sdarran * See the License for the specific language governing permissions
12a864dc36Sdarran * and limitations under the License.
13a864dc36Sdarran *
14a864dc36Sdarran * When distributing Covered Code, include this CDDL HEADER in each
15a864dc36Sdarran * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16a864dc36Sdarran * If applicable, add the following below this CDDL HEADER, with the
17a864dc36Sdarran * fields enclosed by brackets "[]" replaced with your own identifying
18a864dc36Sdarran * information: Portions Copyright [yyyy] [name of copyright owner]
19a864dc36Sdarran *
20a864dc36Sdarran * CDDL HEADER END
21a864dc36Sdarran */
22ebc5b22cSskrll
23ebc5b22cSskrll #ifdef HAVE_NBTOOL_CONFIG_H
24ebc5b22cSskrll #include "nbtool_config.h"
25ebc5b22cSskrll #endif
26ebc5b22cSskrll
27a864dc36Sdarran /*
28a864dc36Sdarran * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
29a864dc36Sdarran * Use is subject to license terms.
30a864dc36Sdarran */
31a864dc36Sdarran
32a864dc36Sdarran #pragma ident "%Z%%M% %I% %E% SMI"
33a864dc36Sdarran
34a864dc36Sdarran /*
35a864dc36Sdarran * This file implements a barrier, a synchronization primitive designed to allow
36a864dc36Sdarran * threads to wait for each other at given points. Barriers are initialized
37a864dc36Sdarran * with a given number of threads, n, using barrier_init(). When a thread calls
38a864dc36Sdarran * barrier_wait(), that thread blocks until n - 1 other threads reach the
39a864dc36Sdarran * barrier_wait() call using the same barrier_t. When n threads have reached
40a864dc36Sdarran * the barrier, they are all awakened and sent on their way. One of the threads
41a864dc36Sdarran * returns from barrier_wait() with a return code of 1; the remaining threads
42a864dc36Sdarran * get a return code of 0.
43a864dc36Sdarran */
44a864dc36Sdarran
45badca804Slukem #include <errno.h>
46a864dc36Sdarran #include <pthread.h>
47ba2539a9Schs #ifdef illumos
48a864dc36Sdarran #include <synch.h>
49bb8023b5Sdarran #endif
50a864dc36Sdarran #include <stdio.h>
51a864dc36Sdarran
52a864dc36Sdarran #include "barrier.h"
53badca804Slukem #include "ctftools.h"
54a864dc36Sdarran
55a864dc36Sdarran void
barrier_init(barrier_t * bar,int nthreads)56a864dc36Sdarran barrier_init(barrier_t *bar, int nthreads)
57a864dc36Sdarran {
58badca804Slukem if ((errno = pthread_mutex_init(&bar->bar_lock, NULL)) != 0)
59badca804Slukem terminate("%s: pthread_mutex_init(bar_lock)", __func__);
60ba2539a9Schs #ifdef illumos
61badca804Slukem if ((errno = sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL)) != 0)
62badca804Slukem terminate("%s: sema_init(bar_sem)", __func__);
636eddcd88Slukem #elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
646eddcd88Slukem if ((bar->bar_sem = dispatch_semaphore_create(0)) == NULL)
656eddcd88Slukem terminate("%s: dispatch_semaphore_create()\n", __func__);
66ba2539a9Schs #else
67badca804Slukem if (sem_init(&bar->bar_sem, 0, 0) == -1)
68badca804Slukem terminate("%s: sem_init(bar_sem)", __func__);
69ba2539a9Schs #endif
70ba2539a9Schs
71a864dc36Sdarran bar->bar_numin = 0;
72a864dc36Sdarran bar->bar_nthr = nthreads;
73a864dc36Sdarran }
74a864dc36Sdarran
75a864dc36Sdarran int
barrier_wait(barrier_t * bar)76a864dc36Sdarran barrier_wait(barrier_t *bar)
77a864dc36Sdarran {
786eddcd88Slukem #if defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
796eddcd88Slukem long error;
806eddcd88Slukem #endif
81badca804Slukem if ((errno = pthread_mutex_lock(&bar->bar_lock)) != 0)
82badca804Slukem terminate("%s: pthread_mutex_lock(bar_lock)", __func__);
83a864dc36Sdarran
84a864dc36Sdarran if (++bar->bar_numin < bar->bar_nthr) {
85badca804Slukem if ((errno = pthread_mutex_unlock(&bar->bar_lock)) != 0)
86badca804Slukem terminate("%s: pthread_mutex_unlock(bar_lock)",
87badca804Slukem __func__);
88ba2539a9Schs #ifdef illumos
89badca804Slukem if ((errno = sema_wait(&bar->bar_sem)) != 0)
90badca804Slukem terminate("%s: sema_wait(bar_sem)", __func__);
916eddcd88Slukem #elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
926eddcd88Slukem if ((error = dispatch_semaphore_wait(bar->bar_sem, DISPATCH_TIME_FOREVER)) != 0)
936eddcd88Slukem terminate("%s: dispatch_semaphore_wait(bar_sem) = %ld\n",
946eddcd88Slukem __func__, error);
95ba2539a9Schs #else
96badca804Slukem if (sem_wait(&bar->bar_sem) == -1)
97badca804Slukem terminate("%s: sem_wait(bar_sem)", __func__);
98ba2539a9Schs #endif
99ba2539a9Schs
100a864dc36Sdarran return (0);
101a864dc36Sdarran
102a864dc36Sdarran } else {
103a864dc36Sdarran int i;
104a864dc36Sdarran
105a864dc36Sdarran /* reset for next use */
106a864dc36Sdarran bar->bar_numin = 0;
1076eddcd88Slukem for (i = 1; i < bar->bar_nthr; i++) {
108ba2539a9Schs #ifdef illumos
109badca804Slukem if ((errno = sema_post(&bar->bar_sem)) != 0)
110badca804Slukem terminate("%s: sema_post(bar_sem)", __func__);
1116eddcd88Slukem #elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE)
112*f64554c8Slukem /* return value doesn't matter */
113*f64554c8Slukem dispatch_semaphore_signal(bar->bar_sem);
114ba2539a9Schs #else
115badca804Slukem if (sem_post(&bar->bar_sem) == -1)
116badca804Slukem terminate("%s: sem_post(bar_sem)", __func__);
117ba2539a9Schs #endif
1186eddcd88Slukem }
119badca804Slukem if ((errno = pthread_mutex_unlock(&bar->bar_lock)) != 0)
120badca804Slukem terminate("%s: pthread_mutex_unlock(bar_lock)",
121badca804Slukem __func__);
122a864dc36Sdarran
123a864dc36Sdarran return (1);
124a864dc36Sdarran }
125a864dc36Sdarran }
126