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