1/* Copyright (C) 2010-2022 Free Software Foundation, Inc.
2   Contributed by ARM Ltd.
3
4   This file is part of the GNU Offloading and Multi Processing Library
5   (libgomp).
6
7   Libgomp is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3, or (at your option)
10   any later version.
11
12   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15   more details.
16
17   Under Section 7 of GPL version 3, you are granted additional
18   permissions described in the GCC Runtime Library Exception, version
19   3.1, as published by the Free Software Foundation.
20
21   You should have received a copy of the GNU General Public License and
22   a copy of the GCC Runtime Library Exception along with this program;
23   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24   <http://www.gnu.org/licenses/>.  */
25
26/* Provide target-specific access to the futex system call.  */
27
28/* The include file hierachy above us (wait.h) has pushed visibility
29   hidden, this will be applied to prototypes with headers we include
30   with the effect that we cannot link against an external function
31   (syscall). The solution here is to push default visibility, include
32   our required headers then reinstante the original visibility.  */
33
34#pragma GCC visibility push(default)
35
36#define _GNU_SOURCE
37#include <unistd.h>
38#include <sys/syscall.h>
39
40#pragma GCC visibility pop
41
42static inline void
43futex_wait (int *addr, int val)
44{
45  int err = syscall (SYS_futex, addr, gomp_futex_wait, val, NULL);
46  if (__builtin_expect (err < 0 && errno == ENOSYS, 0))
47    {
48      gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
49      gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
50      syscall (SYS_futex, addr, gomp_futex_wait, val, NULL);
51    }
52}
53
54static inline void
55futex_wake (int *addr, int count)
56{
57  int err = syscall (SYS_futex, addr, gomp_futex_wake, count);
58  if (__builtin_expect (err < 0 && errno == ENOSYS, 0))
59    {
60      gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
61      gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
62      syscall (SYS_futex, addr, gomp_futex_wake, count);
63    }
64}
65
66static inline void
67cpu_relax (void)
68{
69  __asm volatile ("" : : : "memory");
70}
71