debug_lock.h
1 /*
2  shm-arena shared memory arena
3  Copyright (C) 2006-2008 Lance Arsenault (LGPL v3)
4 
5 
6  This file is part of shm-arena.
7 
8  shm-arena is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12 
13  shm-arena is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this program. If not, see
20  <http://www.gnu.org/licenses/>.
21 */
22 
36 #ifdef WITH_SPEW
37 # define IF_SPEW(...) __VA_ARGS__
38 #else
39 # define IF_SPEW(...) /* empty macro, WITH_SPEW not defined */
40 #endif
41 
42 
43 
44 
45 
46 #ifdef SHM_DEBUG
47 
48 
49 
50 /* Turn spew on or off just in this file by selecting one of these two
51  * set of defines. LSPEW stands for Lock SPEW. MSPEW stands to My
52  * SPEW. Okay, I drew a blank. */
53 
54 #if 0 /* 1 for spew on, 0 for spew off */
55 # define LSPEW(level, fmt, ...) SPEW_RET(0, level, fmt, ##__VA_ARGS__ )
56 # define MSPEW(level, fmt, ...) SPEW(level, fmt, ##__VA_ARGS__ )
57 #else
58 # define LSPEW(level, fmt, ...) (0) /* no spew */
59 # define MSPEW(level, fmt, ...) /* empty macro, no spew */
60 #endif
61 
62 
63 
64 /* Set PRETEND_LOCK to use pretend read-write locks and mutexes which
65  * do not block the caller like real ones do. This is for shm-arena
66  * development only. */
67 #if 0
68 # define PRETEND_LOCK
69 #endif
70 
71 static inline
72 int shm_rwlock_rdlock(pthread_rwlock_t *rwlock)
73 {
74  int ret;
75  MSPEW(_DEBUG, "before rwlock_rdlock");
76  ret = pthread_rwlock_rdlock(rwlock);
77  MSPEW(_DEBUG, " after rwlock_rdlock");
78  ASSERT(!ret);
79  return ret;
80 }
81 
82 static inline
83 int shm_rwlock_wrlock(pthread_rwlock_t *rwlock)
84 {
85  int ret;
86  MSPEW(_DEBUG, "before rwlock_wrlock");
87  ret = pthread_rwlock_wrlock(rwlock);
88  MSPEW(_DEBUG, " after rwlock_wrlock");
89  ASSERT(!ret);
90  return ret;
91 }
92 
93 static inline
94 int shm_rwlock_unlock(pthread_rwlock_t *rwlock)
95 {
96  int ret;
97  MSPEW(_DEBUG, "before rwlock_unlock");
98  ret = pthread_rwlock_unlock(rwlock);
99  MSPEW(_DEBUG, " after rwlock_unlock");
100  ASSERT(!ret);
101  return ret;
102 }
103 
104 static inline
105 int shm_mutex_lock(pthread_mutex_t *mutex)
106 {
107  int ret;
108  MSPEW(_DEBUG, "before mutex_lock");
109  ret = pthread_mutex_lock(mutex);
110  MSPEW(_DEBUG, " after mutex_lock");
111  ASSERT(!ret);
112  return ret;
113 }
114 
115 static inline
116 int shm_mutex_unlock(pthread_mutex_t *mutex)
117 {
118  int ret;
119  MSPEW(_DEBUG, "before mutex_unlock");
120  ret = pthread_mutex_unlock(mutex);
121  MSPEW(_DEBUG, " after mutex_unlock");
122  ASSERT(!ret);
123  return ret;
124 }
125 
126 /* Replace the locking and unlocking pthread_*_*lock() calls with our
127  * wrappers, without doing anything but including this header file. */
128 
129 #ifdef PRETEND_LOCK
130 
131 
132 #define pthread_rwlock_rdlock(x) \
133  LSPEW(_DEBUG, "pthread_rwlock_rdlock(" #x ")")
134 
135 #define pthread_rwlock_wrlock(x) \
136  LSPEW(_DEBUG, "pthread_rwlock_wrlock(" #x ")")
137 
138 #define pthread_rwlock_unlock(x) \
139  LSPEW(_DEBUG, "pthread_rwlock_unlock(" #x ")")
140 
141 #define pthread_mutex_lock(x) \
142  LSPEW(_DEBUG, "pthread_mutex_lock(" #x ")")
143 
144 #define pthread_mutex_unlock(x) \
145  LSPEW(_DEBUG, "pthread_mutex_unlock(" #x ")")
146 
147 
148 #else /* #ifdef PRETEND_LOCK */
149 
150 
151 #define pthread_rwlock_rdlock(x) \
152  (LSPEW(_DEBUG, "pthread_rwlock_rdlock(" #x ")")+ \
153  shm_rwlock_rdlock((x)))
154 
155 #define pthread_rwlock_wrlock(x) \
156  (LSPEW(_DEBUG, "pthread_rwlock_wrlock(" #x ")")+ \
157  shm_rwlock_wrlock((x)))
158 
159 #define pthread_rwlock_unlock(x) \
160  (LSPEW(_DEBUG, "pthread_rwlock_unlock(" #x ")")+ \
161  shm_rwlock_unlock((x)))
162 
163 #define pthread_mutex_lock(x) \
164  (LSPEW(_DEBUG, "pthread_mutex_lock(" #x ")")+ \
165  shm_mutex_lock((x)))
166 
167 #define pthread_mutex_unlock(x) \
168  (LSPEW(_DEBUG, "pthread_mutex_unlock(" #x ")")+ \
169  shm_mutex_unlock((x)))
170 
171 
172 #endif /* #ifdef PRETEND_LOCK */
173 
174 
175 
176 #endif /* #ifdef SHM_DEBUG */
pthread_rwlock_t rwlock
Definition: arena.h:371

Shared Memory Arena version RC-0.0.25