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_spinlock.h>
33 struct ck_barrier_combining_queue {
34 struct ck_barrier_combining_group *head;
35 struct ck_barrier_combining_group *tail;
38 static struct ck_barrier_combining_group *
39 ck_barrier_combining_queue_dequeue(struct ck_barrier_combining_queue *queue)
41 struct ck_barrier_combining_group *front = NULL;
43 if (queue->head != NULL) {
45 queue->head = queue->head->next;
52 ck_barrier_combining_insert(struct ck_barrier_combining_group *parent,
53 struct ck_barrier_combining_group *tnode,
54 struct ck_barrier_combining_group **child)
58 tnode->parent = parent;
61 * After inserting, we must increment the parent group's count for
62 * number of threads expected to reach it; otherwise, the
63 * barrier may end prematurely.
70 * This implementation of software combining tree barriers
71 * uses level order traversal to insert new thread groups
72 * into the barrier's tree. We use a queue to implement this
76 ck_barrier_combining_queue_enqueue(struct ck_barrier_combining_queue *queue,
77 struct ck_barrier_combining_group *node_value)
80 node_value->next = NULL;
81 if (queue->head == NULL) {
82 queue->head = queue->tail = node_value;
86 queue->tail->next = node_value;
87 queue->tail = node_value;
94 ck_barrier_combining_group_init(struct ck_barrier_combining *root,
95 struct ck_barrier_combining_group *tnode,
98 struct ck_barrier_combining_group *node;
99 struct ck_barrier_combining_queue queue;
101 queue.head = queue.tail = NULL;
106 tnode->left = tnode->right = NULL;
109 * Finds the first available node for linkage into the combining
110 * tree. The use of a spinlock is excusable as this is a one-time
111 * initialization cost.
113 ck_spinlock_fas_lock(&root->mutex);
114 ck_barrier_combining_queue_enqueue(&queue, root->root);
115 while (queue.head != NULL) {
116 node = ck_barrier_combining_queue_dequeue(&queue);
118 /* If the left child is free, link the group there. */
119 if (node->left == NULL) {
120 ck_barrier_combining_insert(node, tnode, &node->left);
124 /* If the right child is free, link the group there. */
125 if (node->right == NULL) {
126 ck_barrier_combining_insert(node, tnode, &node->right);
131 * If unsuccessful, try inserting as a child of the children of the
134 ck_barrier_combining_queue_enqueue(&queue, node->left);
135 ck_barrier_combining_queue_enqueue(&queue, node->right);
139 ck_spinlock_fas_unlock(&root->mutex);
144 ck_barrier_combining_init(struct ck_barrier_combining *root,
145 struct ck_barrier_combining_group *init_root)
149 init_root->count = 0;
150 init_root->sense = 0;
151 init_root->parent = init_root->left = init_root->right = NULL;
152 ck_spinlock_fas_init(&root->mutex);
153 root->root = init_root;
158 ck_barrier_combining_aux(struct ck_barrier_combining *barrier,
159 struct ck_barrier_combining_group *tnode,
164 * If this is the last thread in the group, it moves on to the parent group.
165 * Otherwise, it spins on this group's sense.
167 if (ck_pr_faa_uint(&tnode->count, 1) == tnode->k - 1) {
169 * If we are and will be the last thread entering the barrier for the
170 * current group then signal the parent group if one exists.
172 if (tnode->parent != NULL)
173 ck_barrier_combining_aux(barrier, tnode->parent, sense);
176 * Once the thread returns from its parent(s), it reinitializes the group's
177 * arrival count and signals other threads to continue by flipping the group
178 * sense. Order of these operations is not important since we assume a static
179 * number of threads are members of a barrier for the lifetime of the barrier.
180 * Since count is explicitly reinitialized, it is guaranteed that at any point
181 * tnode->count is equivalent to tnode->k if and only if that many threads
182 * are at the barrier.
184 ck_pr_store_uint(&tnode->count, 0);
186 ck_pr_store_uint(&tnode->sense, ~tnode->sense);
188 while (sense != ck_pr_load_uint(&tnode->sense))
191 ck_pr_fence_memory();
197 ck_barrier_combining(struct ck_barrier_combining *barrier,
198 struct ck_barrier_combining_group *tnode,
199 struct ck_barrier_combining_state *state)
202 ck_barrier_combining_aux(barrier, tnode, state->sense);
204 /* Reverse the execution context's sense for the next barrier. */
205 state->sense = ~state->sense;