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
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
54 #if defined(SPARC_SPINLOCKS)
56 static inline int __spin_trylock(spinlock_t *lock)
60 asm volatile("ldstub [%1], %0"
65 return (result == 0) ? 0 : EBUSY;
68 static inline void __spin_unlock(spinlock_t *lock)
70 asm volatile("":::"memory");
74 static inline void __spin_lock_init(spinlock_t *lock)
79 static inline int __spin_is_locked(spinlock_t *lock)
84 #elif defined(POWERPC_SPINLOCKS)
86 static inline int __spin_trylock(spinlock_t *lock)
103 return (result == 1) ? 0 : EBUSY;
106 static inline void __spin_unlock(spinlock_t *lock)
108 asm volatile("eieio":::"memory");
112 static inline void __spin_lock_init(spinlock_t *lock)
117 static inline int __spin_is_locked(spinlock_t *lock)
122 #elif defined(INTEL_SPINLOCKS)
124 static inline int __spin_trylock(spinlock_t *lock)
128 asm volatile("xchgl %0,%1"
129 : "=r" (oldval), "=m" (*lock)
133 return oldval > 0 ? 0 : EBUSY;
136 static inline void __spin_unlock(spinlock_t *lock)
138 asm volatile("":::"memory");
142 static inline void __spin_lock_init(spinlock_t *lock)
147 static inline int __spin_is_locked(spinlock_t *lock)
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.
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)
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)
189 val = __add_and_fetch(lock, 0);
193 #elif defined(MIPS_SPINLOCKS)
195 static inline unsigned int load_linked(unsigned long addr)
199 __asm__ __volatile__("ll\t%0,(%1)"
206 static inline unsigned int store_conditional(unsigned long addr, unsigned int value)
210 __asm__ __volatile__("sc\t%0,(%2)"
212 : "0" (value), "r" (addr));
216 static inline int __spin_trylock(spinlock_t *lock)
221 mw = load_linked(lock);
224 } while (!store_conditional(lock, 1));
226 asm volatile("":::"memory");
231 static inline void __spin_unlock(spinlock_t *lock)
233 asm volatile("":::"memory");
237 static inline void __spin_lock_init(spinlock_t *lock)
242 static inline int __spin_is_locked(spinlock_t *lock)
248 #error Need to implement spinlock code in spinlock.c
255 static void yield_cpu(void)
259 #ifdef USE_SCHED_YIELD
262 /* Linux will busy loop for delays < 2ms on real time tasks */
264 tm.tv_nsec = 2000000L + 1;
265 nanosleep(&tm, NULL);
273 static int smp_machine = 0;
275 static inline void __spin_lock(spinlock_t *lock)
279 while(__spin_trylock(lock)) {
280 while(__spin_is_locked(lock)) {
281 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
288 static void __read_lock(tdb_rwlock_t *rwlock)
293 __spin_lock(&rwlock->lock);
295 if (!(rwlock->count & RWLOCK_BIAS)) {
297 __spin_unlock(&rwlock->lock);
301 __spin_unlock(&rwlock->lock);
303 while(rwlock->count & RWLOCK_BIAS) {
304 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
311 static void __write_lock(tdb_rwlock_t *rwlock)
316 __spin_lock(&rwlock->lock);
318 if (rwlock->count == 0) {
319 rwlock->count |= RWLOCK_BIAS;
320 __spin_unlock(&rwlock->lock);
324 __spin_unlock(&rwlock->lock);
326 while(rwlock->count != 0) {
327 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
334 static void __write_unlock(tdb_rwlock_t *rwlock)
336 __spin_lock(&rwlock->lock);
339 if (!(rwlock->count & RWLOCK_BIAS))
340 fprintf(stderr, "bug: write_unlock\n");
343 rwlock->count &= ~RWLOCK_BIAS;
344 __spin_unlock(&rwlock->lock);
347 static void __read_unlock(tdb_rwlock_t *rwlock)
349 __spin_lock(&rwlock->lock);
353 fprintf(stderr, "bug: read_unlock\n");
355 if (rwlock->count & RWLOCK_BIAS)
356 fprintf(stderr, "bug: read_unlock\n");
360 __spin_unlock(&rwlock->lock);
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);
375 __read_lock(&rwlocks[list+1]);
379 __write_lock(&rwlocks[list+1]);
383 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
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);
398 __read_unlock(&rwlocks[list+1]);
402 __write_unlock(&rwlocks[list+1]);
406 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
412 int tdb_create_rwlocks(int fd, unsigned int hash_size)
415 tdb_rwlock_t *rwlocks;
417 size = TDB_SPINLOCK_SIZE(hash_size);
418 rwlocks = malloc(size);
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) {
432 smp_machine = this_is_smp();
437 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
439 tdb_rwlock_t *rwlocks;
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;
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))