Path: blob/main/sys/contrib/ck/src/ck_barrier_mcs.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_stdbool.h>3132void33ck_barrier_mcs_init(struct ck_barrier_mcs *barrier, unsigned int nthr)34{35unsigned int i, j;3637ck_pr_store_uint(&barrier->tid, 0);3839for (i = 0; i < nthr; ++i) {40for (j = 0; j < 4; ++j) {41/*42* If there are still threads that don't have parents,43* add it as a child.44*/45barrier[i].havechild[j] = ((i << 2) + j < nthr - 1) ? ~0 : 0;4647/*48* childnotready is initialized to havechild to ensure49* a thread does not wait for a child that does not exist.50*/51barrier[i].childnotready[j] = barrier[i].havechild[j];52}5354/* The root thread does not have a parent. */55barrier[i].parent = (i == 0) ?56&barrier[i].dummy :57&barrier[(i - 1) >> 2].childnotready[(i - 1) & 3];5859/* Leaf threads do not have any children. */60barrier[i].children[0] = ((i << 1) + 1 >= nthr) ?61&barrier[i].dummy :62&barrier[(i << 1) + 1].parentsense;6364barrier[i].children[1] = ((i << 1) + 2 >= nthr) ?65&barrier[i].dummy :66&barrier[(i << 1) + 2].parentsense;6768barrier[i].parentsense = 0;69}7071return;72}7374void75ck_barrier_mcs_subscribe(struct ck_barrier_mcs *barrier, struct ck_barrier_mcs_state *state)76{7778state->sense = ~0;79state->vpid = ck_pr_faa_uint(&barrier->tid, 1);80return;81}8283CK_CC_INLINE static bool84ck_barrier_mcs_check_children(unsigned int *childnotready)85{8687if (ck_pr_load_uint(&childnotready[0]) != 0)88return false;89if (ck_pr_load_uint(&childnotready[1]) != 0)90return false;91if (ck_pr_load_uint(&childnotready[2]) != 0)92return false;93if (ck_pr_load_uint(&childnotready[3]) != 0)94return false;9596return true;97}9899CK_CC_INLINE static void100ck_barrier_mcs_reinitialize_children(struct ck_barrier_mcs *node)101{102103ck_pr_store_uint(&node->childnotready[0], node->havechild[0]);104ck_pr_store_uint(&node->childnotready[1], node->havechild[1]);105ck_pr_store_uint(&node->childnotready[2], node->havechild[2]);106ck_pr_store_uint(&node->childnotready[3], node->havechild[3]);107return;108}109110void111ck_barrier_mcs(struct ck_barrier_mcs *barrier,112struct ck_barrier_mcs_state *state)113{114115/*116* Wait until all children have reached the barrier and are done waiting117* for their children.118*/119while (ck_barrier_mcs_check_children(barrier[state->vpid].childnotready) == false)120ck_pr_stall();121122/* Reinitialize for next barrier. */123ck_barrier_mcs_reinitialize_children(&barrier[state->vpid]);124125/* Inform parent thread and its children have arrived at the barrier. */126ck_pr_store_uint(barrier[state->vpid].parent, 0);127128/* Wait until parent indicates all threads have arrived at the barrier. */129if (state->vpid != 0) {130while (ck_pr_load_uint(&barrier[state->vpid].parentsense) != state->sense)131ck_pr_stall();132}133134/* Inform children of successful barrier. */135ck_pr_store_uint(barrier[state->vpid].children[0], state->sense);136ck_pr_store_uint(barrier[state->vpid].children[1], state->sense);137state->sense = ~state->sense;138ck_pr_fence_memory();139return;140}141142143