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