]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[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.86 2001/01/24 19:43:28 momjian 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__)
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                 "       tas     %1              \n"
226                 "       sne     %0              \n"
227 :               "=d"(rv), "=m"(*lock)
228 :               "1"(*lock)
229 :               "cc");
230
231         return rv;
232 }
233
234 #endif   /* defined(__mc68000__) && defined(__linux__) */
235
236
237 #if defined(NEED_VAX_TAS_ASM)
238 /*
239  * VAXen -- even multiprocessor ones
240  * (thanks to Tom Ivar Helbekkmo)
241  */
242 #define TAS(lock) tas(lock)
243
244 static __inline__ int
245 tas(volatile slock_t *lock)
246 {
247         register        _res;
248
249         __asm__ __volatile__(
250                 "       movl    $1, r0                  \n"
251                 "       bbssi   $0, (%1), 1f    \n"
252                 "       clrl    r0                              \n"
253                 "1:     movl    r0, %0                  \n"
254 :               "=r"(_res)
255 :               "r"(lock)
256 :               "r0");
257         return (int) _res;
258 }
259
260 #endif   /* NEED_VAX_TAS_ASM */
261
262
263 #if defined(NEED_NS32K_TAS_ASM)
264 #define TAS(lock) tas(lock)
265
266 static __inline__ int
267 tas(volatile slock_t *lock)
268 {
269         register        _res;
270
271         __asm__ __volatile__(
272                 "       sbitb   0, %0   \n"
273                 "       sfsd    %1              \n"
274 :               "=m"(*lock), "=r"(_res));
275         return (int) _res;
276 }
277
278 #endif   /* NEED_NS32K_TAS_ASM */
279
280
281
282 #else                                                   /* !__GNUC__ */
283
284 /***************************************************************************
285  * All non-gcc inlines
286  */
287
288 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
289 #define TAS(lock)       tas(lock)
290
291 asm int
292 tas(volatile slock_t *s_lock)
293 {
294 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
295 %mem s_lock
296         pushl %ebx
297         movl s_lock, %ebx
298         movl $255, %eax
299         lock
300         xchgb %al, (%ebx)
301         popl %ebx
302 }
303
304 #endif   /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
305
306 #endif   /* defined(__GNUC__) */
307
308
309
310 /*************************************************************************
311  * These are the platforms that do not use inline assembler (and hence
312  * have common code for gcc and non-gcc compilers, if both are available).
313  */
314
315
316 #if defined(__alpha)
317
318 /*
319  * Correct multi-processor locking methods are explained in section 5.5.3
320  * of the Alpha AXP Architecture Handbook, which at this writing can be
321  * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
322  * For gcc we implement the handbook's code directly with inline assembler.
323  */
324 #if defined(__GNUC__)
325
326 #define TAS(lock)  tas(lock)
327 #define S_UNLOCK(lock)  \
328 do \
329 {\
330         __asm__ __volatile__ (" mb \n"); \
331         *(lock) = 0; \
332 } while (0)
333
334 static __inline__ int
335 tas(volatile slock_t *lock)
336 {
337         register slock_t _res;
338
339         __asm__ __volatile__(
340                 "       ldq             $0, %0  \n"
341                 "       bne             $0, 2f  \n"
342                 "       ldq_l   %1, %0  \n"
343                 "       bne             %1, 2f  \n"
344                 "       mov             1,  $0  \n"
345                 "       stq_c   $0, %0  \n"
346                 "       beq             $0, 2f  \n"
347                 "       mb                              \n"
348                 "       br              3f              \n"
349                 "2:     mov             1, %1   \n"
350                 "3:                                     \n"
351 :               "=m"(*lock), "=r"(_res)
352 :
353 :               "0");
354
355         return (int) _res;
356 }
357
358 #else                                                   /* !defined(__GNUC__) */
359
360 /*
361  * The Tru64 compiler doesn't support gcc-style inline asm, but it does
362  * have some builtin functions that accomplish much the same results.
363  * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
364  * regardless of the compiler in use.  LOCK_LONG and UNLOCK_LONG only
365  * operate on an int (ie, longword), but that's OK as long as we define
366  * S_INIT_LOCK to zero out the whole quadword.
367  */
368
369 #include <alpha/builtins.h>
370
371 #define S_INIT_LOCK(lock)  (*(lock) = 0)
372 #define TAS(lock)                  (__LOCK_LONG_RETRY((lock), 1) == 0)
373 #define S_UNLOCK(lock)     __UNLOCK_LONG(lock)
374
375 #endif   /* defined(__GNUC__) */
376
377 #endif   /* __alpha */
378
379
380 #if defined(__hpux)
381 /*
382  * HP-UX (PA-RISC)
383  *
384  * Note that slock_t on PA-RISC is a structure instead of char
385  * (see include/port/hpux.h).
386  *
387  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
388  * all words set to non-zero. tas() is in tas.s
389  */
390
391 #define S_UNLOCK(lock) \
392         do { \
393                 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
394                 lock_->sema[0] = lock_->sema[1] = \
395                 lock_->sema[2] = lock_->sema[3] = -1; \
396         } while (0)
397
398 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
399
400 #endif   /* __hpux */
401
402
403 #if defined(__QNX__)
404 /*
405  * QNX 4
406  *
407  * Note that slock_t under QNX is sem_t instead of char
408  */
409 #define TAS(lock)               (sem_trywait((lock)) < 0)
410 #define S_UNLOCK(lock)  sem_post((lock))
411 #define S_INIT_LOCK(lock)               sem_init((lock), 1, 1)
412 #define S_LOCK_FREE(lock)               ((lock)->value)
413 #endif   /* __QNX__ */
414
415
416 #if defined(__sgi)
417 /*
418  * SGI IRIX 5
419  * slock_t is defined as a unsigned long. We use the standard SGI
420  * mutex API.
421  *
422  * The following comment is left for historical reasons, but is probably
423  * not a good idea since the mutex ABI is supported.
424  *
425  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
426  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
427  * for the R3000 chips out there.
428  */
429 #include "mutex.h"
430 #define TAS(lock)       (test_and_set(lock,1))
431 #define S_UNLOCK(lock)  (test_then_and(lock,0))
432 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
433 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
434 #endif   /* __sgi */
435
436 #if defined(sinix)
437 /*
438  * SINIX / Reliant UNIX
439  * slock_t is defined as a struct abilock_t, which has a single unsigned long
440  * member. (Basically same as SGI)
441  *
442  */
443 #define TAS(lock)       (!acquire_lock(lock))
444 #define S_UNLOCK(lock)  release_lock(lock)
445 #define S_INIT_LOCK(lock)       init_lock(lock)
446 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
447 #endif   /* sinix */
448
449
450 #if defined(_AIX)
451 /*
452  * AIX (POWER)
453  *
454  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
455  * (see storage/ipc.h).
456  */
457 #define TAS(lock)       cs((int *) (lock), 0, 1)
458 #endif   /* _AIX */
459
460
461 #if defined (nextstep)
462 /*
463  * NEXTSTEP (mach)
464  * slock_t is defined as a struct mutex.
465  */
466
467 #define S_LOCK(lock)    mutex_lock(lock)
468 #define S_UNLOCK(lock)  mutex_unlock(lock)
469 #define S_INIT_LOCK(lock)       mutex_init(lock)
470 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
471 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
472 #endif   /* nextstep */
473
474
475
476 #else                                                   /* !HAS_TEST_AND_SET */
477
478 /*
479  * Fake spinlock implementation using SysV semaphores --- slow and prone
480  * to fall foul of kernel limits on number of semaphores, so don't use this
481  * unless you must!
482  */
483
484 typedef struct
485 {
486         /* reference to semaphore used to implement this spinlock */
487         IpcSemaphoreId semId;
488         int                     sem;
489 } slock_t;
490
491 extern bool s_lock_free_sema(volatile slock_t *lock);
492 extern void s_unlock_sema(volatile slock_t *lock);
493 extern void s_init_lock_sema(volatile slock_t *lock);
494 extern int      tas_sema(volatile slock_t *lock);
495
496 #define S_LOCK_FREE(lock)       s_lock_free_sema(lock)
497 #define S_UNLOCK(lock)   s_unlock_sema(lock)
498 #define S_INIT_LOCK(lock)       s_init_lock_sema(lock)
499 #define TAS(lock)       tas_sema(lock)
500
501 #endif   /* HAS_TEST_AND_SET */
502
503
504
505 /****************************************************************************
506  * Default Definitions - override these above as needed.
507  */
508
509 #if !defined(S_LOCK)
510 #define S_LOCK(lock) \
511         do { \
512                 if (TAS(lock)) \
513                         s_lock((lock), __FILE__, __LINE__); \
514         } while (0)
515 #endif   /* S_LOCK */
516
517 #if !defined(S_LOCK_SLEEP)
518 #define S_LOCK_SLEEP(lock,spins) \
519         s_lock_sleep((spins), 0, (lock), __FILE__, __LINE__)
520 #endif   /* S_LOCK_SLEEP */
521
522 #if !defined(S_LOCK_SLEEP_INTERVAL)
523 #define S_LOCK_SLEEP_INTERVAL(lock,spins,microsec) \
524         s_lock_sleep((spins), (microsec), (lock), __FILE__, __LINE__)
525 #endif   /* S_LOCK_SLEEP_INTERVAL */
526
527 #if !defined(S_LOCK_FREE)
528 #define S_LOCK_FREE(lock)       (*(lock) == 0)
529 #endif   /* S_LOCK_FREE */
530
531 #if !defined(S_UNLOCK)
532 #define S_UNLOCK(lock)          (*(lock) = 0)
533 #endif   /* S_UNLOCK */
534
535 #if !defined(S_INIT_LOCK)
536 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
537 #endif   /* S_INIT_LOCK */
538
539 #if !defined(TAS)
540 extern int      tas(volatile slock_t *lock);            /* in port/.../tas.s, or
541                                                                                                  * s_lock.c */
542
543 #define TAS(lock)               tas(lock)
544 #endif   /* TAS */
545
546
547 #endif   /* S_LOCK_H */