Path: blob/main/sys/contrib/ck/src/ck_barrier_centralized.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_pr.h>2930void31ck_barrier_centralized(struct ck_barrier_centralized *barrier,32struct ck_barrier_centralized_state *state,33unsigned int n_threads)34{35unsigned int sense, value;3637/*38* Every execution context has a sense associated with it.39* This sense is reversed when the barrier is entered. Every40* thread will spin on the global sense until the last thread41* reverses it.42*/43sense = state->sense = ~state->sense;44value = ck_pr_faa_uint(&barrier->value, 1);45if (value == n_threads - 1) {46ck_pr_store_uint(&barrier->value, 0);47ck_pr_fence_memory();48ck_pr_store_uint(&barrier->sense, sense);49return;50}5152ck_pr_fence_atomic_load();53while (sense != ck_pr_load_uint(&barrier->sense))54ck_pr_stall();5556ck_pr_fence_acquire();57return;58}596061