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