]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Fix macros that were not properly surrounded by parens or braces.
[postgresql] / src / include / storage / s_lock.h
1 /*-------------------------------------------------------------------------
2  *
3  * s_lock.h--
4  *         This file contains the implementation (if any) for spinlocks.
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.35 1998/06/15 18:40:04 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 /*
15  *       DESCRIPTION
16  *              The public functions that must be provided are:
17  *
18  *              void S_INIT_LOCK(slock_t *lock)
19  *
20  *              void S_LOCK(slock_t *lock)
21  *
22  *              void S_UNLOCK(slock_t *lock)
23  *
24  *              void S_LOCK_FREE(slock_t *lock)
25  *                      Tests if the lock is free. Returns non-zero if free, 0 if locked.
26  *
27  *              The S_LOCK() macro      implements a primitive but still useful random
28  *              backoff to avoid hordes of busywaiting lockers chewing CPU.
29  *
30  *              Effectively:
31  *              void
32  *              S_LOCK(slock_t *lock)
33  *              {
34  *                      while (TAS(lock))
35  *                      {
36  *                      // back off the cpu for a semi-random short time
37  *                      }
38  *              }
39  *
40  *              This implementation takes advantage of a tas function written
41  *              (in assembly language) on machines that have a native test-and-set
42  *              instruction. Alternative mutex implementations may also be used.
43  *              This function is hidden under the TAS macro to allow substitutions.
44  *
45  *              #define TAS(lock) tas(lock)
46  *              int tas(slock_t *lock)          // True if lock already set
47  *
48  *              If none of this can be done, POSTGRES will default to using
49  *              System V semaphores (and take a large performance hit -- around 40%
50  *              of its time on a DS5000/240 is spent in semop(3)...).
51  *
52  *      NOTES
53  *              AIX has a test-and-set but the recommended interface is the cs(3)
54  *              system call.  This provides an 8-instruction (plus system call
55  *              overhead) uninterruptible compare-and-set operation.  True
56  *              spinlocks might be faster but using cs(3) still speeds up the
57  *              regression test suite by about 25%.  I don't have an assembler
58  *              manual for POWER in any case.
59  *
60  *              There are default implementations for all these macros at the bottom
61  *              of this file. Check if your platform can use these or needs to
62  *              override them.
63  *
64  */
65 #if !defined(S_LOCK_H)
66 #define S_LOCK_H
67
68 #include "storage/ipc.h"
69
70 #if defined(HAS_TEST_AND_SET)
71
72 #if defined(linux)
73 /***************************************************************************
74  * All Linux
75  */
76
77 #if defined(__alpha)
78
79 #define S_UNLOCK(lock) { __asm__("mb"); *(lock) = 0; }
80
81 #endif /* __alpha */
82
83
84
85
86 #else /* linux */
87 /***************************************************************************
88  * All non Linux
89  */
90
91 #if defined (nextstep)
92 /*
93  * NEXTSTEP (mach)
94  * slock_t is defined as a struct mutex.
95  */
96
97 #define S_LOCK(lock)    mutex_lock(lock)
98
99 #define S_UNLOCK(lock)  mutex_unlock(lock)
100
101 #define S_INIT_LOCK(lock)       mutex_init(lock)
102
103 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
104 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
105
106 #endif /* nextstep */
107
108
109
110 #if defined(__sgi)
111 /*
112  * SGI IRIX 5
113  * slock_t is defined as a struct abilock_t, which has a single unsigned long
114  * member.
115  *
116  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
117  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
118  * for the R3000 chips out there.
119  */
120 #define TAS(lock)       (!acquire_lock(lock))
121
122 #define S_UNLOCK(lock)  release_lock(lock)
123
124 #define S_INIT_LOCK(lock)       init_lock(lock)
125
126 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
127
128 #endif /* __sgi */
129
130
131
132 #if defined(__alpha)
133 /*
134  * OSF/1 (Alpha AXP)
135  *
136  * Note that slock_t on the Alpha AXP is msemaphore instead of char
137  * (see storage/ipc.h).
138  */
139 #define TAS(lock)       (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
140
141 #define S_UNLOCK(lock)  msem_unlock((lock), 0)
142
143 #define S_INIT_LOCK(lock)       msem_init((lock), MSEM_UNLOCKED)
144
145 #define S_LOCK_FREE(lock)       (!(lock)->msem_state)
146
147 #endif /* __alpha */
148
149
150
151 #if defined(_AIX)
152 /*
153  * AIX (POWER)
154  *
155  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
156  * (see storage/ipc.h).
157  */
158 #define TAS(lock)       cs((int *) (lock), 0, 1)
159
160 #endif /* _AIX */
161
162
163
164 #if defined(__hpux)
165 /*
166  * HP-UX (PA-RISC)
167  *
168  * Note that slock_t on PA-RISC is a structure instead of char
169  * (see storage/ipc.h).
170  *
171  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
172  * all words set to non-zero. tas() in tas.s
173  */
174 static slock_t clear_lock =
175 {-1, -1, -1, -1};
176
177 #define S_UNLOCK(lock)  (*(lock) = clear_lock)  /* struct assignment */
178
179 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
180
181 #endif /* __hpux */
182
183
184
185 #endif /* else defined(linux) */
186
187
188
189
190 /****************************************************************************
191  * Default Definitions - override these above as needed.
192  */
193
194 #if !defined(S_LOCK)
195
196 #include <sys/time.h>
197
198 #define S_NSPINCYCLE    16
199 #define S_MAX_BUSY              1000 * S_NSPINCYCLE
200
201 extern int      s_spincycle[];
202 extern void s_lock_stuck(slock_t *lock, char *file, int line);
203
204 #if defined(S_LOCK_DEBUG)
205
206 extern void s_lock(slock_t *lock);
207
208 #define S_LOCK(lock) s_lock(lock, __FILE__, __LINE__)
209
210 #else /* S_LOCK_DEBUG */
211
212 #define S_LOCK(lock) \
213 do { \
214         int spins = 0; \
215         while (TAS(lock)) \
216         { \
217                 struct timeval  delay; \
218                 delay.tv_sec = 0; \
219                 delay.tv_usec = s_spincycle[spins++ % S_NSPINCYCLE]; \
220                 (void) select(0, NULL, NULL, NULL, &delay); \
221                 if (spins > S_MAX_BUSY) \
222                 { \
223                         /* It's been well over a minute...  */ \
224                         s_lock_stuck(lock, __FILE__, __LINE__); \
225                 } \
226         } \
227 } while(0)
228
229 #endif /* S_LOCK_DEBUG */
230 #endif /* S_LOCK */
231
232
233
234 #if !defined(S_LOCK_FREE)
235 #define S_LOCK_FREE(lock)       ((*lock) == 0)
236 #endif /* S_LOCK_FREE */
237
238 #if !defined(S_UNLOCK)
239 #define S_UNLOCK(lock)          (*(lock) = 0)
240 #endif /* S_UNLOCK */
241
242 #if !defined(S_INIT_LOCK)
243 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
244 #endif /* S_INIT_LOCK */
245
246 #if !defined(TAS)
247 int                     tas(slock_t *lock); /* port/.../tas.s, or s_lock.c */
248
249 #define TAS(lock)               tas(lock)
250 #endif /* TAS */
251
252
253 #endif /* HAS_TEST_AND_SET */
254
255 #endif /* S_LOCK_H */