]> git.ipfire.org Git - thirdparty/glibc.git/blame - mach/lowlevellock.h
Benchtests: Remove broken walk benchmarks
[thirdparty/glibc.git] / mach / lowlevellock.h
CommitLineData
fb4cc8a0 1/* Low-level lock implementation. Mach gsync-based version.
dff8da6b 2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
fb4cc8a0
AA
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
fb4cc8a0
AA
18
19#ifndef _MACH_LOWLEVELLOCK_H
20#define _MACH_LOWLEVELLOCK_H 1
21
dac7c640 22#include <mach.h>
fb4cc8a0
AA
23#include <mach/gnumach.h>
24#include <atomic.h>
25
26/* Gsync flags. */
27#ifndef GSYNC_SHARED
79962d89
ST
28# define GSYNC_SHARED 0x01
29# define GSYNC_QUAD 0x02
30# define GSYNC_TIMED 0x04
31# define GSYNC_BROADCAST 0x08
32# define GSYNC_MUTATE 0x10
fb4cc8a0
AA
33#endif
34
35/* Static initializer for low-level locks. */
18c2ab9a 36#define LLL_LOCK_INITIALIZER 0
fb4cc8a0 37
644d98ec 38#define LLL_PRIVATE 0
33038a7d 39#define LLL_SHARED GSYNC_SHARED
644d98ec 40
59bb023c
ST
41/* Interruptible version of __gsync_wait. */
42extern kern_return_t __gsync_wait_intr
43(
44 mach_port_t task,
45 vm_offset_t addr,
46 unsigned val1,
47 unsigned val2,
48 natural_t msec,
49 int flags
50);
51
fb4cc8a0
AA
52/* Wait on address PTR, without blocking if its contents
53 * are different from VAL. */
bec41242 54#define __lll_wait(ptr, val, flags) \
fb4cc8a0
AA
55 __gsync_wait (__mach_task_self (), \
56 (vm_offset_t)(ptr), (val), 0, 0, (flags))
bec41242
ST
57#define lll_wait(var, val, flags) \
58 __lll_wait (&(var), val, flags)
fb4cc8a0 59
59bb023c
ST
60/* Interruptible version. */
61#define __lll_wait_intr(ptr, val, flags) \
62 __gsync_wait_intr (__mach_task_self (), \
63 (vm_offset_t)(ptr), (val), 0, 0, (flags))
64#define lll_wait_intr(var, val, flags) \
65 __lll_wait_intr ((&var), val, flags)
66
fb4cc8a0 67/* Wake one or more threads waiting on address PTR. */
bec41242 68#define __lll_wake(ptr, flags) \
fb4cc8a0 69 __gsync_wake (__mach_task_self (), (vm_offset_t)(ptr), 0, (flags))
bec41242
ST
70#define lll_wake(var, flags) \
71 __lll_wake (&(var), flags)
fb4cc8a0
AA
72
73/* Acquire the lock at PTR. */
bec41242 74#define __lll_lock(ptr, flags) \
fb4cc8a0
AA
75 ({ \
76 int *__iptr = (int *)(ptr); \
77 int __flags = (flags); \
34a5a146
JM
78 if (*__iptr != 0 \
79 || atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) != 0) \
fb4cc8a0
AA
80 while (1) \
81 { \
22f4ab2d 82 if (atomic_exchange_acquire (__iptr, 2) == 0) \
fb4cc8a0 83 break; \
bec41242 84 __lll_wait (__iptr, 2, __flags); \
fb4cc8a0
AA
85 } \
86 (void)0; \
87 })
bec41242
ST
88#define lll_lock(var, flags) \
89 __lll_lock (&(var), flags)
fb4cc8a0
AA
90
91/* Try to acquire the lock at PTR, without blocking.
92 Evaluates to zero on success. */
bec41242 93#define __lll_trylock(ptr) \
fb4cc8a0
AA
94 ({ \
95 int *__iptr = (int *)(ptr); \
34a5a146
JM
96 *__iptr == 0 \
97 && atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) == 0 ? 0 : -1; \
fb4cc8a0 98 })
bec41242
ST
99#define lll_trylock(var) \
100 __lll_trylock (&(var))
fb4cc8a0
AA
101
102/* Release the lock at PTR. */
bec41242 103#define __lll_unlock(ptr, flags) \
fb4cc8a0
AA
104 ({ \
105 int *__iptr = (int *)(ptr); \
22f4ab2d 106 if (atomic_exchange_release (__iptr, 0) == 2) \
bec41242 107 __lll_wake (__iptr, (flags)); \
fb4cc8a0
AA
108 (void)0; \
109 })
bec41242
ST
110#define lll_unlock(var, flags) \
111 __lll_unlock (&(var), flags)
fb4cc8a0
AA
112
113#endif