]>
Commit | Line | Data |
---|---|---|
fb4cc8a0 | 1 | /* Low-level lock implementation. Mach gsync-based version. |
55325314 | 2 | Copyright (C) 1994-2018 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 | |
17 | <http://www.gnu.org/licenses/>. */ | |
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 | |
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 | |
32 | #endif | |
33 | ||
34 | /* Static initializer for low-level locks. */ | |
35 | #define LLL_INITIALIZER 0 | |
36 | ||
37 | /* Wait on address PTR, without blocking if its contents | |
38 | * are different from VAL. */ | |
39 | #define lll_wait(ptr, val, flags) \ | |
40 | __gsync_wait (__mach_task_self (), \ | |
41 | (vm_offset_t)(ptr), (val), 0, 0, (flags)) | |
42 | ||
43 | /* Wake one or more threads waiting on address PTR. */ | |
44 | #define lll_wake(ptr, flags) \ | |
45 | __gsync_wake (__mach_task_self (), (vm_offset_t)(ptr), 0, (flags)) | |
46 | ||
47 | /* Acquire the lock at PTR. */ | |
48 | #define lll_lock(ptr, flags) \ | |
49 | ({ \ | |
50 | int *__iptr = (int *)(ptr); \ | |
51 | int __flags = (flags); \ | |
52 | if (*__iptr != 0 || \ | |
53 | atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) != 0) \ | |
54 | while (1) \ | |
55 | { \ | |
56 | if (atomic_exchange_acq (__iptr, 2) == 0) \ | |
57 | break; \ | |
58 | lll_wait (__iptr, 2, __flags); \ | |
59 | } \ | |
60 | (void)0; \ | |
61 | }) | |
62 | ||
63 | /* Try to acquire the lock at PTR, without blocking. | |
64 | Evaluates to zero on success. */ | |
65 | #define lll_trylock(ptr) \ | |
66 | ({ \ | |
67 | int *__iptr = (int *)(ptr); \ | |
68 | *__iptr == 0 && \ | |
69 | atomic_compare_and_exchange_bool_acq (__iptr, 1, 0) == 0 ? 0 : -1; \ | |
70 | }) | |
71 | ||
72 | /* Release the lock at PTR. */ | |
73 | #define lll_unlock(ptr, flags) \ | |
74 | ({ \ | |
75 | int *__iptr = (int *)(ptr); \ | |
76 | if (atomic_exchange_rel (__iptr, 0) == 2) \ | |
77 | lll_wake (__iptr, (flags)); \ | |
78 | (void)0; \ | |
79 | }) | |
80 | ||
81 | #endif |