]> git.ipfire.org Git - thirdparty/glibc.git/blame - mach/lowlevellock.h
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / mach / lowlevellock.h
CommitLineData
fb4cc8a0 1/* Low-level lock implementation. Mach gsync-based version.
6d7e8eda 2 Copyright (C) 1994-2023 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
22#include <mach/gnumach.h>
23#include <atomic.h>
24
25/* Gsync flags. */
26#ifndef GSYNC_SHARED
79962d89
ST
27# define GSYNC_SHARED 0x01
28# define GSYNC_QUAD 0x02
29# define GSYNC_TIMED 0x04
30# define GSYNC_BROADCAST 0x08
31# define GSYNC_MUTATE 0x10
fb4cc8a0
AA
32#endif
33
34/* Static initializer for low-level locks. */
18c2ab9a 35#define LLL_LOCK_INITIALIZER 0
fb4cc8a0 36
644d98ec 37#define LLL_PRIVATE 0
33038a7d 38#define LLL_SHARED GSYNC_SHARED
644d98ec 39
59bb023c
ST
40/* Interruptible version of __gsync_wait. */
41extern kern_return_t __gsync_wait_intr
42(
43 mach_port_t task,
44 vm_offset_t addr,
45 unsigned val1,
46 unsigned val2,
47 natural_t msec,
48 int flags
49);
50
fb4cc8a0
AA
51/* Wait on address PTR, without blocking if its contents
52 * are different from VAL. */
bec41242 53#define __lll_wait(ptr, val, flags) \
fb4cc8a0
AA
54 __gsync_wait (__mach_task_self (), \
55 (vm_offset_t)(ptr), (val), 0, 0, (flags))
bec41242
ST
56#define lll_wait(var, val, flags) \
57 __lll_wait (&(var), val, flags)
fb4cc8a0 58
59bb023c
ST
59/* Interruptible version. */
60#define __lll_wait_intr(ptr, val, flags) \
61 __gsync_wait_intr (__mach_task_self (), \
62 (vm_offset_t)(ptr), (val), 0, 0, (flags))
63#define lll_wait_intr(var, val, flags) \
64 __lll_wait_intr ((&var), val, flags)
65
fb4cc8a0 66/* Wake one or more threads waiting on address PTR. */
bec41242 67#define __lll_wake(ptr, flags) \
fb4cc8a0 68 __gsync_wake (__mach_task_self (), (vm_offset_t)(ptr), 0, (flags))
bec41242
ST
69#define lll_wake(var, flags) \
70 __lll_wake (&(var), flags)
fb4cc8a0
AA
71
72/* Acquire the lock at PTR. */
bec41242 73#define __lll_lock(ptr, flags) \
fb4cc8a0
AA
74 ({ \
75 int *__iptr = (int *)(ptr); \
76 int __flags = (flags); \
34a5a146
JM
77 if (*__iptr != 0 \
78 || atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) != 0) \
fb4cc8a0
AA
79 while (1) \
80 { \
22f4ab2d 81 if (atomic_exchange_acquire (__iptr, 2) == 0) \
fb4cc8a0 82 break; \
bec41242 83 __lll_wait (__iptr, 2, __flags); \
fb4cc8a0
AA
84 } \
85 (void)0; \
86 })
bec41242
ST
87#define lll_lock(var, flags) \
88 __lll_lock (&(var), flags)
fb4cc8a0
AA
89
90/* Try to acquire the lock at PTR, without blocking.
91 Evaluates to zero on success. */
bec41242 92#define __lll_trylock(ptr) \
fb4cc8a0
AA
93 ({ \
94 int *__iptr = (int *)(ptr); \
34a5a146
JM
95 *__iptr == 0 \
96 && atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) == 0 ? 0 : -1; \
fb4cc8a0 97 })
bec41242
ST
98#define lll_trylock(var) \
99 __lll_trylock (&(var))
fb4cc8a0
AA
100
101/* Release the lock at PTR. */
bec41242 102#define __lll_unlock(ptr, flags) \
fb4cc8a0
AA
103 ({ \
104 int *__iptr = (int *)(ptr); \
22f4ab2d 105 if (atomic_exchange_release (__iptr, 0) == 2) \
bec41242 106 __lll_wake (__iptr, (flags)); \
fb4cc8a0
AA
107 (void)0; \
108 })
bec41242
ST
109#define lll_unlock(var, flags) \
110 __lll_unlock (&(var), flags)
fb4cc8a0
AA
111
112#endif