|
Packit Service |
82fcde |
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef _LOWLEVELLOCK_H
|
|
Packit Service |
82fcde |
#define _LOWLEVELLOCK_H 1
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <stap-probe.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef __ASSEMBLER__
|
|
Packit Service |
82fcde |
# include <time.h>
|
|
Packit Service |
82fcde |
# include <sys/param.h>
|
|
Packit Service |
82fcde |
# include <bits/pthreadtypes.h>
|
|
Packit Service |
82fcde |
# include <kernel-features.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# ifndef LOCK_INSTR
|
|
Packit Service |
82fcde |
# ifdef UP
|
|
Packit Service |
82fcde |
# define LOCK_INSTR /* nothing */
|
|
Packit Service |
82fcde |
# else
|
|
Packit Service |
82fcde |
# define LOCK_INSTR "lock;"
|
|
Packit Service |
82fcde |
# endif
|
|
Packit Service |
82fcde |
# endif
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
# ifndef LOCK
|
|
Packit Service |
82fcde |
# ifdef UP
|
|
Packit Service |
82fcde |
# define LOCK
|
|
Packit Service |
82fcde |
# else
|
|
Packit Service |
82fcde |
# define LOCK lock
|
|
Packit Service |
82fcde |
# endif
|
|
Packit Service |
82fcde |
# endif
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <lowlevellock-futex.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* XXX Remove when no assembler code uses futexes anymore. */
|
|
Packit Service |
82fcde |
#define SYS_futex __NR_futex
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef __ASSEMBLER__
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Initializer for lock. */
|
|
Packit Service |
82fcde |
#define LLL_LOCK_INITIALIZER (0)
|
|
Packit Service |
82fcde |
#define LLL_LOCK_INITIALIZER_LOCKED (1)
|
|
Packit Service |
82fcde |
#define LLL_LOCK_INITIALIZER_WAITERS (2)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* NB: in the lll_trylock macro we simply return the value in %eax
|
|
Packit Service |
82fcde |
after the cmpxchg instruction. In case the operation succeded this
|
|
Packit Service |
82fcde |
value is zero. In case the operation failed, the cmpxchg instruction
|
|
Packit Service |
82fcde |
has loaded the current value of the memory work which is guaranteed
|
|
Packit Service |
82fcde |
to be nonzero. */
|
|
Packit Service |
82fcde |
#if !IS_IN (libc) || defined UP
|
|
Packit Service |
82fcde |
# define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
# define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
|
|
Packit Service |
82fcde |
"je 0f\n\t" \
|
|
Packit Service |
82fcde |
"lock; cmpxchgl %2, %1\n\t" \
|
|
Packit Service |
82fcde |
"jmp 1f\n\t" \
|
|
Packit Service |
82fcde |
"0:\tcmpxchgl %2, %1\n\t" \
|
|
Packit Service |
82fcde |
"1:"
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_trylock(futex) \
|
|
Packit Service |
82fcde |
({ int ret; \
|
|
Packit Service |
82fcde |
__asm __volatile (__lll_trylock_asm \
|
|
Packit Service |
82fcde |
: "=a" (ret), "=m" (futex) \
|
|
Packit Service |
82fcde |
: "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
|
|
Packit Service |
82fcde |
"0" (LLL_LOCK_INITIALIZER) \
|
|
Packit Service |
82fcde |
: "memory"); \
|
|
Packit Service |
82fcde |
ret; })
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_cond_trylock(futex) \
|
|
Packit Service |
82fcde |
({ int ret; \
|
|
Packit Service |
82fcde |
__asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
|
|
Packit Service |
82fcde |
: "=a" (ret), "=m" (futex) \
|
|
Packit Service |
82fcde |
: "r" (LLL_LOCK_INITIALIZER_WAITERS), \
|
|
Packit Service |
82fcde |
"m" (futex), "0" (LLL_LOCK_INITIALIZER) \
|
|
Packit Service |
82fcde |
: "memory"); \
|
|
Packit Service |
82fcde |
ret; })
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#if !IS_IN (libc) || defined UP
|
|
Packit Service |
82fcde |
# define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
|
|
Packit Service |
82fcde |
"jz 24f\n\t"
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
# define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
|
|
Packit Service |
82fcde |
"je 0f\n\t" \
|
|
Packit Service |
82fcde |
"lock; cmpxchgl %4, %2\n\t" \
|
|
Packit Service |
82fcde |
"jnz 1f\n\t" \
|
|
Packit Service |
82fcde |
"jmp 24f\n" \
|
|
Packit Service |
82fcde |
"0:\tcmpxchgl %4, %2\n\t" \
|
|
Packit Service |
82fcde |
"jz 24f\n\t"
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_lock(futex, private) \
|
|
Packit Service |
82fcde |
(void) \
|
|
Packit Service |
82fcde |
({ int ignore1, ignore2, ignore3; \
|
|
Packit Service |
82fcde |
if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
|
|
Packit Service |
82fcde |
__asm __volatile (__lll_lock_asm_start \
|
|
Packit Service |
82fcde |
"1:\tlea %2, %%" RDI_LP "\n" \
|
|
Packit Service |
82fcde |
"2:\tsub $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset 128\n" \
|
|
Packit Service |
82fcde |
"3:\tcallq __lll_lock_wait_private\n" \
|
|
Packit Service |
82fcde |
"4:\tadd $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset -128\n" \
|
|
Packit Service |
82fcde |
"24:" \
|
|
Packit Service |
82fcde |
: "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
|
|
Packit Service |
82fcde |
"=a" (ignore3) \
|
|
Packit Service |
82fcde |
: "0" (1), "m" (futex), "3" (0) \
|
|
Packit Service |
82fcde |
: "cx", "r11", "cc", "memory"); \
|
|
Packit Service |
82fcde |
else \
|
|
Packit Service |
82fcde |
__asm __volatile (__lll_lock_asm_start \
|
|
Packit Service |
82fcde |
"1:\tlea %2, %%" RDI_LP "\n" \
|
|
Packit Service |
82fcde |
"2:\tsub $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset 128\n" \
|
|
Packit Service |
82fcde |
"3:\tcallq __lll_lock_wait\n" \
|
|
Packit Service |
82fcde |
"4:\tadd $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset -128\n" \
|
|
Packit Service |
82fcde |
"24:" \
|
|
Packit Service |
82fcde |
: "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
|
|
Packit Service |
82fcde |
"=a" (ignore3) \
|
|
Packit Service |
82fcde |
: "1" (1), "m" (futex), "3" (0), "0" (private) \
|
|
Packit Service |
82fcde |
: "cx", "r11", "cc", "memory"); \
|
|
Packit Service |
82fcde |
}) \
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_cond_lock(futex, private) \
|
|
Packit Service |
82fcde |
(void) \
|
|
Packit Service |
82fcde |
({ int ignore1, ignore2, ignore3; \
|
|
Packit Service |
82fcde |
__asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
|
|
Packit Service |
82fcde |
"jz 24f\n" \
|
|
Packit Service |
82fcde |
"1:\tlea %2, %%" RDI_LP "\n" \
|
|
Packit Service |
82fcde |
"2:\tsub $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset 128\n" \
|
|
Packit Service |
82fcde |
"3:\tcallq __lll_lock_wait\n" \
|
|
Packit Service |
82fcde |
"4:\tadd $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset -128\n" \
|
|
Packit Service |
82fcde |
"24:" \
|
|
Packit Service |
82fcde |
: "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
|
|
Packit Service |
82fcde |
"=a" (ignore3) \
|
|
Packit Service |
82fcde |
: "1" (2), "m" (futex), "3" (0), "0" (private) \
|
|
Packit Service |
82fcde |
: "cx", "r11", "cc", "memory"); \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_timedlock(futex, timeout, private) \
|
|
Packit Service |
82fcde |
({ int result, ignore1, ignore2, ignore3; \
|
|
Packit Service |
82fcde |
__asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
|
|
Packit Service |
82fcde |
"jz 24f\n" \
|
|
Packit Service |
82fcde |
"1:\tlea %4, %%" RDI_LP "\n" \
|
|
Packit Service |
82fcde |
"0:\tmov %8, %%" RDX_LP "\n" \
|
|
Packit Service |
82fcde |
"2:\tsub $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset 128\n" \
|
|
Packit Service |
82fcde |
"3:\tcallq __lll_timedlock_wait\n" \
|
|
Packit Service |
82fcde |
"4:\tadd $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset -128\n" \
|
|
Packit Service |
82fcde |
"24:" \
|
|
Packit Service |
82fcde |
: "=a" (result), "=D" (ignore1), "=S" (ignore2), \
|
|
Packit Service |
82fcde |
"=&d" (ignore3), "=m" (futex) \
|
|
Packit Service |
82fcde |
: "0" (0), "1" (1), "m" (futex), "m" (timeout), \
|
|
Packit Service |
82fcde |
"2" (private) \
|
|
Packit Service |
82fcde |
: "memory", "cx", "cc", "r10", "r11"); \
|
|
Packit Service |
82fcde |
result; })
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
extern int __lll_timedlock_elision (int *futex, short *adapt_count,
|
|
Packit Service |
82fcde |
const struct timespec *timeout,
|
|
Packit Service |
82fcde |
int private) attribute_hidden;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_timedlock_elision(futex, adapt_count, timeout, private) \
|
|
Packit Service |
82fcde |
__lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#if !IS_IN (libc) || defined UP
|
|
Packit Service |
82fcde |
# define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
|
|
Packit Service |
82fcde |
"je 24f\n\t"
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
# define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
|
|
Packit Service |
82fcde |
"je 0f\n\t" \
|
|
Packit Service |
82fcde |
"lock; decl %0\n\t" \
|
|
Packit Service |
82fcde |
"jne 1f\n\t" \
|
|
Packit Service |
82fcde |
"jmp 24f\n\t" \
|
|
Packit Service |
82fcde |
"0:\tdecl %0\n\t" \
|
|
Packit Service |
82fcde |
"je 24f\n\t"
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_unlock(futex, private) \
|
|
Packit Service |
82fcde |
(void) \
|
|
Packit Service |
82fcde |
({ int ignore; \
|
|
Packit Service |
82fcde |
if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
|
|
Packit Service |
82fcde |
__asm __volatile (__lll_unlock_asm_start \
|
|
Packit Service |
82fcde |
"1:\tlea %0, %%" RDI_LP "\n" \
|
|
Packit Service |
82fcde |
"2:\tsub $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset 128\n" \
|
|
Packit Service |
82fcde |
"3:\tcallq __lll_unlock_wake_private\n" \
|
|
Packit Service |
82fcde |
"4:\tadd $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset -128\n" \
|
|
Packit Service |
82fcde |
"24:" \
|
|
Packit Service |
82fcde |
: "=m" (futex), "=&D" (ignore) \
|
|
Packit Service |
82fcde |
: "m" (futex) \
|
|
Packit Service |
82fcde |
: "ax", "cx", "r11", "cc", "memory"); \
|
|
Packit Service |
82fcde |
else \
|
|
Packit Service |
82fcde |
__asm __volatile (__lll_unlock_asm_start \
|
|
Packit Service |
82fcde |
"1:\tlea %0, %%" RDI_LP "\n" \
|
|
Packit Service |
82fcde |
"2:\tsub $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset 128\n" \
|
|
Packit Service |
82fcde |
"3:\tcallq __lll_unlock_wake\n" \
|
|
Packit Service |
82fcde |
"4:\tadd $128, %%" RSP_LP "\n" \
|
|
Packit Service |
82fcde |
".cfi_adjust_cfa_offset -128\n" \
|
|
Packit Service |
82fcde |
"24:" \
|
|
Packit Service |
82fcde |
: "=m" (futex), "=&D" (ignore) \
|
|
Packit Service |
82fcde |
: "m" (futex), "S" (private) \
|
|
Packit Service |
82fcde |
: "ax", "cx", "r11", "cc", "memory"); \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_islocked(futex) \
|
|
Packit Service |
82fcde |
(futex != LLL_LOCK_INITIALIZER)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
|
|
Packit Service |
82fcde |
wake-up when the clone terminates. The memory location contains the
|
|
Packit Service |
82fcde |
thread ID while the clone is running and is reset to zero by the kernel
|
|
Packit Service |
82fcde |
afterwards. The kernel up to version 3.16.3 does not use the private futex
|
|
Packit Service |
82fcde |
operations for futex wake-up when the clone terminates. */
|
|
Packit Service |
82fcde |
#define lll_wait_tid(tid) \
|
|
Packit Service |
82fcde |
do { \
|
|
Packit Service |
82fcde |
__typeof (tid) __tid; \
|
|
Packit Service |
82fcde |
while ((__tid = (tid)) != 0) \
|
|
Packit Service |
82fcde |
lll_futex_wait (&(tid), __tid, LLL_SHARED);\
|
|
Packit Service |
82fcde |
} while (0)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
extern int __lll_timedwait_tid (int *, const struct timespec *)
|
|
Packit Service |
82fcde |
attribute_hidden;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* As lll_wait_tid, but with a timeout. If the timeout occurs then return
|
|
Packit Service |
82fcde |
ETIMEDOUT. If ABSTIME is invalid, return EINVAL.
|
|
Packit Service |
82fcde |
XXX Note that this differs from the generic version in that we do the
|
|
Packit Service |
82fcde |
error checking here and not in __lll_timedwait_tid. */
|
|
Packit Service |
82fcde |
#define lll_timedwait_tid(tid, abstime) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
int __result = 0; \
|
|
Packit Service |
82fcde |
if ((tid) != 0) \
|
|
Packit Service |
82fcde |
__result = __lll_timedwait_tid (&(tid), (abstime)); \
|
|
Packit Service |
82fcde |
__result; })
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
|
|
Packit Service |
82fcde |
attribute_hidden;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
extern int __lll_unlock_elision (int *lock, int private)
|
|
Packit Service |
82fcde |
attribute_hidden;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
extern int __lll_trylock_elision (int *lock, short *adapt_count)
|
|
Packit Service |
82fcde |
attribute_hidden;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define lll_lock_elision(futex, adapt_count, private) \
|
|
Packit Service |
82fcde |
__lll_lock_elision (&(futex), &(adapt_count), private)
|
|
Packit Service |
82fcde |
#define lll_unlock_elision(futex, adapt_count, private) \
|
|
Packit Service |
82fcde |
__lll_unlock_elision (&(futex), private)
|
|
Packit Service |
82fcde |
#define lll_trylock_elision(futex, adapt_count) \
|
|
Packit Service |
82fcde |
__lll_trylock_elision (&(futex), &(adapt_count))
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#endif /* !__ASSEMBLER__ */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#endif /* lowlevellock.h */
|