2 * Copyright 2011-2015 Samy Al Bahra.
3 * Copyright 2011 David Joseph.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <ck_barrier.h>
31 #include <ck_stdbool.h>
34 ck_barrier_mcs_init(struct ck_barrier_mcs *barrier, unsigned int nthr)
38 ck_pr_store_uint(&barrier->tid, 0);
40 for (i = 0; i < nthr; ++i) {
41 for (j = 0; j < 4; ++j) {
43 * If there are still threads that don't have parents,
46 barrier[i].havechild[j] = ((i << 2) + j < nthr - 1) ? ~0 : 0;
49 * childnotready is initialized to havechild to ensure
50 * a thread does not wait for a child that does not exist.
52 barrier[i].childnotready[j] = barrier[i].havechild[j];
55 /* The root thread does not have a parent. */
56 barrier[i].parent = (i == 0) ?
58 &barrier[(i - 1) >> 2].childnotready[(i - 1) & 3];
60 /* Leaf threads do not have any children. */
61 barrier[i].children[0] = ((i << 1) + 1 >= nthr) ?
63 &barrier[(i << 1) + 1].parentsense;
65 barrier[i].children[1] = ((i << 1) + 2 >= nthr) ?
67 &barrier[(i << 1) + 2].parentsense;
69 barrier[i].parentsense = 0;
76 ck_barrier_mcs_subscribe(struct ck_barrier_mcs *barrier, struct ck_barrier_mcs_state *state)
80 state->vpid = ck_pr_faa_uint(&barrier->tid, 1);
84 CK_CC_INLINE static bool
85 ck_barrier_mcs_check_children(unsigned int *childnotready)
88 if (ck_pr_load_uint(&childnotready[0]) != 0)
90 if (ck_pr_load_uint(&childnotready[1]) != 0)
92 if (ck_pr_load_uint(&childnotready[2]) != 0)
94 if (ck_pr_load_uint(&childnotready[3]) != 0)
100 CK_CC_INLINE static void
101 ck_barrier_mcs_reinitialize_children(struct ck_barrier_mcs *node)
104 ck_pr_store_uint(&node->childnotready[0], node->havechild[0]);
105 ck_pr_store_uint(&node->childnotready[1], node->havechild[1]);
106 ck_pr_store_uint(&node->childnotready[2], node->havechild[2]);
107 ck_pr_store_uint(&node->childnotready[3], node->havechild[3]);
112 ck_barrier_mcs(struct ck_barrier_mcs *barrier,
113 struct ck_barrier_mcs_state *state)
117 * Wait until all children have reached the barrier and are done waiting
118 * for their children.
120 while (ck_barrier_mcs_check_children(barrier[state->vpid].childnotready) == false)
123 /* Reinitialize for next barrier. */
124 ck_barrier_mcs_reinitialize_children(&barrier[state->vpid]);
126 /* Inform parent thread and its children have arrived at the barrier. */
127 ck_pr_store_uint(barrier[state->vpid].parent, 0);
129 /* Wait until parent indicates all threads have arrived at the barrier. */
130 if (state->vpid != 0) {
131 while (ck_pr_load_uint(&barrier[state->vpid].parentsense) != state->sense)
135 /* Inform children of successful barrier. */
136 ck_pr_store_uint(barrier[state->vpid].children[0], state->sense);
137 ck_pr_store_uint(barrier[state->vpid].children[1], state->sense);
138 state->sense = ~state->sense;
139 ck_pr_fence_memory();