On 17/07/15 08:40, Sebastian Huber wrote:
Hello,
the libgomp configuration for RTEMS uses currently the POSIX
implementation. Unfortunately the performance is unacceptable bad, so
I work currently on a specialized RTEMS configuration. I would like to
reuse the code of the Linux futex barrier. On RTEMS there is no
kernel/user space separation. In order to make the futex management
simpler, I would like to optionally embed a futex object in the
barrier. Would a change like this be acceptable?
Attached is a more complete example.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.hu...@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
>From 0410937e6f07dd1923dbcf20a0b2cda5953cd1cc Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.hu...@embedded-brains.de>
Date: Wed, 1 Jul 2015 22:14:38 +0200
Subject: [PATCH] [gomp] Add RTEMS configuration
libgomp/ChangeLog
2015-07-18 Sebastian Huber <sebastian.hu...@embedded-brains.de>
* config/rtems/bar.c: New.
* config/rtems/bar.h: Likewise.
* config/rtems/mutex.c: Likewise.
* config/rtems/mutex.h: Likewise.
* config/rtems/sem.c: Likewise.
* config/rtems/sem.h: Likewise.
* config/linux/bar.c (gomp_barrier_wait): Use
barrier_futex_wake() instead of futex_wake(). Use
barrier_futex_wait() instead of futex_wait(). Use
barrier_do_wait() instead of do_wait().
(gomp_team_barrier_wake): Likewise.
(gomp_team_barrier_wait_end): Likewise.
(gomp_team_barrier_wait_end): Likewise.
(gomp_team_barrier_wait_cancel_end): Likewise.
(gomp_team_barrier_cancel): Likewise.
* config/linux/bar.h (gomp_barrier_t): Add optional futex
member.
* config/linux/wait.h (barrier_futex_wake): New.
(barrier_futex_wait): Likewise.
(barrier_do_wait): Likewise.
* configure.ac (*-*-rtems*): Check that Newlib provides a proper
<sys/lock.h> header file.
* configure.tgt (*-*-rtems*): Enable RTEMS configuration if
supported by Newlib.
* configure: Regenerate.
---
libgomp/config/linux/bar.c | 22 ++++++++-------
libgomp/config/linux/bar.h | 3 ++
libgomp/config/linux/wait.h | 6 ++++
libgomp/config/rtems/bar.c | 65 ++++++++++++++++++++++++++++++++++++++++++++
libgomp/config/rtems/bar.h | 39 ++++++++++++++++++++++++++
libgomp/config/rtems/mutex.c | 1 +
libgomp/config/rtems/mutex.h | 57 ++++++++++++++++++++++++++++++++++++++
libgomp/config/rtems/sem.c | 1 +
libgomp/config/rtems/sem.h | 55 +++++++++++++++++++++++++++++++++++++
libgomp/configure | 17 ++++++++++++
libgomp/configure.ac | 7 +++++
libgomp/configure.tgt | 7 +++++
12 files changed, 270 insertions(+), 10 deletions(-)
create mode 100644 libgomp/config/rtems/bar.c
create mode 100644 libgomp/config/rtems/bar.h
create mode 100644 libgomp/config/rtems/mutex.c
create mode 100644 libgomp/config/rtems/mutex.h
create mode 100644 libgomp/config/rtems/sem.c
create mode 100644 libgomp/config/rtems/sem.h
diff --git a/libgomp/config/linux/bar.c b/libgomp/config/linux/bar.c
index 51fbd99..920b85e 100644
--- a/libgomp/config/linux/bar.c
+++ b/libgomp/config/linux/bar.c
@@ -40,12 +40,12 @@ gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
bar->awaited = bar->total;
__atomic_store_n (&bar->generation, bar->generation + BAR_INCR,
MEMMODEL_RELEASE);
- futex_wake ((int *) &bar->generation, INT_MAX);
+ barrier_futex_wake ((int *) &bar->generation, INT_MAX, &bar->futex);
}
else
{
do
- do_wait ((int *) &bar->generation, state);
+ barrier_do_wait ((int *) &bar->generation, state, &bar->futex);
while (__atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE) == state);
}
}
@@ -74,7 +74,8 @@ gomp_barrier_wait_last (gomp_barrier_t *bar)
void
gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
{
- futex_wake ((int *) &bar->generation, count == 0 ? INT_MAX : count);
+ barrier_futex_wake ((int *) &bar->generation, count == 0 ? INT_MAX : count,
+ &bar->futex);
}
void
@@ -100,7 +101,7 @@ gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
state &= ~BAR_CANCELLED;
state += BAR_INCR - BAR_WAS_LAST;
__atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
- futex_wake ((int *) &bar->generation, INT_MAX);
+ barrier_futex_wake ((int *) &bar->generation, INT_MAX, &bar->futex);
return;
}
}
@@ -109,7 +110,7 @@ gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
state &= ~BAR_CANCELLED;
do
{
- do_wait ((int *) &bar->generation, generation);
+ barrier_do_wait ((int *) &bar->generation, generation, &bar->futex);
gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
{
@@ -163,7 +164,7 @@ gomp_team_barrier_wait_cancel_end (gomp_barrier_t *bar,
{
state += BAR_INCR - BAR_WAS_LAST;
__atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
- futex_wake ((int *) &bar->generation, INT_MAX);
+ barrier_futex_wake ((int *) &bar->generation, INT_MAX, &bar->futex);
return false;
}
}
@@ -174,7 +175,7 @@ gomp_team_barrier_wait_cancel_end (gomp_barrier_t *bar,
generation = state;
do
{
- do_wait ((int *) &bar->generation, generation);
+ barrier_do_wait ((int *) &bar->generation, generation, &bar->futex);
gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
if (__builtin_expect (gen & BAR_CANCELLED, 0))
return true;
@@ -199,13 +200,14 @@ gomp_team_barrier_wait_cancel (gomp_barrier_t *bar)
void
gomp_team_barrier_cancel (struct gomp_team *team)
{
+ gomp_barrier_t *bar = &team->barrier;
gomp_mutex_lock (&team->task_lock);
- if (team->barrier.generation & BAR_CANCELLED)
+ if (bar->generation & BAR_CANCELLED)
{
gomp_mutex_unlock (&team->task_lock);
return;
}
- team->barrier.generation |= BAR_CANCELLED;
+ bar->generation |= BAR_CANCELLED;
gomp_mutex_unlock (&team->task_lock);
- futex_wake ((int *) &team->barrier.generation, INT_MAX);
+ barrier_futex_wake ((int *) &bar->generation, INT_MAX, &bar->futex);
}
diff --git a/libgomp/config/linux/bar.h b/libgomp/config/linux/bar.h
index 3236436..b47b9f6 100644
--- a/libgomp/config/linux/bar.h
+++ b/libgomp/config/linux/bar.h
@@ -40,6 +40,9 @@ typedef struct
unsigned generation;
unsigned awaited __attribute__((aligned (64)));
unsigned awaited_final;
+#ifdef HAVE_BARRIER_FUTEX
+ gomp_barrier_futex_t futex;
+#endif
} gomp_barrier_t;
typedef unsigned int gomp_barrier_state_t;
diff --git a/libgomp/config/linux/wait.h b/libgomp/config/linux/wait.h
index 7f57454..b244494 100644
--- a/libgomp/config/linux/wait.h
+++ b/libgomp/config/linux/wait.h
@@ -65,6 +65,12 @@ static inline void do_wait (int *addr, int val)
futex_wait (addr, val);
}
+#define barrier_futex_wait(addr, val, futex) futex_wait (addr, val)
+
+#define barrier_futex_wake(addr, count, futex) futex_wake (addr, count)
+
+#define barrier_do_wait(addr, val, futex) do_wait (addr, val)
+
#ifdef HAVE_ATTRIBUTE_VISIBILITY
# pragma GCC visibility pop
#endif
diff --git a/libgomp/config/rtems/bar.c b/libgomp/config/rtems/bar.c
new file mode 100644
index 0000000..a152f0a
--- /dev/null
+++ b/libgomp/config/rtems/bar.c
@@ -0,0 +1,65 @@
+/* Copyright (C) 2015 Free Software Foundation, Inc.
+ Contributed by Sebastian Huber <sebastian.hu...@embedded-brains.de>.
+
+ This file is part of the GNU OpenMP Library (libgomp).
+
+ Libgomp is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ more details.
+
+ Under Section 7 of GPL version 3, you are granted additional
+ permissions described in the GCC Runtime Library Exception, version
+ 3.1, as published by the Free Software Foundation.
+
+ You should have received a copy of the GNU General Public License and
+ a copy of the GCC Runtime Library Exception along with this program;
+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+/* This is the RTEMS implementation of a barrier synchronization
+ mechanism for libgomp. This type is private to the library. */
+
+#include <libgomp.h>
+#include <bar.h>
+
+static void
+barrier_futex_wait (int *addr, int val, gomp_barrier_futex_t *futex)
+{
+ _Futex_Wait (futex, addr, val);
+}
+
+static void
+barrier_futex_wake (int *addr, int count, gomp_barrier_futex_t *futex)
+{
+ _Futex_Wake (futex, count);
+}
+
+static int
+do_spin (int *addr, int val)
+{
+ unsigned long long i, count = gomp_spin_count_var;
+
+ if (__builtin_expect (gomp_managed_threads > gomp_available_cpus, 0))
+ count = gomp_throttled_spin_count_var;
+ for (i = 0; i < count; i++)
+ if (__builtin_expect (__atomic_load_n (addr, MEMMODEL_RELAXED) != val, 0))
+ return 0;
+ return 1;
+}
+
+static void
+barrier_do_wait (int *addr, int val, gomp_barrier_futex_t *futex)
+{
+ if (do_spin (addr, val))
+ barrier_futex_wait (addr, val, futex);
+}
+
+#define GOMP_WAIT_H 1
+
+#include "../linux/bar.c"
diff --git a/libgomp/config/rtems/bar.h b/libgomp/config/rtems/bar.h
new file mode 100644
index 0000000..a9acdea
--- /dev/null
+++ b/libgomp/config/rtems/bar.h
@@ -0,0 +1,39 @@
+/* Copyright (C) 2015 Free Software Foundation, Inc.
+ Contributed by Sebastian Huber <sebastian.hu...@embedded-brains.de>.
+
+ This file is part of the GNU OpenMP Library (libgomp).
+
+ Libgomp is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ more details.
+
+ Under Section 7 of GPL version 3, you are granted additional
+ permissions described in the GCC Runtime Library Exception, version
+ 3.1, as published by the Free Software Foundation.
+
+ You should have received a copy of the GNU General Public License and
+ a copy of the GCC Runtime Library Exception along with this program;
+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+/* This is the RTEMS implementation of a barrier synchronization
+ mechanism for libgomp. This type is private to the library. */
+
+#ifndef GOMP_RTEMS_BARRIER_H
+#define GOMP_RTEMS_BARRIER_H 1
+
+#include <sys/lock.h>
+
+typedef struct _Futex_Control gomp_barrier_futex_t;
+
+#define HAVE_BARRIER_FUTEX 1
+
+#include "../linux/bar.h"
+
+#endif /* GOMP_RTEMS_BARRIER_H */
diff --git a/libgomp/config/rtems/mutex.c b/libgomp/config/rtems/mutex.c
new file mode 100644
index 0000000..39bb64d
--- /dev/null
+++ b/libgomp/config/rtems/mutex.c
@@ -0,0 +1 @@
+/* Everything is in the header. */
diff --git a/libgomp/config/rtems/mutex.h b/libgomp/config/rtems/mutex.h
new file mode 100644
index 0000000..39901ee
--- /dev/null
+++ b/libgomp/config/rtems/mutex.h
@@ -0,0 +1,57 @@
+/* Copyright (C) 2015 Free Software Foundation, Inc.
+ Contributed by Sebastian Huber <sebastian.hu...@embedded-brains.de>.
+
+ This file is part of the GNU OpenMP Library (libgomp).
+
+ Libgomp is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ more details.
+
+ Under Section 7 of GPL version 3, you are granted additional
+ permissions described in the GCC Runtime Library Exception, version
+ 3.1, as published by the Free Software Foundation.
+
+ You should have received a copy of the GNU General Public License and
+ a copy of the GCC Runtime Library Exception along with this program;
+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+/* This is the RTEMS implementation of a mutex synchronization
+ mechanism for libgomp. This type is private to the library. */
+
+#ifndef GOMP_MUTEX_H
+#define GOMP_MUTEX_H 1
+
+#include <sys/lock.h>
+
+typedef struct _Mutex_Control gomp_mutex_t;
+
+#define GOMP_MUTEX_INIT_0 1
+
+static inline void gomp_mutex_init (gomp_mutex_t *mutex)
+{
+ _Mutex_Initialize (mutex);
+}
+
+static inline void gomp_mutex_lock (gomp_mutex_t *mutex)
+{
+ _Mutex_Acquire (mutex);
+}
+
+static inline void gomp_mutex_unlock (gomp_mutex_t *mutex)
+{
+ _Mutex_Release (mutex);
+}
+
+static inline void gomp_mutex_destroy (gomp_mutex_t *mutex)
+{
+ _Mutex_Destroy (mutex);
+}
+
+#endif /* GOMP_MUTEX_H */
diff --git a/libgomp/config/rtems/sem.c b/libgomp/config/rtems/sem.c
new file mode 100644
index 0000000..39bb64d
--- /dev/null
+++ b/libgomp/config/rtems/sem.c
@@ -0,0 +1 @@
+/* Everything is in the header. */
diff --git a/libgomp/config/rtems/sem.h b/libgomp/config/rtems/sem.h
new file mode 100644
index 0000000..0d6765b
--- /dev/null
+++ b/libgomp/config/rtems/sem.h
@@ -0,0 +1,55 @@
+/* Copyright (C) 2015 Free Software Foundation, Inc.
+ Contributed by Sebastian Huber <sebastian.hu...@embedded-brains.de>.
+
+ This file is part of the GNU OpenMP Library (libgomp).
+
+ Libgomp is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ more details.
+
+ Under Section 7 of GPL version 3, you are granted additional
+ permissions described in the GCC Runtime Library Exception, version
+ 3.1, as published by the Free Software Foundation.
+
+ You should have received a copy of the GNU General Public License and
+ a copy of the GCC Runtime Library Exception along with this program;
+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+/* This is the RTEMS implementation of a semaphore synchronization
+ mechanism for libgomp. This type is private to the library. */
+
+#ifndef GOMP_SEM_H
+#define GOMP_SEM_H 1
+
+#include <sys/lock.h>
+
+typedef struct _Semaphore_Control gomp_sem_t;
+
+static inline void gomp_sem_init (gomp_sem_t *sem, int value)
+{
+ _Semaphore_Initialize (sem, (unsigned int) value);
+}
+
+static inline void gomp_sem_wait (gomp_sem_t *sem)
+{
+ _Semaphore_Wait (sem);
+}
+
+static inline void gomp_sem_post (gomp_sem_t *sem)
+{
+ _Semaphore_Post (sem);
+}
+
+static inline void gomp_sem_destroy (gomp_sem_t *sem)
+{
+ _Semaphore_Destroy (sem);
+}
+
+#endif /* GOMP_SEM_H */
diff --git a/libgomp/configure b/libgomp/configure
index f1a92ba..a42e6bb 100755
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -15338,6 +15338,23 @@ $as_echo "#define HAVE_BROKEN_POSIX_SEMAPHORES 1" >>confdefs.h
;;
esac
+# RTEMS specific checks
+case "$host" in
+ *-*-rtems*)
+ ac_fn_c_check_type "$LINENO" "struct _Mutex_Control" "ac_cv_type_struct__Mutex_Control" "#include <sys/lock.h>
+"
+if test "x$ac_cv_type_struct__Mutex_Control" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT__MUTEX_CONTROL 1
+_ACEOF
+
+
+fi
+
+ ;;
+esac
+
# Check whether --enable-linux-futex was given.
if test "${enable_linux_futex+set}" = set; then :
enableval=$enable_linux_futex;
diff --git a/libgomp/configure.ac b/libgomp/configure.ac
index 9cf0218..461845e 100644
--- a/libgomp/configure.ac
+++ b/libgomp/configure.ac
@@ -213,6 +213,13 @@ case "$host" in
;;
esac
+# RTEMS specific checks
+case "$host" in
+ *-*-rtems*)
+ AC_CHECK_TYPES([struct _Mutex_Control],[],[],[#include <sys/lock.h>])
+ ;;
+esac
+
GCC_LINUX_FUTEX(:)
# Check for pthread_{,attr_}[sg]etaffinity_np.
diff --git a/libgomp/configure.tgt b/libgomp/configure.tgt
index 2970f6f..25ec7fc 100644
--- a/libgomp/configure.tgt
+++ b/libgomp/configure.tgt
@@ -151,6 +151,13 @@ case "${target}" in
XLDFLAGS="${XLDFLAGS} -lpthread"
;;
+ *-*-rtems*)
+ # Use self-contained synchronization objects if provided by Newlib
+ if test "x$ac_cv_type_struct__Mutex_Control" = xyes ; then
+ config_path="rtems posix"
+ fi
+ ;;
+
*)
;;
--
1.8.4.5