Path: blob/main/sys/contrib/ck/src/ck_barrier_combining.c
48262 views
/*1* Copyright 2011-2015 Samy Al Bahra.2* Copyright 2011 David Joseph.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <ck_barrier.h>28#include <ck_cc.h>29#include <ck_pr.h>30#include <ck_spinlock.h>3132struct ck_barrier_combining_queue {33struct ck_barrier_combining_group *head;34struct ck_barrier_combining_group *tail;35};3637static struct ck_barrier_combining_group *38ck_barrier_combining_queue_dequeue(struct ck_barrier_combining_queue *queue)39{40struct ck_barrier_combining_group *front = NULL;4142if (queue->head != NULL) {43front = queue->head;44queue->head = queue->head->next;45}4647return front;48}4950static void51ck_barrier_combining_insert(struct ck_barrier_combining_group *parent,52struct ck_barrier_combining_group *tnode,53struct ck_barrier_combining_group **child)54{5556*child = tnode;57tnode->parent = parent;5859/*60* After inserting, we must increment the parent group's count for61* number of threads expected to reach it; otherwise, the62* barrier may end prematurely.63*/64parent->k++;65return;66}6768/*69* This implementation of software combining tree barriers70* uses level order traversal to insert new thread groups71* into the barrier's tree. We use a queue to implement this72* traversal.73*/74static void75ck_barrier_combining_queue_enqueue(struct ck_barrier_combining_queue *queue,76struct ck_barrier_combining_group *node_value)77{7879node_value->next = NULL;80if (queue->head == NULL) {81queue->head = queue->tail = node_value;82return;83}8485queue->tail->next = node_value;86queue->tail = node_value;8788return;89}909192void93ck_barrier_combining_group_init(struct ck_barrier_combining *root,94struct ck_barrier_combining_group *tnode,95unsigned int nthr)96{97struct ck_barrier_combining_group *node;98struct ck_barrier_combining_queue queue;99100queue.head = queue.tail = NULL;101102tnode->k = nthr;103tnode->count = 0;104tnode->sense = 0;105tnode->left = tnode->right = NULL;106107/*108* Finds the first available node for linkage into the combining109* tree. The use of a spinlock is excusable as this is a one-time110* initialization cost.111*/112ck_spinlock_fas_lock(&root->mutex);113ck_barrier_combining_queue_enqueue(&queue, root->root);114while (queue.head != NULL) {115node = ck_barrier_combining_queue_dequeue(&queue);116117/* If the left child is free, link the group there. */118if (node->left == NULL) {119ck_barrier_combining_insert(node, tnode, &node->left);120goto leave;121}122123/* If the right child is free, link the group there. */124if (node->right == NULL) {125ck_barrier_combining_insert(node, tnode, &node->right);126goto leave;127}128129/*130* If unsuccessful, try inserting as a child of the children of the131* current node.132*/133ck_barrier_combining_queue_enqueue(&queue, node->left);134ck_barrier_combining_queue_enqueue(&queue, node->right);135}136137leave:138ck_spinlock_fas_unlock(&root->mutex);139return;140}141142void143ck_barrier_combining_init(struct ck_barrier_combining *root,144struct ck_barrier_combining_group *init_root)145{146147init_root->k = 0;148init_root->count = 0;149init_root->sense = 0;150init_root->parent = init_root->left = init_root->right = NULL;151ck_spinlock_fas_init(&root->mutex);152root->root = init_root;153return;154}155156static void157ck_barrier_combining_aux(struct ck_barrier_combining *barrier,158struct ck_barrier_combining_group *tnode,159unsigned int sense)160{161162/*163* If this is the last thread in the group, it moves on to the parent group.164* Otherwise, it spins on this group's sense.165*/166if (ck_pr_faa_uint(&tnode->count, 1) == tnode->k - 1) {167/*168* If we are and will be the last thread entering the barrier for the169* current group then signal the parent group if one exists.170*/171if (tnode->parent != NULL)172ck_barrier_combining_aux(barrier, tnode->parent, sense);173174/*175* Once the thread returns from its parent(s), it reinitializes the group's176* arrival count and signals other threads to continue by flipping the group177* sense. Order of these operations is not important since we assume a static178* number of threads are members of a barrier for the lifetime of the barrier.179* Since count is explicitly reinitialized, it is guaranteed that at any point180* tnode->count is equivalent to tnode->k if and only if that many threads181* are at the barrier.182*/183ck_pr_store_uint(&tnode->count, 0);184ck_pr_fence_store();185ck_pr_store_uint(&tnode->sense, ~tnode->sense);186} else {187while (sense != ck_pr_load_uint(&tnode->sense))188ck_pr_stall();189}190ck_pr_fence_memory();191192return;193}194195void196ck_barrier_combining(struct ck_barrier_combining *barrier,197struct ck_barrier_combining_group *tnode,198struct ck_barrier_combining_state *state)199{200201ck_barrier_combining_aux(barrier, tnode, state->sense);202203/* Reverse the execution context's sense for the next barrier. */204state->sense = ~state->sense;205return;206}207208209