| 1 |
735 |
jeremybenn |
/* Copyright (C) 2005, 2008, 2009, 2011 Free Software Foundation, Inc.
|
| 2 |
|
|
Contributed by Richard Henderson <rth@redhat.com>.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of the GNU OpenMP Library (libgomp).
|
| 5 |
|
|
|
| 6 |
|
|
Libgomp is free software; you can redistribute it and/or modify it
|
| 7 |
|
|
under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 3, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
|
| 12 |
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
| 13 |
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
| 14 |
|
|
more details.
|
| 15 |
|
|
|
| 16 |
|
|
Under Section 7 of GPL version 3, you are granted additional
|
| 17 |
|
|
permissions described in the GCC Runtime Library Exception, version
|
| 18 |
|
|
3.1, as published by the Free Software Foundation.
|
| 19 |
|
|
|
| 20 |
|
|
You should have received a copy of the GNU General Public License and
|
| 21 |
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
| 22 |
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
| 23 |
|
|
<http://www.gnu.org/licenses/>. */
|
| 24 |
|
|
|
| 25 |
|
|
/* This is a Linux specific implementation of a barrier synchronization
|
| 26 |
|
|
mechanism for libgomp. This type is private to the library. This
|
| 27 |
|
|
implementation uses atomic instructions and the futex syscall. */
|
| 28 |
|
|
|
| 29 |
|
|
#ifndef GOMP_BARRIER_H
|
| 30 |
|
|
#define GOMP_BARRIER_H 1
|
| 31 |
|
|
|
| 32 |
|
|
#include "mutex.h"
|
| 33 |
|
|
|
| 34 |
|
|
typedef struct
|
| 35 |
|
|
{
|
| 36 |
|
|
/* Make sure total/generation is in a mostly read cacheline, while
|
| 37 |
|
|
awaited in a separate cacheline. */
|
| 38 |
|
|
unsigned total __attribute__((aligned (64)));
|
| 39 |
|
|
unsigned generation;
|
| 40 |
|
|
unsigned awaited __attribute__((aligned (64)));
|
| 41 |
|
|
} gomp_barrier_t;
|
| 42 |
|
|
typedef unsigned int gomp_barrier_state_t;
|
| 43 |
|
|
|
| 44 |
|
|
static inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
|
| 45 |
|
|
{
|
| 46 |
|
|
bar->total = count;
|
| 47 |
|
|
bar->awaited = count;
|
| 48 |
|
|
bar->generation = 0;
|
| 49 |
|
|
}
|
| 50 |
|
|
|
| 51 |
|
|
static inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
|
| 52 |
|
|
{
|
| 53 |
|
|
__atomic_add_fetch (&bar->awaited, count - bar->total, MEMMODEL_ACQ_REL);
|
| 54 |
|
|
bar->total = count;
|
| 55 |
|
|
}
|
| 56 |
|
|
|
| 57 |
|
|
static inline void gomp_barrier_destroy (gomp_barrier_t *bar)
|
| 58 |
|
|
{
|
| 59 |
|
|
}
|
| 60 |
|
|
|
| 61 |
|
|
extern void gomp_barrier_wait (gomp_barrier_t *);
|
| 62 |
|
|
extern void gomp_barrier_wait_last (gomp_barrier_t *);
|
| 63 |
|
|
extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
|
| 64 |
|
|
extern void gomp_team_barrier_wait (gomp_barrier_t *);
|
| 65 |
|
|
extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
|
| 66 |
|
|
gomp_barrier_state_t);
|
| 67 |
|
|
extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
|
| 68 |
|
|
|
| 69 |
|
|
static inline gomp_barrier_state_t
|
| 70 |
|
|
gomp_barrier_wait_start (gomp_barrier_t *bar)
|
| 71 |
|
|
{
|
| 72 |
|
|
unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE) & ~3;
|
| 73 |
|
|
/* A memory barrier is needed before exiting from the various forms
|
| 74 |
|
|
of gomp_barrier_wait, to satisfy OpenMP API version 3.1 section
|
| 75 |
|
|
2.8.6 flush Construct, which says there is an implicit flush during
|
| 76 |
|
|
a barrier region. This is a convenient place to add the barrier,
|
| 77 |
|
|
so we use MEMMODEL_ACQ_REL here rather than MEMMODEL_ACQUIRE. */
|
| 78 |
|
|
ret += __atomic_add_fetch (&bar->awaited, -1, MEMMODEL_ACQ_REL) == 0;
|
| 79 |
|
|
return ret;
|
| 80 |
|
|
}
|
| 81 |
|
|
|
| 82 |
|
|
static inline bool
|
| 83 |
|
|
gomp_barrier_last_thread (gomp_barrier_state_t state)
|
| 84 |
|
|
{
|
| 85 |
|
|
return state & 1;
|
| 86 |
|
|
}
|
| 87 |
|
|
|
| 88 |
|
|
/* All the inlines below must be called with team->task_lock
|
| 89 |
|
|
held. */
|
| 90 |
|
|
|
| 91 |
|
|
static inline void
|
| 92 |
|
|
gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
|
| 93 |
|
|
{
|
| 94 |
|
|
bar->generation |= 1;
|
| 95 |
|
|
}
|
| 96 |
|
|
|
| 97 |
|
|
static inline void
|
| 98 |
|
|
gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
|
| 99 |
|
|
{
|
| 100 |
|
|
bar->generation &= ~1;
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
static inline void
|
| 104 |
|
|
gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
|
| 105 |
|
|
{
|
| 106 |
|
|
bar->generation |= 2;
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
static inline bool
|
| 110 |
|
|
gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
|
| 111 |
|
|
{
|
| 112 |
|
|
return (bar->generation & 2) != 0;
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
static inline void
|
| 116 |
|
|
gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
|
| 117 |
|
|
{
|
| 118 |
|
|
bar->generation = (state & ~3) + 4;
|
| 119 |
|
|
}
|
| 120 |
|
|
|
| 121 |
|
|
#endif /* GOMP_BARRIER_H */
|