xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* libc-internal interface for mutex locks. Mach cthreads version. |
| 2 | Copyright (C) 1996-2016 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 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _LIBC_LOCK_H |
| 20 | #define _LIBC_LOCK_H 1 |
| 21 | |
| 22 | #ifdef _LIBC |
| 23 | #include <cthreads.h> |
| 24 | #define __libc_lock_t struct mutex |
| 25 | #else |
| 26 | typedef struct __libc_lock_opaque__ __libc_lock_t; |
| 27 | #endif |
| 28 | |
| 29 | /* Type for key of thread specific data. */ |
| 30 | typedef cthread_key_t __libc_key_t; |
| 31 | |
| 32 | /* Define a lock variable NAME with storage class CLASS. The lock must be |
| 33 | initialized with __libc_lock_init before it can be used (or define it |
| 34 | with __libc_lock_define_initialized, below). Use `extern' for CLASS to |
| 35 | declare a lock defined in another module. In public structure |
| 36 | definitions you must use a pointer to the lock structure (i.e., NAME |
| 37 | begins with a `*'), because its storage size will not be known outside |
| 38 | of libc. */ |
| 39 | #define __libc_lock_define(CLASS,NAME) \ |
| 40 | CLASS __libc_lock_t NAME; |
| 41 | |
| 42 | /* Define an initialized lock variable NAME with storage class CLASS. */ |
| 43 | #define __libc_lock_define_initialized(CLASS,NAME) \ |
| 44 | CLASS __libc_lock_t NAME = MUTEX_INITIALIZER; |
| 45 | |
| 46 | /* Initialize the named lock variable, leaving it in a consistent, unlocked |
| 47 | state. */ |
| 48 | #define __libc_lock_init(NAME) __mutex_init (&(NAME)) |
| 49 | |
| 50 | /* Finalize the named lock variable, which must be locked. It cannot be |
| 51 | used again until __libc_lock_init is called again on it. This must be |
| 52 | called on a lock variable before the containing storage is reused. */ |
| 53 | #define __libc_lock_fini(NAME) __mutex_unlock (&(NAME)) |
| 54 | |
| 55 | /* Lock the named lock variable. */ |
| 56 | #define __libc_lock_lock(NAME) __mutex_lock (&(NAME)) |
| 57 | |
| 58 | /* Lock the named lock variable. */ |
| 59 | #define __libc_lock_trylock(NAME) (!__mutex_trylock (&(NAME))) |
| 60 | |
| 61 | /* Unlock the named lock variable. */ |
| 62 | #define __libc_lock_unlock(NAME) __mutex_unlock (&(NAME)) |
| 63 | |
| 64 | |
| 65 | /* XXX for now */ |
| 66 | #define __libc_rwlock_define __libc_lock_define |
| 67 | #define __libc_rwlock_define_initialized __libc_lock_define_initialized |
| 68 | #define __libc_rwlock_init __libc_lock_init |
| 69 | #define __libc_rwlock_fini __libc_lock_fini |
| 70 | #define __libc_rwlock_rdlock __libc_lock_lock |
| 71 | #define __libc_rwlock_wrlock __libc_lock_lock |
| 72 | #define __libc_rwlock_tryrdlock __libc_lock_trylock |
| 73 | #define __libc_rwlock_trywrlock __libc_lock_trylock |
| 74 | #define __libc_rwlock_unlock __libc_lock_unlock |
| 75 | |
| 76 | |
| 77 | /* Start a critical region with a cleanup function */ |
| 78 | #define __libc_cleanup_region_start(DOIT, FCT, ARG) \ |
| 79 | { \ |
| 80 | typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0; \ |
| 81 | typeof (ARG) __save_ARG = ARG; \ |
| 82 | /* close brace is in __libc_cleanup_region_end below. */ |
| 83 | |
| 84 | /* End a critical region started with __libc_cleanup_region_start. */ |
| 85 | #define __libc_cleanup_region_end(DOIT) \ |
| 86 | if ((DOIT) && __save_FCT != 0) \ |
| 87 | (*__save_FCT)(__save_ARG); \ |
| 88 | } |
| 89 | |
| 90 | /* Sometimes we have to exit the block in the middle. */ |
| 91 | #define __libc_cleanup_end(DOIT) \ |
| 92 | if ((DOIT) && __save_FCT != 0) \ |
| 93 | (*__save_FCT)(__save_ARG); \ |
| 94 | |
| 95 | |
| 96 | /* Use mutexes as once control variables. */ |
| 97 | |
| 98 | struct __libc_once |
| 99 | { |
| 100 | __libc_lock_t lock; |
| 101 | int done; |
| 102 | }; |
| 103 | |
| 104 | #define __libc_once_define(CLASS,NAME) \ |
| 105 | CLASS struct __libc_once NAME = { MUTEX_INITIALIZER, 0 } |
| 106 | |
| 107 | |
| 108 | /* Call handler iff the first call. */ |
| 109 | #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \ |
| 110 | do { \ |
| 111 | __libc_lock_lock (ONCE_CONTROL.lock); \ |
| 112 | if (!ONCE_CONTROL.done) \ |
| 113 | (INIT_FUNCTION) (); \ |
| 114 | ONCE_CONTROL.done = 1; \ |
| 115 | __libc_lock_unlock (ONCE_CONTROL.lock); \ |
| 116 | } while (0) |
| 117 | |
| 118 | /* Get once control variable. */ |
| 119 | #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL).done != 0) |
| 120 | |
| 121 | #ifdef _LIBC |
| 122 | /* We need portable names for some functions. E.g., when they are |
| 123 | used as argument to __libc_cleanup_region_start. */ |
| 124 | #define __libc_mutex_unlock __mutex_unlock |
| 125 | #endif |
| 126 | |
| 127 | #define __libc_key_create(KEY,DEST) cthread_keycreate (KEY) |
| 128 | #define __libc_setspecific(KEY,VAL) cthread_setspecific (KEY, VAL) |
| 129 | void *__libc_getspecific (__libc_key_t key); |
| 130 | |
| 131 | /* XXX until cthreads supports recursive locks */ |
| 132 | #define __libc_lock_define_initialized_recursive __libc_lock_define_initialized |
| 133 | #define __libc_lock_init_recursive __libc_lock_init |
| 134 | #define __libc_lock_fini_recursive __libc_lock_fini |
| 135 | #define __libc_lock_trylock_recursive __libc_lock_trylock |
| 136 | #define __libc_lock_unlock_recursive __libc_lock_unlock |
| 137 | #define __libc_lock_lock_recursive __libc_lock_lock |
| 138 | |
| 139 | #define __rtld_lock_define_initialized_recursive __libc_lock_define_initialized |
| 140 | #define __rtld_lock_fini_recursive __libc_lock_fini |
| 141 | #define __rtld_lock_trylock_recursive __libc_lock_trylock |
| 142 | #define __rtld_lock_unlock_recursive __libc_lock_unlock |
| 143 | #define __rtld_lock_lock_recursive __libc_lock_lock |
| 144 | |
| 145 | #endif /* libc-lock.h */ |