1 /* libc-internal interface for mutex locks. LinuxThreads version.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 #define _LIBC_LOCK_H 1
24 #define __libc_lock_t pthread_mutex_t
26 /* Define a lock variable NAME with storage class CLASS. The lock must be
27 initialized with __libc_lock_init before it can be used (or define it
28 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
29 declare a lock defined in another module. In public structure
30 definitions you must use a pointer to the lock structure (i.e., NAME
31 begins with a `*'), because its storage size will not be known outside
33 #define __libc_lock_define(CLASS,NAME) \
34 CLASS __libc_lock_t NAME;
36 /* Define an initialized lock variable NAME with storage class CLASS. */
37 #define __libc_lock_define_initialized(CLASS,NAME) \
38 CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
40 /* Initialize the named lock variable, leaving it in a consistent, unlocked
42 #define __libc_lock_init(NAME) \
43 (__pthread_mutex_init != NULL ? __pthread_mutex_init (&(NAME), NULL) : 0);
45 /* Same as last but this time we initialize a recursive mutex. */
46 #define __libc_lock_init_recursive(NAME) \
48 if (__pthread_mutex_init != NULL) \
50 pthread_mutexattr_t __attr; \
51 __pthread_mutexattr_init (&__attr); \
52 __pthread_mutexattr_setkind_np (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
53 __pthread_mutex_init (&(NAME), &__attr); \
54 __pthread_mutexattr_destroy (&__attr); \
58 /* Finalize the named lock variable, which must be locked. It cannot be
59 used again until __libc_lock_init is called again on it. This must be
60 called on a lock variable before the containing storage is reused. */
61 #define __libc_lock_fini(NAME) \
62 (__pthread_mutex_destroy != NULL ? __pthread_mutex_destroy (&(NAME)) : 0);
64 /* Lock the named lock variable. */
65 #define __libc_lock_lock(NAME) \
66 (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (&(NAME)) : 0);
68 /* Try to lock the named lock variable. */
69 #define __libc_lock_trylock(NAME) \
70 (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (&(NAME)) : 0);
72 /* Unlock the named lock variable. */
73 #define __libc_lock_unlock(NAME) \
74 (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (&(NAME)) : 0);
76 /* Start critical region with cleanup. */
77 #define __libc_cleanup_region_start(FCT, ARG) \
78 { struct _pthread_cleanup_buffer _buffer; \
79 if (_pthread_cleanup_push_defer != NULL) { \
80 _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
83 /* End critical region with cleanup. */
84 #define __libc_cleanup_region_end(DOIT) \
85 if (_pthread_cleanup_push_defer != NULL) { \
86 _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
90 /* Make the pthread functions weak so that we can elide them from
91 single-threaded processes. */
92 #pragma weak __pthread_mutex_init
93 #pragma weak __pthread_mutex_destroy
94 #pragma weak __pthread_mutex_lock
95 #pragma weak __pthread_mutex_trylock
96 #pragma weak __pthread_mutex_unlock
97 #pragma weak __pthread_mutexattr_init
98 #pragma weak __pthread_mutexattr_destroy
99 #pragma weak __pthread_mutexattr_setkind_np
100 #pragma weak __pthread_key_create
101 #pragma weak __pthread_setspecific
102 #pragma weak __pthread_getspecific
103 #pragma weak __pthread_initialize
104 #pragma weak _pthread_cleanup_push_defer
105 #pragma weak _pthread_cleanup_pop_restore
107 /* We need portable names for some functions. E.g., when they are
108 used as argument to __libc_cleanup_region_start. */
109 #define __libc_mutex_unlock __pthread_mutex_unlock
111 #endif /* libc-lock.h */