The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/contrib/ck/src/ck_barrier_dissemination.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright 2011-2015 Samy Al Bahra.
    3  * Copyright 2011 David Joseph.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    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.
   14  *
   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
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <ck_barrier.h>
   29 #include <ck_cc.h>
   30 #include <ck_pr.h>
   31 #include <ck_spinlock.h>
   32 
   33 #include "ck_internal.h"
   34 
   35 void
   36 ck_barrier_dissemination_init(struct ck_barrier_dissemination *barrier,
   37     struct ck_barrier_dissemination_flag **barrier_internal,
   38     unsigned int nthr)
   39 {
   40         unsigned int i, j, k, size, offset;
   41         bool p = nthr & (nthr - 1);
   42 
   43         barrier->nthr = nthr;
   44         barrier->size = size = ck_internal_log(ck_internal_power_2(nthr));
   45         ck_pr_store_uint(&barrier->tid, 0);
   46 
   47         for (i = 0; i < nthr; ++i) {
   48                 barrier[i].flags[0] = barrier_internal[i];
   49                 barrier[i].flags[1] = barrier_internal[i] + size;
   50         }
   51 
   52         for (i = 0; i < nthr; ++i) {
   53                 for (k = 0, offset = 1; k < size; ++k, offset <<= 1) {
   54                         /*
   55                          * Determine the thread's partner, j, for the current round, k.
   56                          * Partners are chosen such that by the completion of the barrier,
   57                          * every thread has been directly (having one of its flag set) or
   58                          * indirectly (having one of its partners's flags set) signaled
   59                          * by every other thread in the barrier.
   60                          */
   61                         if (p == false)
   62                                 j = (i + offset) & (nthr - 1);
   63                         else
   64                                 j = (i + offset) % nthr;
   65 
   66                         /* Set the thread's partner for round k. */
   67                         barrier[i].flags[0][k].pflag = &barrier[j].flags[0][k].tflag;
   68                         barrier[i].flags[1][k].pflag = &barrier[j].flags[1][k].tflag;
   69 
   70                         /* Set the thread's flags to false. */
   71                         barrier[i].flags[0][k].tflag = barrier[i].flags[1][k].tflag = 0;
   72                 }
   73         }
   74 
   75         return;
   76 }
   77 
   78 void
   79 ck_barrier_dissemination_subscribe(struct ck_barrier_dissemination *barrier,
   80     struct ck_barrier_dissemination_state *state)
   81 {
   82 
   83         state->parity = 0;
   84         state->sense = ~0;
   85         state->tid = ck_pr_faa_uint(&barrier->tid, 1);
   86         return;
   87 }
   88 
   89 unsigned int
   90 ck_barrier_dissemination_size(unsigned int nthr)
   91 {
   92 
   93         return (ck_internal_log(ck_internal_power_2(nthr)) << 1);
   94 }
   95 
   96 void
   97 ck_barrier_dissemination(struct ck_barrier_dissemination *barrier,
   98     struct ck_barrier_dissemination_state *state)
   99 {
  100         unsigned int i;
  101         unsigned int size = barrier->size;
  102 
  103         for (i = 0; i < size; ++i) {
  104                 unsigned int *pflag, *tflag;
  105 
  106                 pflag = barrier[state->tid].flags[state->parity][i].pflag;
  107                 tflag = &barrier[state->tid].flags[state->parity][i].tflag;
  108 
  109                 /* Unblock current partner. */
  110                 ck_pr_store_uint(pflag, state->sense);
  111 
  112                 /* Wait until some other thread unblocks this one. */
  113                 while (ck_pr_load_uint(tflag) != state->sense)
  114                         ck_pr_stall();
  115         }
  116 
  117         /*
  118          * Dissemination barriers use two sets of flags to prevent race conditions
  119          * between successive calls to the barrier. Parity indicates which set will
  120          * be used for the next barrier. They also use a sense reversal technique
  121          * to avoid re-initialization of the flags for every two calls to the barrier.
  122          */
  123         if (state->parity == 1)
  124                 state->sense = ~state->sense;
  125 
  126         state->parity = 1 - state->parity;
  127 
  128         ck_pr_fence_acquire();
  129         return;
  130 }

Cache object: 56e96b22828520cda812d616e8eba52a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.