Deleted Added
full compact
barrier.c (178529) barrier.c (178546)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *

--- 24 unchanged lines hidden (view full) ---

33 * barrier_wait(), that thread blocks until n - 1 other threads reach the
34 * barrier_wait() call using the same barrier_t. When n threads have reached
35 * the barrier, they are all awakened and sent on their way. One of the threads
36 * returns from barrier_wait() with a return code of 1; the remaining threads
37 * get a return code of 0.
38 */
39
40#include <pthread.h>
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *

--- 24 unchanged lines hidden (view full) ---

33 * barrier_wait(), that thread blocks until n - 1 other threads reach the
34 * barrier_wait() call using the same barrier_t. When n threads have reached
35 * the barrier, they are all awakened and sent on their way. One of the threads
36 * returns from barrier_wait() with a return code of 1; the remaining threads
37 * get a return code of 0.
38 */
39
40#include <pthread.h>
41#if defined(sun)
41#include <synch.h>
42#include <synch.h>
43#endif
42#include <stdio.h>
43
44#include "barrier.h"
45
46void
47barrier_init(barrier_t *bar, int nthreads)
48{
49 pthread_mutex_init(&bar->bar_lock, NULL);
44#include <stdio.h>
45
46#include "barrier.h"
47
48void
49barrier_init(barrier_t *bar, int nthreads)
50{
51 pthread_mutex_init(&bar->bar_lock, NULL);
52#if defined(sun)
50 sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL);
53 sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL);
54#else
55 sem_init(&bar->bar_sem, 0, 0);
56#endif
51
52 bar->bar_numin = 0;
53 bar->bar_nthr = nthreads;
54}
55
56int
57barrier_wait(barrier_t *bar)
58{
59 pthread_mutex_lock(&bar->bar_lock);
60
61 if (++bar->bar_numin < bar->bar_nthr) {
62 pthread_mutex_unlock(&bar->bar_lock);
57
58 bar->bar_numin = 0;
59 bar->bar_nthr = nthreads;
60}
61
62int
63barrier_wait(barrier_t *bar)
64{
65 pthread_mutex_lock(&bar->bar_lock);
66
67 if (++bar->bar_numin < bar->bar_nthr) {
68 pthread_mutex_unlock(&bar->bar_lock);
69#if defined(sun)
63 sema_wait(&bar->bar_sem);
70 sema_wait(&bar->bar_sem);
71#else
72 sem_wait(&bar->bar_sem);
73#endif
64
65 return (0);
66
67 } else {
68 int i;
69
70 /* reset for next use */
71 bar->bar_numin = 0;
72 for (i = 1; i < bar->bar_nthr; i++)
74
75 return (0);
76
77 } else {
78 int i;
79
80 /* reset for next use */
81 bar->bar_numin = 0;
82 for (i = 1; i < bar->bar_nthr; i++)
83#if defined(sun)
73 sema_post(&bar->bar_sem);
84 sema_post(&bar->bar_sem);
85#else
86 sem_post(&bar->bar_sem);
87#endif
74 pthread_mutex_unlock(&bar->bar_lock);
75
76 return (1);
77 }
78}
88 pthread_mutex_unlock(&bar->bar_lock);
89
90 return (1);
91 }
92}