]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Ye-old pgindent run. Same 4-space tabs.
[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  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.70 2000/04/12 17:16:51 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 /*
17  *       DESCRIPTION
18  *              The public macros that must be provided are:
19  *
20  *              void S_INIT_LOCK(slock_t *lock)
21  *
22  *              void S_LOCK(slock_t *lock)
23  *
24  *              void S_UNLOCK(slock_t *lock)
25  *
26  *              void S_LOCK_FREE(slock_t *lock)
27  *                      Tests if the lock is free. Returns non-zero if free, 0 if locked.
28  *
29  *              The S_LOCK() macro      implements a primitive but still useful random
30  *              backoff to avoid hordes of busywaiting lockers chewing CPU.
31  *
32  *              Effectively:
33  *              void
34  *              S_LOCK(slock_t *lock)
35  *              {
36  *                      while (TAS(lock))
37  *                      {
38  *                      // back off the cpu for a semi-random short time
39  *                      }
40  *              }
41  *
42  *              This implementation takes advantage of a tas function written
43  *              (in assembly language) on machines that have a native test-and-set
44  *              instruction. Alternative mutex implementations may also be used.
45  *              This function is hidden under the TAS macro to allow substitutions.
46  *
47  *              #define TAS(lock) tas(lock)
48  *              int tas(slock_t *lock)          // True if lock already set
49  *
50  *              There are default implementations for all these macros at the bottom
51  *              of this file. Check if your platform can use these or needs to
52  *              override them.
53  *
54  *      NOTES
55  *              If none of this can be done, POSTGRES will default to using
56  *              System V semaphores (and take a large performance hit -- around 40%
57  *              of its time on a DS5000/240 is spent in semop(3)...).
58  *
59  *              AIX has a test-and-set but the recommended interface is the cs(3)
60  *              system call.  This provides an 8-instruction (plus system call
61  *              overhead) uninterruptible compare-and-set operation.  True
62  *              spinlocks might be faster but using cs(3) still speeds up the
63  *              regression test suite by about 25%.  I don't have an assembler
64  *              manual for POWER in any case.
65  *
66  */
67 #if !defined(S_LOCK_H)
68 #define S_LOCK_H
69
70 #include "storage/ipc.h"
71
72 extern void s_lock_sleep(unsigned spin);
73
74 #if defined(HAS_TEST_AND_SET)
75
76
77 #if defined(__GNUC__)
78 /*************************************************************************
79  * All the gcc inlines
80  */
81
82
83 #if defined(__i386__)
84 #define TAS(lock) tas(lock)
85
86 static __inline__ int
87 tas(volatile slock_t *lock)
88 {
89         register slock_t _res = 1;
90
91 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
92         return (int) _res;
93 }
94
95 #endif   /* __i386__ */
96
97
98
99 #if defined(__arm__) || defined(__arm__)
100 #define TAS(lock) tas(lock)
101
102 static __inline__ int
103 tas(volatile slock_t *lock)
104 {
105         register slock_t _res = 1;
106
107 __asm__("swpb %0, %0, [%3]": "=r"(_res), "=m"(*lock):"0"(_res), "r" (lock));
108         return (int) _res;
109 }
110
111 #endif   /* __arm__ */
112
113
114
115 #if defined(__sparc__)
116 #define TAS(lock) tas(lock)
117
118 static __inline__ int
119 tas(volatile slock_t *lock)
120 {
121         register slock_t _res = 1;
122
123         __asm__("ldstub [%2], %0" \
124 :                       "=r"(_res), "=m"(*lock) \
125 :                       "r"(lock));
126         return (int) _res;
127 }
128
129 #endif   /* __sparc__ */
130
131
132 #if defined(__mc68000__) && defined(__linux__)
133 #define TAS(lock) tas(lock)
134
135 static __inline__ int
136 tas(volatile slock_t *lock)
137 {
138         register int rv;
139         
140         __asm__ __volatile__ (
141                 "tas %1; sne %0"
142                 : "=d" (rv), "=m"(*lock)
143                 : "1" (*lock)
144                 : "cc" );
145         return rv;
146 }
147
148 #endif /* defined(__mc68000__) && defined(__linux__) */
149
150
151 #if defined(NEED_VAX_TAS_ASM)
152 /*
153  * VAXen -- even multiprocessor ones
154  * (thanks to Tom Ivar Helbekkmo)
155  */
156 #define TAS(lock) tas(lock)
157
158 typedef unsigned char slock_t;
159
160 static __inline__ int
161 tas(volatile slock_t *lock)
162 {
163         register        _res;
164
165         __asm__("       movl $1, r0 \
166                         bbssi $0, (%1), 1 f \
167                         clrl r0 \
168 1:                      movl r0, %0 "
169 :                       "=r"(_res)                      /* return value, in register */
170 :                       "r"(lock)                       /* argument, 'lock pointer', in register */
171 :                       "r0");                          /* inline code uses this register */
172         return (int) _res;
173 }
174
175 #endif   /* NEED_VAX_TAS_ASM */
176
177
178
179 #if defined(NEED_NS32K_TAS_ASM)
180 #define TAS(lock) tas(lock)
181
182 static __inline__ int
183 tas(volatile slock_t *lock)
184 {
185   register _res;
186   __asm__("sbitb 0, %0 \n\
187         sfsd %1"
188         : "=m"(*lock), "=r"(_res));
189   return (int) _res; 
190 }
191
192 #endif  /* NEED_NS32K_TAS_ASM */
193
194
195
196 #else                                                   /* __GNUC__ */
197 /***************************************************************************
198  * All non gcc
199  */
200
201 #if defined(__QNX__)
202 /*
203  * QNX 4
204  *
205  * Note that slock_t under QNX is sem_t instead of char
206  */
207 #define TAS(lock)       (sem_trywait((lock)) < 0)
208 #define S_UNLOCK(lock)  sem_post((lock))
209 #define S_INIT_LOCK(lock)       sem_init((lock), 1, 1)
210 #define S_LOCK_FREE(lock)       (lock)->value
211 #endif   /* __QNX__ */
212
213
214 #if defined(NEED_I386_TAS_ASM)
215 /* non gcc i386 based things */
216
217 #if defined(USE_UNIVEL_CC)
218 #define TAS(lock)       tas(lock)
219
220 asm int
221 tas(slock_t *s_lock)
222 {
223 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
224 %mem s_lock
225         pushl %ebx
226         movl s_lock, %ebx
227         movl $255, %eax
228         lock
229         xchgb %al, (%ebx)
230         popl %ebx
231 }
232
233 #endif   /* USE_UNIVEL_CC */
234
235 #endif   /* NEED_I386_TAS_ASM */
236
237 #endif   /* defined(__GNUC__) */
238
239
240
241 /*************************************************************************
242  * These are the platforms that have common code for gcc and non-gcc
243  */
244
245
246 #if defined(__alpha)
247
248 #if defined(__osf__)
249 /*
250  * OSF/1 (Alpha AXP)
251  *
252  * Note that slock_t on the Alpha AXP is msemaphore instead of char
253  * (see storage/ipc.h).
254  */
255 #define TAS(lock)         (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
256 #define S_UNLOCK(lock) msem_unlock((lock), 0)
257 #define S_INIT_LOCK(lock)               msem_init((lock), MSEM_UNLOCKED)
258 #define S_LOCK_FREE(lock)         (!(lock)->msem_state)
259
260 #else /* i.e. not __osf__ */
261
262 #define TAS(lock) tas(lock)
263 #define S_UNLOCK(lock) do { __asm__("mb"); *(lock) = 0; } while (0)
264
265 static __inline__ int
266 tas(volatile slock_t *lock)
267 {
268  register slock_t _res;
269
270 __asm__("        ldq   $0, %0                      \n\
271                                  bne   $0, 3f              \n\
272                                  ldq_l $0, %0                    \n\
273                                  bne   $0, 3f              \n\
274                                  or    $31, 1, $0                  \n\
275                                  stq_c $0, %0                              \n\
276                                  beq   $0, 2f                      \n\
277                                  bis   $31, $31, %1        \n\
278                                  mb                                                        \n\
279                                  jmp   $31, 4f                     \n\
280                           2: or    $31, 1, $0                      \n\
281                           3: bis   $0, $0, %1              \n\
282                           4: nop          ": "=m"(*lock), "=r"(_res): :"0");
283
284         return (int) _res;
285 }
286 #endif /* __osf__ */
287
288 #endif /* __alpha */
289
290
291 #if defined(__hpux)
292 /*
293  * HP-UX (PA-RISC)
294  *
295  * Note that slock_t on PA-RISC is a structure instead of char
296  * (see include/port/hpux.h).
297  *
298  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
299  * all words set to non-zero. tas() in tas.s
300  */
301
302 #define S_UNLOCK(lock) \
303 do { \
304         volatile slock_t *lock_ = (volatile slock_t *) (lock); \
305         lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
306 } while (0)
307
308 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
309
310 #endif   /* __hpux */
311
312
313 #if defined(__sgi)
314 /*
315  * SGI IRIX 5
316  * slock_t is defined as a unsigned long. We use the standard SGI
317  * mutex API. 
318  *
319  * The following comment is left for historical reasons, but is probably
320  * not a good idea since the mutex ABI is supported.
321  *
322  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
323  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
324  * for the R3000 chips out there.
325  */
326 #include "mutex.h"
327 #define TAS(lock)       (test_and_set(lock,1))
328 #define S_UNLOCK(lock)  (test_then_and(lock,0))
329 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
330 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
331 #endif   /* __sgi */
332
333 #if defined(sinix)
334 /*
335  * SINIX / Reliant UNIX 
336  * slock_t is defined as a struct abilock_t, which has a single unsigned long
337  * member. (Basically same as SGI)
338  *
339  */
340 #define TAS(lock)       (!acquire_lock(lock))
341 #define S_UNLOCK(lock)  release_lock(lock)
342 #define S_INIT_LOCK(lock)       init_lock(lock)
343 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
344 #endif   /* sinix */
345  
346
347 #if defined(_AIX)
348 /*
349  * AIX (POWER)
350  *
351  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
352  * (see storage/ipc.h).
353  */
354 #define TAS(lock)       cs((int *) (lock), 0, 1)
355 #endif   /* _AIX */
356
357
358 #if defined (nextstep)
359 /*
360  * NEXTSTEP (mach)
361  * slock_t is defined as a struct mutex.
362  */
363
364 #define S_LOCK(lock)    mutex_lock(lock)
365 #define S_UNLOCK(lock)  mutex_unlock(lock)
366 #define S_INIT_LOCK(lock)       mutex_init(lock)
367 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
368 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
369 #endif   /* nextstep */
370
371
372
373
374 /****************************************************************************
375  * Default Definitions - override these above as needed.
376  */
377
378 #if !defined(S_LOCK)
379 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
380
381 #define S_LOCK(lock) \
382         do { \
383                 if (TAS((volatile slock_t *) lock)) \
384                         s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
385         } while (0)
386 #endif   /* S_LOCK */
387
388 #if !defined(S_LOCK_FREE)
389 #define S_LOCK_FREE(lock)       (*(lock) == 0)
390 #endif   /* S_LOCK_FREE */
391
392 #if !defined(S_UNLOCK)
393 #define S_UNLOCK(lock)          (*(lock) = 0)
394 #endif   /* S_UNLOCK */
395
396 #if !defined(S_INIT_LOCK)
397 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
398 #endif   /* S_INIT_LOCK */
399
400 #if !defined(TAS)
401 int                     tas(volatile slock_t *lock);            /* port/.../tas.s, or
402                                                                                                  * s_lock.c */
403
404 #define TAS(lock)               tas((volatile slock_t *) lock)
405 #endif   /* TAS */
406
407 #endif   /* HAS_TEST_AND_SET */
408 #endif   /* S_LOCK_H */
409