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