]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Clean up spinlock assembly code slightly (just cosmetic improvements)
[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.77 2000/12/30 02:34:56 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 /*
295  * Correct multi-processor locking methods are explained in section 5.5.3
296  * of the Alpha AXP Architecture Handbook, which at this writing can be
297  * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
298  * For gcc we implement the handbook's code directly with inline assembler.
299  */
300 #if defined(__GNUC__)
301
302 #define TAS(lock)  tas(lock)
303 #define S_UNLOCK(lock)  do { __asm__ volatile ("mb"); *(lock) = 0; } while (0)
304
305 static __inline__ int
306 tas(volatile slock_t *lock)
307 {
308         register slock_t _res;
309
310         __asm__ volatile
311 ("              ldq   $0, %0            \n\
312                 bne   $0, 2f            \n\
313                 ldq_l %1, %0            \n\
314                 bne   %1, 2f            \n\
315                 mov   1, $0                     \n\
316                 stq_c $0, %0            \n\
317                 beq   $0, 2f            \n\
318                 mb                                      \n\
319                 br    3f                        \n\
320          2: mov   1, %1                 \n\
321          3:       \n" : "=m"(*lock), "=r"(_res) : : "0");
322
323         return (int) _res;
324 }
325
326 #else /* !defined(__GNUC__) */
327
328 /*
329  * The Tru64 compiler doesn't support gcc-style inline asm, but it does
330  * have some builtin functions that accomplish much the same results.
331  * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
332  * regardless of the compiler in use.  LOCK_LONG and UNLOCK_LONG only
333  * operate on an int (ie, longword), but that's OK as long as we define
334  * S_INIT_LOCK to zero out the whole quadword.
335  */
336
337 #include <alpha/builtins.h>
338
339 #define S_INIT_LOCK(lock)  (*(lock) = 0)
340 #define TAS(lock)          (__LOCK_LONG_RETRY((lock), 1) == 0)
341 #define S_UNLOCK(lock)     __UNLOCK_LONG(lock)
342
343 #endif /* defined(__GNUC__) */
344
345 #endif /* __alpha */
346
347
348 #if defined(__hpux)
349 /*
350  * HP-UX (PA-RISC)
351  *
352  * Note that slock_t on PA-RISC is a structure instead of char
353  * (see include/port/hpux.h).
354  *
355  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
356  * all words set to non-zero. tas() is in tas.s
357  */
358
359 #define S_UNLOCK(lock) \
360 do { \
361         volatile slock_t *lock_ = (volatile slock_t *) (lock); \
362         lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
363 } while (0)
364
365 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
366
367 #endif   /* __hpux */
368
369
370 #if defined(__QNX__)
371 /*
372  * QNX 4
373  *
374  * Note that slock_t under QNX is sem_t instead of char
375  */
376 #define TAS(lock)       (sem_trywait((lock)) < 0)
377 #define S_UNLOCK(lock)  sem_post((lock))
378 #define S_INIT_LOCK(lock)       sem_init((lock), 1, 1)
379 #define S_LOCK_FREE(lock)       ((lock)->value)
380 #endif   /* __QNX__ */
381
382
383 #if defined(__sgi)
384 /*
385  * SGI IRIX 5
386  * slock_t is defined as a unsigned long. We use the standard SGI
387  * mutex API. 
388  *
389  * The following comment is left for historical reasons, but is probably
390  * not a good idea since the mutex ABI is supported.
391  *
392  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
393  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
394  * for the R3000 chips out there.
395  */
396 #include "mutex.h"
397 #define TAS(lock)       (test_and_set(lock,1))
398 #define S_UNLOCK(lock)  (test_then_and(lock,0))
399 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
400 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
401 #endif   /* __sgi */
402
403 #if defined(sinix)
404 /*
405  * SINIX / Reliant UNIX 
406  * slock_t is defined as a struct abilock_t, which has a single unsigned long
407  * member. (Basically same as SGI)
408  *
409  */
410 #define TAS(lock)       (!acquire_lock(lock))
411 #define S_UNLOCK(lock)  release_lock(lock)
412 #define S_INIT_LOCK(lock)       init_lock(lock)
413 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
414 #endif   /* sinix */
415  
416
417 #if defined(_AIX)
418 /*
419  * AIX (POWER)
420  *
421  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
422  * (see storage/ipc.h).
423  */
424 #define TAS(lock)       cs((int *) (lock), 0, 1)
425 #endif   /* _AIX */
426
427
428 #if defined (nextstep)
429 /*
430  * NEXTSTEP (mach)
431  * slock_t is defined as a struct mutex.
432  */
433
434 #define S_LOCK(lock)    mutex_lock(lock)
435 #define S_UNLOCK(lock)  mutex_unlock(lock)
436 #define S_INIT_LOCK(lock)       mutex_init(lock)
437 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
438 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
439 #endif   /* nextstep */
440
441
442
443 #else    /* !HAS_TEST_AND_SET */
444
445 /*
446  * Fake spinlock implementation using SysV semaphores --- slow and prone
447  * to fall foul of kernel limits on number of semaphores, so don't use this
448  * unless you must!
449  */
450
451 typedef struct
452 {
453         /* reference to semaphore used to implement this spinlock */
454         IpcSemaphoreId  semId;
455         int                             sem;
456 } slock_t;
457
458 extern bool s_lock_free_sema(volatile slock_t *lock);
459 extern void s_unlock_sema(volatile slock_t *lock);
460 extern void s_init_lock_sema(volatile slock_t *lock);
461 extern int tas_sema(volatile slock_t *lock);
462
463 #define S_LOCK_FREE(lock)   s_lock_free_sema(lock)
464 #define S_UNLOCK(lock)   s_unlock_sema(lock)
465 #define S_INIT_LOCK(lock)   s_init_lock_sema(lock)
466 #define TAS(lock)   tas_sema(lock)
467
468 #endif   /* HAS_TEST_AND_SET */
469
470
471
472 /****************************************************************************
473  * Default Definitions - override these above as needed.
474  */
475
476 #if !defined(S_LOCK)
477 #define S_LOCK(lock) \
478         do { \
479                 if (TAS(lock)) \
480                         s_lock((lock), __FILE__, __LINE__); \
481         } while (0)
482 #endif   /* S_LOCK */
483
484 #if !defined(S_LOCK_SLEEP)
485 #define S_LOCK_SLEEP(lock,spins) \
486         s_lock_sleep((spins), 0, (lock), __FILE__, __LINE__)
487 #endif   /* S_LOCK_SLEEP */
488
489 #if !defined(S_LOCK_SLEEP_INTERVAL)
490 #define S_LOCK_SLEEP_INTERVAL(lock,spins,microsec) \
491         s_lock_sleep((spins), (microsec), (lock), __FILE__, __LINE__)
492 #endif   /* S_LOCK_SLEEP_INTERVAL */
493
494 #if !defined(S_LOCK_FREE)
495 #define S_LOCK_FREE(lock)       (*(lock) == 0)
496 #endif   /* S_LOCK_FREE */
497
498 #if !defined(S_UNLOCK)
499 #define S_UNLOCK(lock)          (*(lock) = 0)
500 #endif   /* S_UNLOCK */
501
502 #if !defined(S_INIT_LOCK)
503 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
504 #endif   /* S_INIT_LOCK */
505
506 #if !defined(TAS)
507 extern int      tas(volatile slock_t *lock);            /* in port/.../tas.s, or
508                                                                                                  * s_lock.c */
509
510 #define TAS(lock)               tas(lock)
511 #endif   /* TAS */
512
513
514 #endif   /* S_LOCK_H */