Home | History | Annotate | Line # | Download | only in linux
bar.c revision 1.1
      1  1.1  mrg /* Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc.
      2  1.1  mrg    Contributed by Richard Henderson <rth (at) redhat.com>.
      3  1.1  mrg 
      4  1.1  mrg    This file is part of the GNU OpenMP Library (libgomp).
      5  1.1  mrg 
      6  1.1  mrg    Libgomp is free software; you can redistribute it and/or modify it
      7  1.1  mrg    under the terms of the GNU General Public License as published by
      8  1.1  mrg    the Free Software Foundation; either version 3, or (at your option)
      9  1.1  mrg    any later version.
     10  1.1  mrg 
     11  1.1  mrg    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
     12  1.1  mrg    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     13  1.1  mrg    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
     14  1.1  mrg    more details.
     15  1.1  mrg 
     16  1.1  mrg    Under Section 7 of GPL version 3, you are granted additional
     17  1.1  mrg    permissions described in the GCC Runtime Library Exception, version
     18  1.1  mrg    3.1, as published by the Free Software Foundation.
     19  1.1  mrg 
     20  1.1  mrg    You should have received a copy of the GNU General Public License and
     21  1.1  mrg    a copy of the GCC Runtime Library Exception along with this program;
     22  1.1  mrg    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23  1.1  mrg    <http://www.gnu.org/licenses/>.  */
     24  1.1  mrg 
     25  1.1  mrg /* This is a Linux specific implementation of a barrier synchronization
     26  1.1  mrg    mechanism for libgomp.  This type is private to the library.  This
     27  1.1  mrg    implementation uses atomic instructions and the futex syscall.  */
     28  1.1  mrg 
     29  1.1  mrg #include <limits.h>
     30  1.1  mrg #include "wait.h"
     31  1.1  mrg 
     32  1.1  mrg 
     33  1.1  mrg void
     34  1.1  mrg gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
     35  1.1  mrg {
     36  1.1  mrg   if (__builtin_expect ((state & 1) != 0, 0))
     37  1.1  mrg     {
     38  1.1  mrg       /* Next time we'll be awaiting TOTAL threads again.  */
     39  1.1  mrg       bar->awaited = bar->total;
     40  1.1  mrg       atomic_write_barrier ();
     41  1.1  mrg       bar->generation += 4;
     42  1.1  mrg       futex_wake ((int *) &bar->generation, INT_MAX);
     43  1.1  mrg     }
     44  1.1  mrg   else
     45  1.1  mrg     {
     46  1.1  mrg       unsigned int generation = state;
     47  1.1  mrg 
     48  1.1  mrg       do
     49  1.1  mrg 	do_wait ((int *) &bar->generation, generation);
     50  1.1  mrg       while (bar->generation == generation);
     51  1.1  mrg     }
     52  1.1  mrg }
     53  1.1  mrg 
     54  1.1  mrg void
     55  1.1  mrg gomp_barrier_wait (gomp_barrier_t *bar)
     56  1.1  mrg {
     57  1.1  mrg   gomp_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
     58  1.1  mrg }
     59  1.1  mrg 
     60  1.1  mrg /* Like gomp_barrier_wait, except that if the encountering thread
     61  1.1  mrg    is not the last one to hit the barrier, it returns immediately.
     62  1.1  mrg    The intended usage is that a thread which intends to gomp_barrier_destroy
     63  1.1  mrg    this barrier calls gomp_barrier_wait, while all other threads
     64  1.1  mrg    call gomp_barrier_wait_last.  When gomp_barrier_wait returns,
     65  1.1  mrg    the barrier can be safely destroyed.  */
     66  1.1  mrg 
     67  1.1  mrg void
     68  1.1  mrg gomp_barrier_wait_last (gomp_barrier_t *bar)
     69  1.1  mrg {
     70  1.1  mrg   gomp_barrier_state_t state = gomp_barrier_wait_start (bar);
     71  1.1  mrg   if (state & 1)
     72  1.1  mrg     gomp_barrier_wait_end (bar, state);
     73  1.1  mrg }
     74  1.1  mrg 
     75  1.1  mrg void
     76  1.1  mrg gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
     77  1.1  mrg {
     78  1.1  mrg   futex_wake ((int *) &bar->generation, count == 0 ? INT_MAX : count);
     79  1.1  mrg }
     80  1.1  mrg 
     81  1.1  mrg void
     82  1.1  mrg gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
     83  1.1  mrg {
     84  1.1  mrg   unsigned int generation;
     85  1.1  mrg 
     86  1.1  mrg   if (__builtin_expect ((state & 1) != 0, 0))
     87  1.1  mrg     {
     88  1.1  mrg       /* Next time we'll be awaiting TOTAL threads again.  */
     89  1.1  mrg       struct gomp_thread *thr = gomp_thread ();
     90  1.1  mrg       struct gomp_team *team = thr->ts.team;
     91  1.1  mrg       bar->awaited = bar->total;
     92  1.1  mrg       atomic_write_barrier ();
     93  1.1  mrg       if (__builtin_expect (team->task_count, 0))
     94  1.1  mrg 	{
     95  1.1  mrg 	  gomp_barrier_handle_tasks (state);
     96  1.1  mrg 	  state &= ~1;
     97  1.1  mrg 	}
     98  1.1  mrg       else
     99  1.1  mrg 	{
    100  1.1  mrg 	  bar->generation = state + 3;
    101  1.1  mrg 	  futex_wake ((int *) &bar->generation, INT_MAX);
    102  1.1  mrg 	  return;
    103  1.1  mrg 	}
    104  1.1  mrg     }
    105  1.1  mrg 
    106  1.1  mrg   generation = state;
    107  1.1  mrg   do
    108  1.1  mrg     {
    109  1.1  mrg       do_wait ((int *) &bar->generation, generation);
    110  1.1  mrg       if (__builtin_expect (bar->generation & 1, 0))
    111  1.1  mrg 	gomp_barrier_handle_tasks (state);
    112  1.1  mrg       if ((bar->generation & 2))
    113  1.1  mrg 	generation |= 2;
    114  1.1  mrg     }
    115  1.1  mrg   while (bar->generation != state + 4);
    116  1.1  mrg }
    117  1.1  mrg 
    118  1.1  mrg void
    119  1.1  mrg gomp_team_barrier_wait (gomp_barrier_t *bar)
    120  1.1  mrg {
    121  1.1  mrg   gomp_team_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
    122  1.1  mrg }
    123