1/* Low-level lock implementation. Mach gsync-based version.
2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
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
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _MACH_LOWLEVELLOCK_H
20#define _MACH_LOWLEVELLOCK_H 1
21
22#include <mach.h>
23#include <mach/gnumach.h>
24#include <atomic.h>
25
26/* Gsync flags. */
27#ifndef GSYNC_SHARED
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
33#endif
34
35/* Static initializer for low-level locks. */
36#define LLL_LOCK_INITIALIZER 0
37
38#define LLL_PRIVATE 0
39#define LLL_SHARED GSYNC_SHARED
40
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
52/* Wait on address PTR, without blocking if its contents
53 * are different from VAL. */
54#define __lll_wait(ptr, val, flags) \
55 __gsync_wait (__mach_task_self (), \
56 (vm_offset_t)(ptr), (val), 0, 0, (flags))
57#define lll_wait(var, val, flags) \
58 __lll_wait (&(var), val, flags)
59
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
67/* Wake one or more threads waiting on address PTR. */
68#define __lll_wake(ptr, flags) \
69 __gsync_wake (__mach_task_self (), (vm_offset_t)(ptr), 0, (flags))
70#define lll_wake(var, flags) \
71 __lll_wake (&(var), flags)
72
73/* Acquire the lock at PTR. */
74#define __lll_lock(ptr, flags) \
75 ({ \
76 int *__iptr = (int *)(ptr); \
77 int __flags = (flags); \
78 if (*__iptr != 0 \
79 || atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) != 0) \
80 while (1) \
81 { \
82 if (atomic_exchange_acquire (__iptr, 2) == 0) \
83 break; \
84 __lll_wait (__iptr, 2, __flags); \
85 } \
86 (void)0; \
87 })
88#define lll_lock(var, flags) \
89 __lll_lock (&(var), flags)
90
91/* Try to acquire the lock at PTR, without blocking.
92 Evaluates to zero on success. */
93#define __lll_trylock(ptr) \
94 ({ \
95 int *__iptr = (int *)(ptr); \
96 *__iptr == 0 \
97 && atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) == 0 ? 0 : -1; \
98 })
99#define lll_trylock(var) \
100 __lll_trylock (&(var))
101
102/* Release the lock at PTR. */
103#define __lll_unlock(ptr, flags) \
104 ({ \
105 int *__iptr = (int *)(ptr); \
106 if (atomic_exchange_release (__iptr, 0) == 2) \
107 __lll_wake (__iptr, (flags)); \
108 (void)0; \
109 })
110#define lll_unlock(var, flags) \
111 __lll_unlock (&(var), flags)
112
113#endif
114

source code of glibc/mach/lowlevellock.h