]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Add Linux ARM.
[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.65 1999/11/06 01:55:43 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 #if defined(__alpha__)
82 #define TAS(lock) tas(lock)
83 #define S_UNLOCK(lock) { __asm__("mb"); *(lock) = 0; }
84
85 static __inline__ int
86 tas(volatile slock_t *lock)
87 {
88         register slock_t _res;
89
90 __asm__("    ldq   $0, %0              \n\
91                  bne   $0, 3f          \n\
92                  ldq_l $0, %0              \n\
93                  bne   $0, 3f          \n\
94                  or    $31, 1, $0          \n\
95                  stq_c $0, %0              \n\
96                  beq   $0, 2f              \n\
97                  bis   $31, $31, %1        \n\
98                  mb                                \n\
99                  jmp   $31, 4f             \n\
100               2: or    $31, 1, $0              \n\
101               3: bis   $0, $0, %1              \n\
102               4: nop      ": "=m"(*lock), "=r"(_res): :"0");
103
104         return (int) _res;
105 }
106
107 #endif   /* __alpha__ */
108
109
110
111 #if defined(__i386__)
112 #define TAS(lock) tas(lock)
113
114 static __inline__ int
115 tas(volatile slock_t *lock)
116 {
117         register slock_t _res = 1;
118
119 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
120         return (int) _res;
121 }
122
123 #endif   /* __i386__ */
124
125
126
127 #if defined(__arm32__) || defined(__arm__)
128 #define TAS(lock) tas(lock)
129
130 static __inline__ int
131 tas(volatile slock_t *lock)
132 {
133         register slock_t _res = 1;
134
135 __asm__("swpb %0, %0, [%3]": "=r"(_res), "=m"(*lock):"0"(_res), "r" (lock));
136         return (int) _res;
137 }
138
139 #endif   /* __arm32__ */
140
141
142
143 #if defined(__sparc__)
144 #define TAS(lock) tas(lock)
145
146 static __inline__ int
147 tas(volatile slock_t *lock)
148 {
149         register slock_t _res = 1;
150
151         __asm__("ldstub [%2], %0" \
152 :                       "=r"(_res), "=m"(*lock) \
153 :                       "r"(lock));
154         return (int) _res;
155 }
156
157 #endif   /* __sparc__ */
158
159
160 #if defined(__mc68000__) && defined(__linux__)
161 #define TAS(lock) tas(lock)
162
163 static __inline__ int
164 tas(volatile slock_t *lock)
165 {
166         register int rv;
167         
168         __asm__ __volatile__ (
169                 "tas %1; sne %0"
170                 : "=d" (rv), "=m"(*lock)
171                 : "1" (*lock)
172                 : "cc" );
173         return rv;
174 }
175
176 #endif /* defined(__mc68000__) && defined(__linux__) */
177
178
179 #if defined(NEED_VAX_TAS_ASM)
180 /*
181  * VAXen -- even multiprocessor ones
182  * (thanks to Tom Ivar Helbekkmo)
183  */
184 #define TAS(lock) tas(lock)
185
186 typedef unsigned char slock_t;
187
188 static __inline__ int
189 tas(volatile slock_t *lock)
190 {
191         register        _res;
192
193         __asm__("       movl $1, r0 \
194                         bbssi $0, (%1), 1 f \
195                         clrl r0 \
196 1:                      movl r0, %0 "
197 :                       "=r"(_res)                      /* return value, in register */
198 :                       "r"(lock)                       /* argument, 'lock pointer', in register */
199 :                       "r0");                          /* inline code uses this register */
200         return (int) _res;
201 }
202
203 #endif   /* NEED_VAX_TAS_ASM */
204
205
206
207 #if defined(NEED_NS32K_TAS_ASM)
208 #define TAS(lock) tas(lock)
209
210 static __inline__ int
211 tas(volatile slock_t *lock)
212 {
213   register _res;
214   __asm__("sbitb 0, %0 \n\
215         sfsd %1"
216         : "=m"(*lock), "=r"(_res));
217   return (int) _res; 
218 }
219
220 #endif  /* NEED_NS32K_TAS_ASM */
221
222
223
224 #else                                                   /* __GNUC__ */
225 /***************************************************************************
226  * All non gcc
227  */
228
229 #if defined(__alpha__)
230 /*
231  * OSF/1 (Alpha AXP)
232  *
233  * Note that slock_t on the Alpha AXP is msemaphore instead of char
234  * (see storage/ipc.h).
235  */
236 #define TAS(lock)       (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
237 #define S_UNLOCK(lock)  msem_unlock((lock), 0)
238 #define S_INIT_LOCK(lock)       msem_init((lock), MSEM_UNLOCKED)
239 #define S_LOCK_FREE(lock)       (!(lock)->msem_state)
240 #endif   /* __alpha__ */
241
242
243
244 #if defined(NEED_I386_TAS_ASM)
245 /* non gcc i386 based things */
246
247 #if defined(USE_UNIVEL_CC)
248 #define TAS(lock)       tas(lock)
249
250 asm int
251 tas(slock_t *s_lock)
252 {
253 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
254 %mem s_lock
255         pushl %ebx
256         movl s_lock, %ebx
257         movl $255, %eax
258         lock
259         xchgb %al, (%ebx)
260         popl %ebx
261 }
262
263 #endif   /* USE_UNIVEL_CC */
264
265 #endif   /* NEED_I386_TAS_ASM */
266
267 #endif   /* defined(__GNUC__) */
268
269
270
271 /*************************************************************************
272  * These are the platforms that have common code for gcc and non-gcc
273  */
274
275 #if defined(__hpux)
276 /*
277  * HP-UX (PA-RISC)
278  *
279  * Note that slock_t on PA-RISC is a structure instead of char
280  * (see include/port/hpux.h).
281  *
282  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
283  * all words set to non-zero. tas() in tas.s
284  */
285
286 #define S_UNLOCK(lock) \
287 { \
288         volatile slock_t *lock_ = (volatile slock_t *) (lock); \
289         lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
290 }
291
292 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
293
294 #endif   /* __hpux */
295
296
297 #if defined(__sgi)
298 /*
299  * SGI IRIX 5
300  * slock_t is defined as a unsigned long. We use the standard SGI
301  * mutex API. 
302  *
303  * The following comment is left for historical reasons, but is probably
304  * not a good idea since the mutex ABI is supported.
305  *
306  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
307  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
308  * for the R3000 chips out there.
309  */
310 #include "mutex.h"
311 #define TAS(lock)       (test_and_set(lock,1))
312 #define S_UNLOCK(lock)  (test_then_and(lock,0))
313 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
314 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
315 #endif   /* __sgi */
316
317 #if defined(sinix)
318 /*
319  * SINIX / Reliant UNIX 
320  * slock_t is defined as a struct abilock_t, which has a single unsigned long
321  * member. (Basically same as SGI)
322  *
323  */
324 #define TAS(lock)       (!acquire_lock(lock))
325 #define S_UNLOCK(lock)  release_lock(lock)
326 #define S_INIT_LOCK(lock)       init_lock(lock)
327 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
328 #endif   /* sinix */
329  
330
331 #if defined(_AIX)
332 /*
333  * AIX (POWER)
334  *
335  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
336  * (see storage/ipc.h).
337  */
338 #define TAS(lock)       cs((int *) (lock), 0, 1)
339 #endif   /* _AIX */
340
341
342 #if defined (nextstep)
343 /*
344  * NEXTSTEP (mach)
345  * slock_t is defined as a struct mutex.
346  */
347
348 #define S_LOCK(lock)    mutex_lock(lock)
349 #define S_UNLOCK(lock)  mutex_unlock(lock)
350 #define S_INIT_LOCK(lock)       mutex_init(lock)
351 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
352 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
353 #endif   /* nextstep */
354
355
356
357
358 /****************************************************************************
359  * Default Definitions - override these above as needed.
360  */
361
362 #if !defined(S_LOCK)
363 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
364
365 #define S_LOCK(lock) \
366         do { \
367                 if (TAS((volatile slock_t *) lock)) \
368                         s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
369         } while (0)
370 #endif   /* S_LOCK */
371
372 #if !defined(S_LOCK_FREE)
373 #define S_LOCK_FREE(lock)       (*(lock) == 0)
374 #endif   /* S_LOCK_FREE */
375
376 #if !defined(S_UNLOCK)
377 #define S_UNLOCK(lock)          (*(lock) = 0)
378 #endif   /* S_UNLOCK */
379
380 #if !defined(S_INIT_LOCK)
381 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
382 #endif   /* S_INIT_LOCK */
383
384 #if !defined(TAS)
385 int                     tas(volatile slock_t *lock);            /* port/.../tas.s, or
386                                                                                                  * s_lock.c */
387
388 #define TAS(lock)               tas((volatile slock_t *) lock)
389 #endif   /* TAS */
390
391 #endif   /* HAS_TEST_AND_SET */
392 #endif   /* S_LOCK_H */
393