]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Support IBM S/390. Patches from Neale Ferguson@softwareAG-usa.com.
[postgresql] / src / include / storage / s_lock.h
1 /*-------------------------------------------------------------------------
2  *
3  * s_lock.h
4  *         This file contains the implementation (if any) for spinlocks.
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.75 2000/12/03 14:41:42 thomas Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 /*
17  *       DESCRIPTION
18  *              The public macros that must be provided are:
19  *
20  *              void S_INIT_LOCK(slock_t *lock)
21  *
22  *              void S_LOCK(slock_t *lock)
23  *
24  *              void S_UNLOCK(slock_t *lock)
25  *
26  *              void S_LOCK_FREE(slock_t *lock)
27  *                      Tests if the lock is free. Returns non-zero if free, 0 if locked.
28  *
29  *              The S_LOCK() macro implements a primitive but still useful random
30  *              backoff to avoid hordes of busywaiting lockers chewing CPU.
31  *
32  *              Effectively:
33  *              void
34  *              S_LOCK(slock_t *lock)
35  *              {
36  *                      while (TAS(lock))
37  *                      {
38  *                      // back off the cpu for a semi-random short time
39  *                      }
40  *              }
41  *
42  *              This implementation takes advantage of a tas function written
43  *              (in assembly language) on machines that have a native test-and-set
44  *              instruction. Alternative mutex implementations may also be used.
45  *              This function is hidden under the TAS macro to allow substitutions.
46  *
47  *              #define TAS(lock) tas(lock)
48  *              int tas(slock_t *lock)          // True if lock already set
49  *
50  *              There are default implementations for all these macros at the bottom
51  *              of this file. Check if your platform can use these or needs to
52  *              override them.
53  *
54  *      NOTES
55  *              If none of this can be done, POSTGRES will default to using
56  *              System V semaphores (and take a large performance hit -- around 40%
57  *              of its time on a DS5000/240 is spent in semop(3)...).
58  *
59  *              AIX has a test-and-set but the recommended interface is the cs(3)
60  *              system call.  This provides an 8-instruction (plus system call
61  *              overhead) uninterruptible compare-and-set operation.  True
62  *              spinlocks might be faster but using cs(3) still speeds up the
63  *              regression test suite by about 25%.  I don't have an assembler
64  *              manual for POWER in any case.
65  *
66  */
67 #ifndef S_LOCK_H
68 #define S_LOCK_H
69
70 #include "storage/ipc.h"
71
72 extern void s_lock_sleep(unsigned spin);
73
74 #if defined(HAS_TEST_AND_SET)
75
76
77 #if defined(__GNUC__)
78 /*************************************************************************
79  * All the gcc inlines
80  */
81
82
83 #if defined(__i386__)
84 #define TAS(lock) tas(lock)
85
86 static __inline__ int
87 tas(volatile slock_t *lock)
88 {
89         register slock_t _res = 1;
90
91 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
92         return (int) _res;
93 }
94
95 #endif   /* __i386__ */
96
97
98 #ifdef __ia64__
99 #define TAS(lock) tas(lock)
100
101 static __inline__ int
102 tas (volatile slock_t *lock)
103 {
104   long int ret;
105
106   __asm__ __volatile__(
107        "xchg4 %0=%1,%2"
108        : "=r"(ret), "=m"(*lock)
109        : "r"(1), "1"(*lock)
110        : "memory");
111
112   return (int) ret;
113 }
114 #endif /* __ia64__ */
115
116
117 #if defined(__arm__) || defined(__arm__)
118 #define TAS(lock) tas(lock)
119
120 static __inline__ int
121 tas(volatile slock_t *lock)
122 {
123         register slock_t _res = 1;
124
125 __asm__("swpb %0, %0, [%3]": "=r"(_res), "=m"(*lock):"0"(_res), "r" (lock));
126         return (int) _res;
127 }
128
129 #endif   /* __arm__ */
130
131 #if defined(__s390__)
132 /*
133  * S/390 Linux
134  */
135 #define TAS(lock)      tas(lock)
136
137 static inline int
138 tas(volatile slock_t *lock)
139 {
140  int _res;
141
142         __asm__ __volatile("    la    1,1\n"
143                            "    l     2,%2\n"
144                            "    slr   0,0\n"
145                            "    cs    0,1,0(2)\n"
146                            "    lr    %1,0"
147                            : "=m" (lock), "=d" (_res)
148                            : "m" (lock)
149                            : "0", "1", "2");
150
151        return (_res);
152 }
153 #endif  /* __s390__ */
154
155
156 #if defined(__sparc__)
157 #define TAS(lock) tas(lock)
158
159 static __inline__ int
160 tas(volatile slock_t *lock)
161 {
162         register slock_t _res = 1;
163
164         __asm__("ldstub [%2], %0" \
165 :                       "=r"(_res), "=m"(*lock) \
166 :                       "r"(lock));
167         return (int) _res;
168 }
169
170 #endif   /* __sparc__ */
171
172
173 #if defined(__mc68000__) && defined(__linux__)
174 #define TAS(lock) tas(lock)
175
176 static __inline__ int
177 tas(volatile slock_t *lock)
178 {
179         register int rv;
180         
181         __asm__ __volatile__ (
182                 "tas %1; sne %0"
183                 : "=d" (rv), "=m"(*lock)
184                 : "1" (*lock)
185                 : "cc" );
186         return rv;
187 }
188
189 #endif /* defined(__mc68000__) && defined(__linux__) */
190
191
192 #if defined(NEED_VAX_TAS_ASM)
193 /*
194  * VAXen -- even multiprocessor ones
195  * (thanks to Tom Ivar Helbekkmo)
196  */
197 #define TAS(lock) tas(lock)
198
199 typedef unsigned char slock_t;
200
201 static __inline__ int
202 tas(volatile slock_t *lock)
203 {
204         register        _res;
205
206         __asm__("       movl $1, r0 \
207                         bbssi $0, (%1), 1 f \
208                         clrl r0 \
209 1:                      movl r0, %0 "
210 :                       "=r"(_res)                      /* return value, in register */
211 :                       "r"(lock)                       /* argument, 'lock pointer', in register */
212 :                       "r0");                          /* inline code uses this register */
213         return (int) _res;
214 }
215
216 #endif   /* NEED_VAX_TAS_ASM */
217
218
219
220 #if defined(NEED_NS32K_TAS_ASM)
221 #define TAS(lock) tas(lock)
222
223 static __inline__ int
224 tas(volatile slock_t *lock)
225 {
226   register _res;
227   __asm__("sbitb 0, %0 \n\
228         sfsd %1"
229         : "=m"(*lock), "=r"(_res));
230   return (int) _res; 
231 }
232
233 #endif  /* NEED_NS32K_TAS_ASM */
234
235
236
237 #else                                                   /* __GNUC__ */
238 /***************************************************************************
239  * All non gcc
240  */
241
242 #if defined(__QNX__)
243 /*
244  * QNX 4
245  *
246  * Note that slock_t under QNX is sem_t instead of char
247  */
248 #define TAS(lock)       (sem_trywait((lock)) < 0)
249 #define S_UNLOCK(lock)  sem_post((lock))
250 #define S_INIT_LOCK(lock)       sem_init((lock), 1, 1)
251 #define S_LOCK_FREE(lock)       (lock)->value
252 #endif   /* __QNX__ */
253
254
255 #if defined(NEED_I386_TAS_ASM)
256 /* non gcc i386 based things */
257
258 #if defined(USE_UNIVEL_CC)
259 #define TAS(lock)       tas(lock)
260
261 asm int
262 tas(volatile slock_t *s_lock)
263 {
264 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
265 %mem s_lock
266         pushl %ebx
267         movl s_lock, %ebx
268         movl $255, %eax
269         lock
270         xchgb %al, (%ebx)
271         popl %ebx
272 }
273
274 #endif   /* USE_UNIVEL_CC */
275
276 #endif   /* NEED_I386_TAS_ASM */
277
278 #endif   /* defined(__GNUC__) */
279
280
281
282 /*************************************************************************
283  * These are the platforms that have common code for gcc and non-gcc
284  */
285
286
287 #if defined(__alpha)
288
289 #if defined(__osf__)
290 /*
291  * OSF/1 (Alpha AXP)
292  *
293  * Note that slock_t on the Alpha AXP is msemaphore instead of char
294  * (see storage/ipc.h).
295  */
296 #include <alpha/builtins.h>
297 #if 0
298 #define TAS(lock)         (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
299 #define S_UNLOCK(lock) msem_unlock((lock), 0)
300 #define S_INIT_LOCK(lock)               msem_init((lock), MSEM_UNLOCKED)
301 #define S_LOCK_FREE(lock)         (!(lock)->msem_state)
302 #else
303 #define TAS(lock)         (__INTERLOCKED_TESTBITSS_QUAD((lock),0))
304 #endif
305
306 #else /* i.e. not __osf__ */
307
308 #define TAS(lock) tas(lock)
309 #define S_UNLOCK(lock) do { __asm__("mb"); *(lock) = 0; } while (0)
310
311 static __inline__ int
312 tas(volatile slock_t *lock)
313 {
314  register slock_t _res;
315
316 __asm__("        ldq   $0, %0                      \n\
317                                  bne   $0, 3f              \n\
318                                  ldq_l $0, %0                    \n\
319                                  bne   $0, 3f              \n\
320                                  or    $31, 1, $0                  \n\
321                                  stq_c $0, %0                              \n\
322                                  beq   $0, 2f                      \n\
323                                  bis   $31, $31, %1        \n\
324                                  mb                                                        \n\
325                                  jmp   $31, 4f                     \n\
326                           2: or    $31, 1, $0                      \n\
327                           3: bis   $0, $0, %1              \n\
328                           4: nop          ": "=m"(*lock), "=r"(_res): :"0");
329
330         return (int) _res;
331 }
332 #endif /* __osf__ */
333
334 #endif /* __alpha */
335
336
337 #if defined(__hpux)
338 /*
339  * HP-UX (PA-RISC)
340  *
341  * Note that slock_t on PA-RISC is a structure instead of char
342  * (see include/port/hpux.h).
343  *
344  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
345  * all words set to non-zero. tas() in tas.s
346  */
347
348 #define S_UNLOCK(lock) \
349 do { \
350         volatile slock_t *lock_ = (volatile slock_t *) (lock); \
351         lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
352 } while (0)
353
354 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
355
356 #endif   /* __hpux */
357
358
359 #if defined(__sgi)
360 /*
361  * SGI IRIX 5
362  * slock_t is defined as a unsigned long. We use the standard SGI
363  * mutex API. 
364  *
365  * The following comment is left for historical reasons, but is probably
366  * not a good idea since the mutex ABI is supported.
367  *
368  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
369  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
370  * for the R3000 chips out there.
371  */
372 #include "mutex.h"
373 #define TAS(lock)       (test_and_set(lock,1))
374 #define S_UNLOCK(lock)  (test_then_and(lock,0))
375 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
376 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
377 #endif   /* __sgi */
378
379 #if defined(sinix)
380 /*
381  * SINIX / Reliant UNIX 
382  * slock_t is defined as a struct abilock_t, which has a single unsigned long
383  * member. (Basically same as SGI)
384  *
385  */
386 #define TAS(lock)       (!acquire_lock(lock))
387 #define S_UNLOCK(lock)  release_lock(lock)
388 #define S_INIT_LOCK(lock)       init_lock(lock)
389 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
390 #endif   /* sinix */
391  
392
393 #if defined(_AIX)
394 /*
395  * AIX (POWER)
396  *
397  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
398  * (see storage/ipc.h).
399  */
400 #define TAS(lock)       cs((int *) (lock), 0, 1)
401 #endif   /* _AIX */
402
403
404 #if defined (nextstep)
405 /*
406  * NEXTSTEP (mach)
407  * slock_t is defined as a struct mutex.
408  */
409
410 #define S_LOCK(lock)    mutex_lock(lock)
411 #define S_UNLOCK(lock)  mutex_unlock(lock)
412 #define S_INIT_LOCK(lock)       mutex_init(lock)
413 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
414 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
415 #endif   /* nextstep */
416
417
418
419
420 /****************************************************************************
421  * Default Definitions - override these above as needed.
422  */
423
424 #if !defined(S_LOCK)
425 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
426
427 #define S_LOCK(lock) \
428         do { \
429                 if (TAS((volatile slock_t *) (lock))) \
430                         s_lock((volatile slock_t *) (lock), __FILE__, __LINE__); \
431         } while (0)
432 #endif   /* S_LOCK */
433
434 #if !defined(S_LOCK_FREE)
435 #define S_LOCK_FREE(lock)       (*(lock) == 0)
436 #endif   /* S_LOCK_FREE */
437
438 #if !defined(S_UNLOCK)
439 #define S_UNLOCK(lock)          (*(lock) = 0)
440 #endif   /* S_UNLOCK */
441
442 #if !defined(S_INIT_LOCK)
443 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
444 #endif   /* S_INIT_LOCK */
445
446 #if !defined(TAS)
447 extern int      tas(volatile slock_t *lock);            /* port/.../tas.s, or
448                                                                                                  * s_lock.c */
449
450 #define TAS(lock)               tas((volatile slock_t *) (lock))
451 #endif   /* TAS */
452
453
454 #else    /* !HAS_TEST_AND_SET */
455
456 /*
457  * Fake spinlock implementation using SysV semaphores --- slow and prone
458  * to fall foul of kernel limits on number of semaphores, so don't use this
459  * unless you must!
460  */
461
462 typedef struct
463 {
464         /* reference to semaphore used to implement this spinlock */
465         IpcSemaphoreId  semId;
466         int                             sem;
467 } slock_t;
468
469 extern bool s_lock_free_sema(volatile slock_t *lock);
470 extern void s_unlock_sema(volatile slock_t *lock);
471 extern void s_init_lock_sema(volatile slock_t *lock);
472 extern int tas_sema(volatile slock_t *lock);
473
474 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
475
476 #define S_LOCK(lock) \
477         do { \
478                 if (TAS((volatile slock_t *) (lock))) \
479                         s_lock((volatile slock_t *) (lock), __FILE__, __LINE__); \
480         } while (0)
481
482 #define S_LOCK_FREE(lock)   s_lock_free_sema(lock)
483 #define S_UNLOCK(lock)   s_unlock_sema(lock)
484 #define S_INIT_LOCK(lock)   s_init_lock_sema(lock)
485 #define TAS(lock)   tas_sema(lock)
486
487 #endif   /* HAS_TEST_AND_SET */
488
489 #endif   /* S_LOCK_H */