| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* libc-internal interface for mutex locks.  NPTL 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 License as | 
|  | 7 | published by the Free Software Foundation; either version 2.1 of the | 
|  | 8 | 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; see the file COPYING.LIB.  If | 
|  | 17 | not, see <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #ifndef _LIBC_LOCK_H | 
|  | 20 | #define _LIBC_LOCK_H 1 | 
|  | 21 |  | 
|  | 22 | #include <pthread.h> | 
|  | 23 | #define __need_NULL | 
|  | 24 | #include <stddef.h> | 
|  | 25 |  | 
|  | 26 |  | 
|  | 27 | /* Mutex type.  */ | 
|  | 28 | #if defined _LIBC || defined _IO_MTSAFE_IO | 
|  | 29 | # if (!IS_IN (libc) && !IS_IN (libpthread)) || !defined _LIBC | 
|  | 30 | typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t; | 
|  | 31 | # else | 
|  | 32 | typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t; | 
|  | 33 | # endif | 
|  | 34 | #else | 
|  | 35 | typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t; | 
|  | 36 | #endif | 
|  | 37 |  | 
|  | 38 | /* Define a lock variable NAME with storage class CLASS.  The lock must be | 
|  | 39 | initialized with __libc_lock_init before it can be used (or define it | 
|  | 40 | with __libc_lock_define_initialized, below).  Use `extern' for CLASS to | 
|  | 41 | declare a lock defined in another module.  In public structure | 
|  | 42 | definitions you must use a pointer to the lock structure (i.e., NAME | 
|  | 43 | begins with a `*'), because its storage size will not be known outside | 
|  | 44 | of libc.  */ | 
|  | 45 | #define __libc_lock_define_recursive(CLASS,NAME) \ | 
|  | 46 | CLASS __libc_lock_recursive_t NAME; | 
|  | 47 |  | 
|  | 48 | /* Define an initialized recursive lock variable NAME with storage | 
|  | 49 | class CLASS.  */ | 
|  | 50 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) | 
|  | 51 | # define __libc_lock_define_initialized_recursive(CLASS, NAME) \ | 
|  | 52 | CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER; | 
|  | 53 | # define _LIBC_LOCK_RECURSIVE_INITIALIZER \ | 
|  | 54 | { LLL_LOCK_INITIALIZER, 0, NULL } | 
|  | 55 | #else | 
|  | 56 | # define __libc_lock_define_initialized_recursive(CLASS,NAME) \ | 
|  | 57 | CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER; | 
|  | 58 | # define _LIBC_LOCK_RECURSIVE_INITIALIZER \ | 
|  | 59 | {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP} | 
|  | 60 | #endif | 
|  | 61 |  | 
|  | 62 | /* Initialize a recursive mutex.  */ | 
|  | 63 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) | 
|  | 64 | # define __libc_lock_init_recursive(NAME) \ | 
|  | 65 | ((void) ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER)) | 
|  | 66 | #else | 
|  | 67 | # define __libc_lock_init_recursive(NAME) \ | 
|  | 68 | do {									      \ | 
|  | 69 | if (__pthread_mutex_init != NULL)					      \ | 
|  | 70 | {									      \ | 
|  | 71 | pthread_mutexattr_t __attr;					      \ | 
|  | 72 | __pthread_mutexattr_init (&__attr);				      \ | 
|  | 73 | __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP);    \ | 
|  | 74 | __pthread_mutex_init (&(NAME).mutex, &__attr);			      \ | 
|  | 75 | __pthread_mutexattr_destroy (&__attr);				      \ | 
|  | 76 | }									      \ | 
|  | 77 | } while (0) | 
|  | 78 | #endif | 
|  | 79 |  | 
|  | 80 | /* Finalize recursive named lock.  */ | 
|  | 81 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) | 
|  | 82 | # define __libc_lock_fini_recursive(NAME) ((void) 0) | 
|  | 83 | #else | 
|  | 84 | # define __libc_lock_fini_recursive(NAME) \ | 
|  | 85 | __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0) | 
|  | 86 | #endif | 
|  | 87 |  | 
|  | 88 | /* Lock the recursive named lock variable.  */ | 
|  | 89 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) | 
|  | 90 | # define __libc_lock_lock_recursive(NAME) \ | 
|  | 91 | do {									      \ | 
|  | 92 | void *self = THREAD_SELF;						      \ | 
|  | 93 | if ((NAME).owner != self)						      \ | 
|  | 94 | {									      \ | 
|  | 95 | lll_lock ((NAME).lock, LLL_PRIVATE);				      \ | 
|  | 96 | (NAME).owner = self;						      \ | 
|  | 97 | }									      \ | 
|  | 98 | ++(NAME).cnt;							      \ | 
|  | 99 | } while (0) | 
|  | 100 | #else | 
|  | 101 | # define __libc_lock_lock_recursive(NAME) \ | 
|  | 102 | __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0) | 
|  | 103 | #endif | 
|  | 104 |  | 
|  | 105 | /* Try to lock the recursive named lock variable.  */ | 
|  | 106 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) | 
|  | 107 | # define __libc_lock_trylock_recursive(NAME) \ | 
|  | 108 | ({									      \ | 
|  | 109 | int result = 0;							      \ | 
|  | 110 | void *self = THREAD_SELF;						      \ | 
|  | 111 | if ((NAME).owner != self)						      \ | 
|  | 112 | {									      \ | 
|  | 113 | if (lll_trylock ((NAME).lock) == 0)				      \ | 
|  | 114 | {								      \ | 
|  | 115 | (NAME).owner = self;					      \ | 
|  | 116 | (NAME).cnt = 1;						      \ | 
|  | 117 | }								      \ | 
|  | 118 | else								      \ | 
|  | 119 | result = EBUSY;						      \ | 
|  | 120 | }									      \ | 
|  | 121 | else								      \ | 
|  | 122 | ++(NAME).cnt;							      \ | 
|  | 123 | result;								      \ | 
|  | 124 | }) | 
|  | 125 | #else | 
|  | 126 | # define __libc_lock_trylock_recursive(NAME) \ | 
|  | 127 | __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0) | 
|  | 128 | #endif | 
|  | 129 |  | 
|  | 130 | /* Unlock the recursive named lock variable.  */ | 
|  | 131 | #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread)) | 
|  | 132 | /* We do no error checking here.  */ | 
|  | 133 | # define __libc_lock_unlock_recursive(NAME) \ | 
|  | 134 | do {									      \ | 
|  | 135 | if (--(NAME).cnt == 0)						      \ | 
|  | 136 | {									      \ | 
|  | 137 | (NAME).owner = NULL;						      \ | 
|  | 138 | lll_unlock ((NAME).lock, LLL_PRIVATE);				      \ | 
|  | 139 | }									      \ | 
|  | 140 | } while (0) | 
|  | 141 | #else | 
|  | 142 | # define __libc_lock_unlock_recursive(NAME) \ | 
|  | 143 | __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0) | 
|  | 144 | #endif | 
|  | 145 |  | 
|  | 146 | /* Note that for I/O cleanup handling we are using the old-style | 
|  | 147 | cancel handling.  It does not have to be integrated with C++ since | 
|  | 148 | no C++ code is called in the middle.  The old-style handling is | 
|  | 149 | faster and the support is not going away.  */ | 
|  | 150 | extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, | 
|  | 151 | void (*routine) (void *), void *arg); | 
|  | 152 | extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer, | 
|  | 153 | int execute); | 
|  | 154 |  | 
|  | 155 | /* Start critical region with cleanup.  */ | 
|  | 156 | #define __libc_cleanup_region_start(DOIT, FCT, ARG) \ | 
|  | 157 | { struct _pthread_cleanup_buffer _buffer;				      \ | 
|  | 158 | int _avail;								      \ | 
|  | 159 | if (DOIT) {								      \ | 
|  | 160 | _avail = PTFAVAIL (_pthread_cleanup_push_defer);			      \ | 
|  | 161 | if (_avail) {							      \ | 
|  | 162 | __libc_ptf_call_always (_pthread_cleanup_push_defer, (&_buffer, FCT,  \ | 
|  | 163 | ARG));	      \ | 
|  | 164 | } else {								      \ | 
|  | 165 | _buffer.__routine = (FCT);					      \ | 
|  | 166 | _buffer.__arg = (ARG);						      \ | 
|  | 167 | }									      \ | 
|  | 168 | } else {								      \ | 
|  | 169 | _avail = 0;							      \ | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | /* End critical region with cleanup.  */ | 
|  | 173 | #define __libc_cleanup_region_end(DOIT) \ | 
|  | 174 | if (_avail) {							      \ | 
|  | 175 | __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\ | 
|  | 176 | } else if (DOIT)							      \ | 
|  | 177 | _buffer.__routine (_buffer.__arg);				      \ | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 |  | 
|  | 181 | /* Hide the definitions which are only supposed to be used inside libc in | 
|  | 182 | a separate file.  This file is not present in the installation!  */ | 
|  | 183 | #ifdef _LIBC | 
|  | 184 | # include "libc-lockP.h" | 
|  | 185 | #endif | 
|  | 186 |  | 
|  | 187 | #endif	/* libc-lock.h */ |