]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
> > I'll re-check that with the ppc architecture guy here.
[postgresql] / src / include / storage / s_lock.h
1 /*-------------------------------------------------------------------------
2  *
3  * s_lock.h
4  *         Hardware-dependent implementation of spinlocks.
5  *
6  *      NOTE: none of the macros in this file are intended to be called directly.
7  *      Call them through the hardware-independent macros in spin.h.
8  *
9  *      The following hardware-dependent macros must be provided for each
10  *      supported platform:
11  *
12  *      void S_INIT_LOCK(slock_t *lock)
13  *              Initialize a spinlock (to the unlocked state).
14  *
15  *      void S_LOCK(slock_t *lock)
16  *              Acquire a spinlock, waiting if necessary.
17  *              Time out and abort() if unable to acquire the lock in a
18  *              "reasonable" amount of time --- typically ~ 1 minute.
19  *
20  *      void S_UNLOCK(slock_t *lock)
21  *              Unlock a previously acquired lock.
22  *
23  *      bool S_LOCK_FREE(slock_t *lock)
24  *              Tests if the lock is free. Returns TRUE if free, FALSE if locked.
25  *              This does *not* change the state of the lock.
26  *
27  *      Note to implementors: there are default implementations for all these
28  *      macros at the bottom of the file.  Check if your platform can use
29  *      these or needs to override them.
30  *
31  *  Usually, S_LOCK() is implemented in terms of an even lower-level macro
32  *      TAS():
33  *
34  *      int TAS(slock_t *lock)
35  *              Atomic test-and-set instruction.  Attempt to acquire the lock,
36  *              but do *not* wait.      Returns 0 if successful, nonzero if unable
37  *              to acquire the lock.
38  *
39  *      TAS() is NOT part of the API, and should never be called directly.
40  *
41  *      CAUTION: on some platforms TAS() may sometimes report failure to acquire
42  *      a lock even when the lock is not locked.  For example, on Alpha TAS()
43  *      will "fail" if interrupted.  Therefore TAS() should always be invoked
44  *      in a retry loop, even if you are certain the lock is free.
45  *
46  *      ANOTHER CAUTION: be sure that TAS() and S_UNLOCK() represent sequence
47  *      points, ie, loads and stores of other values must not be moved across
48  *      a lock or unlock.  In most cases it suffices to make the operation be
49  *      done through a "volatile" pointer.
50  *
51  *      On most supported platforms, TAS() uses a tas() function written
52  *      in assembly language to execute a hardware atomic-test-and-set
53  *      instruction.  Equivalent OS-supplied mutex routines could be used too.
54  *
55  *      If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
56  *      defined), then we fall back on an emulation that uses SysV semaphores
57  *      (see spin.c).  This emulation will be MUCH MUCH slower than a proper TAS()
58  *      implementation, because of the cost of a kernel call per lock or unlock.
59  *      An old report is that Postgres spends around 40% of its time in semop(2)
60  *      when using the SysV semaphore code.
61  *
62  *
63  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
64  * Portions Copyright (c) 1994, Regents of the University of California
65  *
66  *        $Id: s_lock.h,v 1.102 2002/11/10 00:33:43 momjian Exp $
67  *
68  *-------------------------------------------------------------------------
69  */
70 #ifndef S_LOCK_H
71 #define S_LOCK_H
72
73 #include "storage/pg_sema.h"
74
75
76 #if defined(HAS_TEST_AND_SET)
77
78
79 #if defined(__GNUC__)
80 /*************************************************************************
81  * All the gcc inlines
82  */
83
84 /*
85  * Standard gcc asm format:
86  *
87         __asm__ __volatile__(
88                 "       command \n"
89                 "       command \n"
90                 "       command \n"
91 :               "=r"(_res)                      return value, in register
92 :               "r"(lock)                       argument, 'lock pointer', in register
93 :               "r0");                          inline code uses this register
94  */
95
96
97 #if defined(__i386__)
98 #define TAS(lock) tas(lock)
99
100 static __inline__ int
101 tas(volatile slock_t *lock)
102 {
103         register slock_t _res = 1;
104
105         __asm__ __volatile__(
106                 "       lock                    \n"
107                 "       xchgb   %0,%1   \n"
108 :               "=q"(_res), "=m"(*lock)
109 :               "0"(_res));
110         return (int) _res;
111 }
112
113 #endif   /* __i386__ */
114
115
116 #ifdef __ia64__
117 #define TAS(lock) tas(lock)
118
119 static __inline__ int
120 tas(volatile slock_t *lock)
121 {
122         long int        ret;
123
124         __asm__ __volatile__(
125                 "       xchg4   %0=%1,%2        \n"
126 :               "=r"(ret), "=m"(*lock)
127 :               "r"(1), "1"(*lock)
128 :               "memory");
129
130         return (int) ret;
131 }
132
133 #endif   /* __ia64__ */
134
135
136 #if defined(__arm__) || defined(__arm__)
137 #define TAS(lock) tas(lock)
138
139 static __inline__ int
140 tas(volatile slock_t *lock)
141 {
142         register slock_t _res = 1;
143
144         __asm__ __volatile__(
145                 "       swpb    %0, %0, [%3]    \n"
146 :               "=r"(_res), "=m"(*lock)
147 :               "0"(_res), "r"(lock));
148         return (int) _res;
149 }
150
151 #endif   /* __arm__ */
152
153 #if defined(__s390__) || defined(__s390x__)
154 /*
155  * S/390 Linux
156  */
157 #define TAS(lock)          tas(lock)
158
159 static __inline__ int
160 tas(volatile slock_t *lock)
161 {
162         int                     _res;
163
164         __asm__ __volatile__(
165                 "       la      1,1                     \n"
166                 "       l       2,%2            \n"
167                 "       slr 0,0                 \n"
168                 "       cs      0,1,0(2)        \n"
169                 "       lr      %1,0            \n"
170 :               "=m"(lock), "=d"(_res)
171 :               "m"(lock)
172 :               "0", "1", "2");
173
174         return (_res);
175 }
176
177 #endif   /* __s390__ */
178
179
180 #if defined(__sparc__)
181 #define TAS(lock) tas(lock)
182
183 static __inline__ int
184 tas(volatile slock_t *lock)
185 {
186         register slock_t _res = 1;
187
188         __asm__ __volatile__(
189                 "       ldstub  [%2], %0        \n"
190 :               "=r"(_res), "=m"(*lock)
191 :               "r"(lock));
192         return (int) _res;
193 }
194
195 #endif   /* __sparc__ */
196
197 #if defined(__powerpc__) || defined(__powerpc64__)
198 static __inline__ int
199 tas(volatile slock_t *lock)
200 {
201         slock_t _t;
202         int _res;
203
204         __asm__ __volatile__(
205 "       lwarx   %0,0,%3         \n"
206 "       cmpwi   %0,0            \n"
207 "       bne     1f              \n"
208 "       addi    %0,%0,1         \n"
209 "       stwcx.  %0,0,%3         \n"
210 "       isync                   \n"
211 "       beq     2f              \n"
212 "1:     li      %2,1            \n"
213 "       b       3f              \n"
214 "2:                             \n"
215 "       li      %2,0            \n"
216 "3:                             \n"
217
218 :       "=&r" (_t), "=m" (lock), "=r" (_res)
219 :       "r" (lock)
220 :       "cc", "memory"
221         );
222         return _res;
223 }
224 #endif
225
226
227 #if defined(__mc68000__) && defined(__linux__)
228 #define TAS(lock) tas(lock)
229
230 static __inline__ int
231 tas(volatile slock_t *lock)
232 {
233         register int rv;
234
235         __asm__ __volatile__(
236                 "       clrl    %0              \n"
237                 "       tas             %1              \n"
238                 "       sne             %0              \n"
239 :               "=d"(rv), "=m"(*lock)
240 :               "1"(*lock)
241 :               "cc");
242
243         return rv;
244 }
245
246 #endif   /* defined(__mc68000__) && defined(__linux__) */
247
248
249 #if defined(__ppc__) || defined(__powerpc__)
250 /*
251  * We currently use out-of-line assembler for TAS on PowerPC; see s_lock.c.
252  * S_UNLOCK is almost standard but requires a "sync" instruction.
253  */
254 #define S_UNLOCK(lock)  \
255 do \
256 {\
257         __asm__ __volatile__ (" sync \n"); \
258         *((volatile slock_t *) (lock)) = 0; \
259 } while (0)
260
261 #endif /* defined(__ppc__) || defined(__powerpc__) */
262
263
264 #if defined(NEED_VAX_TAS_ASM)
265 /*
266  * VAXen -- even multiprocessor ones
267  * (thanks to Tom Ivar Helbekkmo)
268  */
269 #define TAS(lock) tas(lock)
270
271 static __inline__ int
272 tas(volatile slock_t *lock)
273 {
274         register int    _res;
275
276         __asm__ __volatile__(
277                 "       movl    $1, r0                  \n"
278                 "       bbssi   $0, (%1), 1f    \n"
279                 "       clrl    r0                              \n"
280                 "1:     movl    r0, %0                  \n"
281 :               "=r"(_res)
282 :               "r"(lock)
283 :               "r0");
284         return _res;
285 }
286
287 #endif   /* NEED_VAX_TAS_ASM */
288
289
290 #if defined(NEED_NS32K_TAS_ASM)
291 #define TAS(lock) tas(lock)
292
293 static __inline__ int
294 tas(volatile slock_t *lock)
295 {
296         register int    _res;
297
298         __asm__ __volatile__(
299                 "       sbitb   0, %0   \n"
300                 "       sfsd    %1              \n"
301 :               "=m"(*lock), "=r"(_res));
302         return _res;
303 }
304
305 #endif   /* NEED_NS32K_TAS_ASM */
306
307
308
309 #else                                                   /* !__GNUC__ */
310
311 /***************************************************************************
312  * All non-gcc inlines
313  */
314
315 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
316 #define TAS(lock)       tas(lock)
317
318 asm int
319 tas(volatile slock_t *s_lock)
320 {
321 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
322 %mem s_lock
323         pushl %ebx
324         movl s_lock, %ebx
325         movl $255, %eax
326         lock
327         xchgb %al, (%ebx)
328         popl %ebx
329 }
330
331 #endif   /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
332
333 #endif   /* defined(__GNUC__) */
334
335
336
337 /*************************************************************************
338  * These are the platforms that do not use inline assembler (and hence
339  * have common code for gcc and non-gcc compilers, if both are available).
340  */
341
342
343 #if defined(__alpha)
344
345 /*
346  * Correct multi-processor locking methods are explained in section 5.5.3
347  * of the Alpha AXP Architecture Handbook, which at this writing can be
348  * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
349  * For gcc we implement the handbook's code directly with inline assembler.
350  */
351 #if defined(__GNUC__)
352
353 #define TAS(lock)  tas(lock)
354 #define S_UNLOCK(lock)  \
355 do \
356 {\
357         __asm__ __volatile__ (" mb \n"); \
358         *((volatile slock_t *) (lock)) = 0; \
359 } while (0)
360
361 static __inline__ int
362 tas(volatile slock_t *lock)
363 {
364         register slock_t _res;
365
366         __asm__ __volatile__(
367                 "       ldq             $0, %0  \n"
368                 "       bne             $0, 2f  \n"
369                 "       ldq_l   %1, %0  \n"
370                 "       bne             %1, 2f  \n"
371                 "       mov             1,  $0  \n"
372                 "       stq_c   $0, %0  \n"
373                 "       beq             $0, 2f  \n"
374                 "       mb                              \n"
375                 "       br              3f              \n"
376                 "2:     mov             1, %1   \n"
377                 "3:                                     \n"
378 :               "=m"(*lock), "=r"(_res)
379 :
380 :               "0");
381
382         return (int) _res;
383 }
384
385 #else                                                   /* !defined(__GNUC__) */
386
387 /*
388  * The Tru64 compiler doesn't support gcc-style inline asm, but it does
389  * have some builtin functions that accomplish much the same results.
390  * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
391  * regardless of the compiler in use.  LOCK_LONG and UNLOCK_LONG only
392  * operate on an int (ie, longword), but that's OK as long as we define
393  * S_INIT_LOCK to zero out the whole quadword.
394  */
395
396 #include <alpha/builtins.h>
397
398 #define S_INIT_LOCK(lock)  (*(lock) = 0)
399 #define TAS(lock)                  (__LOCK_LONG_RETRY((lock), 1) == 0)
400 #define S_UNLOCK(lock)     __UNLOCK_LONG(lock)
401
402 #endif   /* defined(__GNUC__) */
403
404 #endif   /* __alpha */
405
406
407 #if defined(__hpux)
408 /*
409  * HP-UX (PA-RISC)
410  *
411  * Note that slock_t on PA-RISC is a structure instead of char
412  * (see include/port/hpux.h).
413  *
414  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
415  * all words set to non-zero. tas() is in tas.s
416  */
417
418 #define S_UNLOCK(lock) \
419         do { \
420                 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
421                 lock_->sema[0] = -1; \
422                 lock_->sema[1] = -1; \
423                 lock_->sema[2] = -1; \
424                 lock_->sema[3] = -1; \
425         } while (0)
426
427 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
428
429 #endif   /* __hpux */
430
431 #if defined(__QNX__) && defined(__WATCOMC__)
432 /*
433  * QNX 4 using WATCOM C
434  */
435 #define TAS(lock) wc_tas(lock)
436 extern slock_t wc_tas(volatile slock_t *lock);
437 #pragma aux wc_tas =\
438                 "       mov   al,1    " \
439                 " lock  xchg    al,[esi]" \
440                 parm [esi]        \
441                 value [al];
442
443 #endif   /* __QNX__ and __WATCOMC__*/
444
445
446 #if defined(__sgi)
447 /*
448  * SGI IRIX 5
449  * slock_t is defined as a unsigned long. We use the standard SGI
450  * mutex API.
451  *
452  * The following comment is left for historical reasons, but is probably
453  * not a good idea since the mutex ABI is supported.
454  *
455  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
456  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
457  * for the R3000 chips out there.
458  */
459 #include "mutex.h"
460 #define TAS(lock)       (test_and_set(lock,1))
461 #define S_UNLOCK(lock)  (test_then_and(lock,0))
462 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
463 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
464 #endif   /* __sgi */
465
466 #if defined(sinix)
467 /*
468  * SINIX / Reliant UNIX
469  * slock_t is defined as a struct abilock_t, which has a single unsigned long
470  * member. (Basically same as SGI)
471  *
472  */
473 #define TAS(lock)       (!acquire_lock(lock))
474 #define S_UNLOCK(lock)  release_lock(lock)
475 #define S_INIT_LOCK(lock)       init_lock(lock)
476 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
477 #endif   /* sinix */
478
479
480 #if defined(_AIX)
481 /*
482  * AIX (POWER)
483  *
484  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
485  */
486 #define TAS(lock)                       _check_lock(lock, 0, 1)
487 #define S_UNLOCK(lock)          _clear_lock(lock, 0)
488 #endif   /* _AIX */
489
490
491 #if defined (nextstep)
492 /*
493  * NEXTSTEP (mach)
494  * slock_t is defined as a struct mutex.
495  */
496
497 #define S_LOCK(lock)    mutex_lock(lock)
498 #define S_UNLOCK(lock)  mutex_unlock(lock)
499 #define S_INIT_LOCK(lock)       mutex_init(lock)
500 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
501 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
502 #endif   /* nextstep */
503
504
505
506 #else                                                   /* !HAS_TEST_AND_SET */
507
508 /*
509  * Fake spinlock implementation using semaphores --- slow and prone
510  * to fall foul of kernel limits on number of semaphores, so don't use this
511  * unless you must!  The subroutines appear in spin.c.
512  */
513 typedef PGSemaphoreData slock_t;
514
515 extern bool s_lock_free_sema(volatile slock_t *lock);
516 extern void s_unlock_sema(volatile slock_t *lock);
517 extern void s_init_lock_sema(volatile slock_t *lock);
518 extern int      tas_sema(volatile slock_t *lock);
519
520 #define S_LOCK_FREE(lock)       s_lock_free_sema(lock)
521 #define S_UNLOCK(lock)   s_unlock_sema(lock)
522 #define S_INIT_LOCK(lock)       s_init_lock_sema(lock)
523 #define TAS(lock)       tas_sema(lock)
524
525 #endif   /* HAS_TEST_AND_SET */
526
527
528
529 /*
530  * Default Definitions - override these above as needed.
531  */
532
533 #if !defined(S_LOCK)
534 #define S_LOCK(lock) \
535         do { \
536                 if (TAS(lock)) \
537                         s_lock((lock), __FILE__, __LINE__); \
538         } while (0)
539 #endif   /* S_LOCK */
540
541 #if !defined(S_LOCK_FREE)
542 #define S_LOCK_FREE(lock)       (*(lock) == 0)
543 #endif   /* S_LOCK_FREE */
544
545 #if !defined(S_UNLOCK)
546 #define S_UNLOCK(lock)          (*((volatile slock_t *) (lock)) = 0)
547 #endif   /* S_UNLOCK */
548
549 #if !defined(S_INIT_LOCK)
550 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
551 #endif   /* S_INIT_LOCK */
552
553 #if !defined(TAS)
554 extern int      tas(volatile slock_t *lock);            /* in port/.../tas.s, or
555                                                                                                  * s_lock.c */
556
557 #define TAS(lock)               tas(lock)
558 #endif   /* TAS */
559
560
561 /*
562  * Platform-independent out-of-line support routines
563  */
564 extern void s_lock(volatile slock_t *lock, const char *file, int line);
565
566 #endif   /* S_LOCK_H */