Samba 3 configuration: preset failing tests, remove unused parameters, don't disable...
[tomato.git] / release / src / router / samba3 / source / tdb / spinlock.c
blobe42a6901c71a166e40be6f3f86444ba9b7dece82
1 /*
2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Anton Blanchard 2001
8 ** NOTE! The following LGPL license applies to the tdb
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #if HAVE_CONFIG_H
27 #include <config.h>
28 #endif
30 #ifdef STANDALONE
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <time.h>
39 #include <signal.h>
40 #include "tdb.h"
41 #include "spinlock.h"
43 #define DEBUG
44 #else
45 #include "includes.h"
46 #endif
48 #ifdef USE_SPINLOCKS
51 * ARCH SPECIFIC
54 #if defined(SPARC_SPINLOCKS)
56 static inline int __spin_trylock(spinlock_t *lock)
58 unsigned int result;
60 asm volatile("ldstub [%1], %0"
61 : "=r" (result)
62 : "r" (lock)
63 : "memory");
65 return (result == 0) ? 0 : EBUSY;
68 static inline void __spin_unlock(spinlock_t *lock)
70 asm volatile("":::"memory");
71 *lock = 0;
74 static inline void __spin_lock_init(spinlock_t *lock)
76 *lock = 0;
79 static inline int __spin_is_locked(spinlock_t *lock)
81 return (*lock != 0);
84 #elif defined(POWERPC_SPINLOCKS)
86 static inline int __spin_trylock(spinlock_t *lock)
88 unsigned int result;
90 __asm__ __volatile__(
91 "1: lwarx %0,0,%1\n\
92 cmpwi 0,%0,0\n\
93 li %0,0\n\
94 bne- 2f\n\
95 li %0,1\n\
96 stwcx. %0,0,%1\n\
97 bne- 1b\n\
98 isync\n\
99 2:" : "=&r"(result)
100 : "r"(lock)
101 : "cr0", "memory");
103 return (result == 1) ? 0 : EBUSY;
106 static inline void __spin_unlock(spinlock_t *lock)
108 asm volatile("eieio":::"memory");
109 *lock = 0;
112 static inline void __spin_lock_init(spinlock_t *lock)
114 *lock = 0;
117 static inline int __spin_is_locked(spinlock_t *lock)
119 return (*lock != 0);
122 #elif defined(INTEL_SPINLOCKS)
124 static inline int __spin_trylock(spinlock_t *lock)
126 int oldval;
128 asm volatile("xchgl %0,%1"
129 : "=r" (oldval), "=m" (*lock)
130 : "0" (0)
131 : "memory");
133 return oldval > 0 ? 0 : EBUSY;
136 static inline void __spin_unlock(spinlock_t *lock)
138 asm volatile("":::"memory");
139 *lock = 1;
142 static inline void __spin_lock_init(spinlock_t *lock)
144 *lock = 1;
147 static inline int __spin_is_locked(spinlock_t *lock)
149 return (*lock != 1);
152 #elif defined(MIPS_SPINLOCKS) && defined(sgi) && (_COMPILER_VERSION >= 730)
154 /* Implement spinlocks on IRIX using the MIPSPro atomic fetch operations. See
155 * sync(3) for the details of the intrinsic operations.
157 * "sgi" and "_COMPILER_VERSION" are always defined by MIPSPro.
160 #ifdef STANDALONE
162 /* MIPSPro 7.3 has "__inline" as an extension, but not "inline. */
163 #define inline __inline
165 #endif /* STANDALONE */
167 /* Returns 0 if the lock is acquired, EBUSY otherwise. */
168 static inline int __spin_trylock(spinlock_t *lock)
170 unsigned int val;
171 val = __lock_test_and_set(lock, 1);
172 return val == 0 ? 0 : EBUSY;
175 static inline void __spin_unlock(spinlock_t *lock)
177 __lock_release(lock);
180 static inline void __spin_lock_init(spinlock_t *lock)
182 __lock_release(lock);
185 /* Returns 1 if the lock is held, 0 otherwise. */
186 static inline int __spin_is_locked(spinlock_t *lock)
188 unsigned int val;
189 val = __add_and_fetch(lock, 0);
190 return val;
193 #elif defined(MIPS_SPINLOCKS)
195 static inline unsigned int load_linked(unsigned long addr)
197 unsigned int res;
199 __asm__ __volatile__("ll\t%0,(%1)"
200 : "=r" (res)
201 : "r" (addr));
203 return res;
206 static inline unsigned int store_conditional(unsigned long addr, unsigned int value)
208 unsigned int res;
210 __asm__ __volatile__("sc\t%0,(%2)"
211 : "=r" (res)
212 : "0" (value), "r" (addr));
213 return res;
216 static inline int __spin_trylock(spinlock_t *lock)
218 unsigned int mw;
220 do {
221 mw = load_linked(lock);
222 if (mw)
223 return EBUSY;
224 } while (!store_conditional(lock, 1));
226 asm volatile("":::"memory");
228 return 0;
231 static inline void __spin_unlock(spinlock_t *lock)
233 asm volatile("":::"memory");
234 *lock = 0;
237 static inline void __spin_lock_init(spinlock_t *lock)
239 *lock = 0;
242 static inline int __spin_is_locked(spinlock_t *lock)
244 return (*lock != 0);
247 #else
248 #error Need to implement spinlock code in spinlock.c
249 #endif
252 * OS SPECIFIC
255 static void yield_cpu(void)
257 struct timespec tm;
259 #ifdef USE_SCHED_YIELD
260 sched_yield();
261 #else
262 /* Linux will busy loop for delays < 2ms on real time tasks */
263 tm.tv_sec = 0;
264 tm.tv_nsec = 2000000L + 1;
265 nanosleep(&tm, NULL);
266 #endif
270 * GENERIC
273 static int smp_machine = 0;
275 static inline void __spin_lock(spinlock_t *lock)
277 int ntries = 0;
279 while(__spin_trylock(lock)) {
280 while(__spin_is_locked(lock)) {
281 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
282 continue;
283 yield_cpu();
288 static void __read_lock(tdb_rwlock_t *rwlock)
290 int ntries = 0;
292 while(1) {
293 __spin_lock(&rwlock->lock);
295 if (!(rwlock->count & RWLOCK_BIAS)) {
296 rwlock->count++;
297 __spin_unlock(&rwlock->lock);
298 return;
301 __spin_unlock(&rwlock->lock);
303 while(rwlock->count & RWLOCK_BIAS) {
304 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
305 continue;
306 yield_cpu();
311 static void __write_lock(tdb_rwlock_t *rwlock)
313 int ntries = 0;
315 while(1) {
316 __spin_lock(&rwlock->lock);
318 if (rwlock->count == 0) {
319 rwlock->count |= RWLOCK_BIAS;
320 __spin_unlock(&rwlock->lock);
321 return;
324 __spin_unlock(&rwlock->lock);
326 while(rwlock->count != 0) {
327 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
328 continue;
329 yield_cpu();
334 static void __write_unlock(tdb_rwlock_t *rwlock)
336 __spin_lock(&rwlock->lock);
338 #ifdef DEBUG
339 if (!(rwlock->count & RWLOCK_BIAS))
340 fprintf(stderr, "bug: write_unlock\n");
341 #endif
343 rwlock->count &= ~RWLOCK_BIAS;
344 __spin_unlock(&rwlock->lock);
347 static void __read_unlock(tdb_rwlock_t *rwlock)
349 __spin_lock(&rwlock->lock);
351 #ifdef DEBUG
352 if (!rwlock->count)
353 fprintf(stderr, "bug: read_unlock\n");
355 if (rwlock->count & RWLOCK_BIAS)
356 fprintf(stderr, "bug: read_unlock\n");
357 #endif
359 rwlock->count--;
360 __spin_unlock(&rwlock->lock);
363 /* TDB SPECIFIC */
365 /* lock a list in the database. list -1 is the alloc list */
366 int tdb_spinlock(TDB_CONTEXT *tdb, int list, int rw_type)
368 tdb_rwlock_t *rwlocks;
370 if (!tdb->map_ptr) return -1;
371 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
373 switch(rw_type) {
374 case F_RDLCK:
375 __read_lock(&rwlocks[list+1]);
376 break;
378 case F_WRLCK:
379 __write_lock(&rwlocks[list+1]);
380 break;
382 default:
383 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
385 return 0;
388 /* unlock the database. */
389 int tdb_spinunlock(TDB_CONTEXT *tdb, int list, int rw_type)
391 tdb_rwlock_t *rwlocks;
393 if (!tdb->map_ptr) return -1;
394 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
396 switch(rw_type) {
397 case F_RDLCK:
398 __read_unlock(&rwlocks[list+1]);
399 break;
401 case F_WRLCK:
402 __write_unlock(&rwlocks[list+1]);
403 break;
405 default:
406 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
409 return 0;
412 int tdb_create_rwlocks(int fd, unsigned int hash_size)
414 unsigned size, i;
415 tdb_rwlock_t *rwlocks;
417 size = TDB_SPINLOCK_SIZE(hash_size);
418 rwlocks = malloc(size);
419 if (!rwlocks)
420 return -1;
422 for(i = 0; i < hash_size+1; i++) {
423 __spin_lock_init(&rwlocks[i].lock);
424 rwlocks[i].count = 0;
427 /* Write it out (appending to end) */
428 if (write(fd, rwlocks, size) != size) {
429 free(rwlocks);
430 return -1;
432 smp_machine = this_is_smp();
433 free(rwlocks);
434 return 0;
437 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
439 tdb_rwlock_t *rwlocks;
440 unsigned i;
442 if (tdb->header.rwlocks == 0) return 0;
443 if (!tdb->map_ptr) return -1;
445 /* We're mmapped here */
446 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
447 for(i = 0; i < tdb->header.hash_size+1; i++) {
448 __spin_lock_init(&rwlocks[i].lock);
449 rwlocks[i].count = 0;
451 return 0;
453 #else
454 int tdb_create_rwlocks(int fd, unsigned int hash_size) { return 0; }
455 int tdb_spinlock(TDB_CONTEXT *tdb, int list, int rw_type) { return -1; }
456 int tdb_spinunlock(TDB_CONTEXT *tdb, int list, int rw_type) { return -1; }
458 /* Non-spinlock version: remove spinlock pointer */
459 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
461 tdb_off off = (tdb_off)((char *)&tdb->header.rwlocks
462 - (char *)&tdb->header);
464 tdb->header.rwlocks = 0;
465 if (lseek(tdb->fd, off, SEEK_SET) != off
466 || write(tdb->fd, (void *)&tdb->header.rwlocks,
467 sizeof(tdb->header.rwlocks))
468 != sizeof(tdb->header.rwlocks))
469 return -1;
470 return 0;
472 #endif