1 /* libc-internal interface for mutex locks. NPTL version. 2 Copyright (C) 1996-2015 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 _BITS_LIBC_LOCK_H 20 #define _BITS_LIBC_LOCK_H 1 21 22 #include <pthread.h> 23 #define __need_NULL 24 #include <stddef.h> 25 26 #define __libc_maybe_call(func, args, else) func args 27 #define __pthread_mutex_init pthread_mutex_init 28 #define __pthread_mutex_destroy pthread_mutex_destroy 29 #define __pthread_mutexattr_init pthread_mutexattr_init 30 #define __pthread_mutexattr_destroy pthread_mutexattr_destroy 31 #define __pthread_mutexattr_settype pthread_mutexattr_settype 32 #define __pthread_mutex_lock pthread_mutex_lock 33 #define __pthread_mutex_unlock pthread_mutex_unlock 34 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER 35 #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE 36 37 /* Mutex type. */ 38 typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t; 39 40 /* Define a lock variable NAME with storage class CLASS. The lock must be 41 initialized with __libc_lock_init before it can be used (or define it 42 with __libc_lock_define_initialized, below). Use `extern' for CLASS to 43 declare a lock defined in another module. In public structure 44 definitions you must use a pointer to the lock structure (i.e., NAME 45 begins with a `*'), because its storage size will not be known outside 46 of libc. */ 47 #define __libc_lock_define_recursive(CLASS,NAME) \ 48 CLASS __libc_lock_recursive_t NAME; 49 50 /* Define an initialized recursive lock variable NAME with storage 51 class CLASS. */ 52 # define __libc_lock_define_initialized_recursive(CLASS,NAME) \ 53 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER; 54 # define _LIBC_LOCK_RECURSIVE_INITIALIZER \ 55 {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP} 56 57 /* Initialize a recursive mutex. */ 58 # define __libc_lock_init_recursive(NAME) \ 59 do { \ 60 pthread_mutexattr_t __attr; \ 61 __pthread_mutexattr_init (&__attr); \ 62 __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \ 63 __pthread_mutex_init (&(NAME).mutex, &__attr); \ 64 __pthread_mutexattr_destroy (&__attr); \ 65 } while (0) 66 67 /* Finalize recursive named lock. */ 68 # define __libc_lock_fini_recursive(NAME) \ 69 __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0) 70 71 /* Lock the recursive named lock variable. */ 72 # define __libc_lock_lock_recursive(NAME) \ 73 __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0) 74 75 /* Try to lock the recursive named lock variable. */ 76 # define __libc_lock_trylock_recursive(NAME) \ 77 __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0) 78 79 /* Unlock the recursive named lock variable. */ 80 # define __libc_lock_unlock_recursive(NAME) \ 81 __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0) 82 83 /* Start critical region with cleanup. */ 84 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \ 85 86 /* End critical region with cleanup. */ 87 #define __libc_cleanup_region_end(DOIT) \ 88 89 /* Sometimes we have to exit the block in the middle. */ 90 #define __libc_cleanup_end(DOIT) \ 91 92 /* Create thread-specific key. */ 93 #define __libc_key_create(KEY, DESTRUCTOR) \ 94 1 95 96 /* Get thread-specific data. */ 97 #define __libc_getspecific(KEY) \ 98 0 99 100 /* Set thread-specific data. */ 101 #define __libc_setspecific(KEY, VALUE) \ 102 0 103 104 105 /* Register handlers to execute before and after `fork'. */ 106 #define __libc_atfork(PREPARE, PARENT, CHILD) \ 107 0 108 109 #endif /* bits/libc-lock.h */ 110