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
51 #if defined(SPARC_SPINLOCKS)
53 static inline int __spin_trylock(spinlock_t *lock)
57 asm volatile("ldstub [%1], %0"
62 return (result == 0) ? 0 : EBUSY;
65 static inline void __spin_unlock(spinlock_t *lock)
67 asm volatile("":::"memory");
71 static inline void __spin_lock_init(spinlock_t *lock)
76 static inline int __spin_is_locked(spinlock_t *lock)
81 #elif defined(POWERPC_SPINLOCKS)
83 static inline int __spin_trylock(spinlock_t *lock)
100 return (result == 1) ? 0 : EBUSY;
103 static inline void __spin_unlock(spinlock_t *lock)
105 asm volatile("eieio":::"memory");
109 static inline void __spin_lock_init(spinlock_t *lock)
114 static inline int __spin_is_locked(spinlock_t *lock)
119 #elif defined(INTEL_SPINLOCKS)
121 static inline int __spin_trylock(spinlock_t *lock)
125 asm volatile("xchgl %0,%1"
126 : "=r" (oldval), "=m" (*lock)
130 return oldval > 0 ? 0 : EBUSY;
133 static inline void __spin_unlock(spinlock_t *lock)
135 asm volatile("":::"memory");
139 static inline void __spin_lock_init(spinlock_t *lock)
144 static inline int __spin_is_locked(spinlock_t *lock)
149 #elif defined(MIPS_SPINLOCKS) && defined(sgi) && (_COMPILER_VERSION >= 730)
151 /* Implement spinlocks on IRIX using the MIPSPro atomic fetch operations. See
152 * sync(3) for the details of the intrinsic operations.
154 * "sgi" and "_COMPILER_VERSION" are always defined by MIPSPro.
159 /* MIPSPro 7.3 has "__inline" as an extension, but not "inline. */
160 #define inline __inline
162 #endif /* STANDALONE */
164 /* Returns 0 if the lock is acquired, EBUSY otherwise. */
165 static inline int __spin_trylock(spinlock_t *lock)
168 val = __lock_test_and_set(lock, 1);
169 return val == 0 ? 0 : EBUSY;
172 static inline void __spin_unlock(spinlock_t *lock)
174 __lock_release(lock);
177 static inline void __spin_lock_init(spinlock_t *lock)
179 __lock_release(lock);
182 /* Returns 1 if the lock is held, 0 otherwise. */
183 static inline int __spin_is_locked(spinlock_t *lock)
186 val = __add_and_fetch(lock, 0);
190 #elif defined(MIPS_SPINLOCKS)
192 static inline unsigned int load_linked(unsigned long addr)
196 __asm__ __volatile__("ll\t%0,(%1)"
203 static inline unsigned int store_conditional(unsigned long addr, unsigned int value)
207 __asm__ __volatile__("sc\t%0,(%2)"
209 : "0" (value), "r" (addr));
213 static inline int __spin_trylock(spinlock_t *lock)
218 mw = load_linked(lock);
221 } while (!store_conditional(lock, 1));
223 asm volatile("":::"memory");
228 static inline void __spin_unlock(spinlock_t *lock)
230 asm volatile("":::"memory");
234 static inline void __spin_lock_init(spinlock_t *lock)
239 static inline int __spin_is_locked(spinlock_t *lock)
245 #error Need to implement spinlock code in spinlock.c
252 static void yield_cpu(void)
256 #ifdef USE_SCHED_YIELD
259 /* Linux will busy loop for delays < 2ms on real time tasks */
261 tm.tv_nsec = 2000000L + 1;
262 nanosleep(&tm, NULL);
266 static int this_is_smp(void)
268 #if defined(HAVE_SYSCONF) && defined(SYSCONF_SC_NPROC_ONLN)
269 return (sysconf(_SC_NPROC_ONLN) > 1) ? 1 : 0;
279 static int smp_machine = 0;
281 static inline void __spin_lock(spinlock_t *lock)
285 while(__spin_trylock(lock)) {
286 while(__spin_is_locked(lock)) {
287 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
294 static void __read_lock(tdb_rwlock_t *rwlock)
299 __spin_lock(&rwlock->lock);
301 if (!(rwlock->count & RWLOCK_BIAS)) {
303 __spin_unlock(&rwlock->lock);
307 __spin_unlock(&rwlock->lock);
309 while(rwlock->count & RWLOCK_BIAS) {
310 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
317 static void __write_lock(tdb_rwlock_t *rwlock)
322 __spin_lock(&rwlock->lock);
324 if (rwlock->count == 0) {
325 rwlock->count |= RWLOCK_BIAS;
326 __spin_unlock(&rwlock->lock);
330 __spin_unlock(&rwlock->lock);
332 while(rwlock->count != 0) {
333 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
340 static void __write_unlock(tdb_rwlock_t *rwlock)
342 __spin_lock(&rwlock->lock);
345 if (!(rwlock->count & RWLOCK_BIAS))
346 fprintf(stderr, "bug: write_unlock\n");
349 rwlock->count &= ~RWLOCK_BIAS;
350 __spin_unlock(&rwlock->lock);
353 static void __read_unlock(tdb_rwlock_t *rwlock)
355 __spin_lock(&rwlock->lock);
359 fprintf(stderr, "bug: read_unlock\n");
361 if (rwlock->count & RWLOCK_BIAS)
362 fprintf(stderr, "bug: read_unlock\n");
366 __spin_unlock(&rwlock->lock);
371 /* lock a list in the database. list -1 is the alloc list */
372 int tdb_spinlock(TDB_CONTEXT *tdb, int list, int rw_type)
374 tdb_rwlock_t *rwlocks;
376 if (!tdb->map_ptr) return -1;
377 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
381 __read_lock(&rwlocks[list+1]);
385 __write_lock(&rwlocks[list+1]);
389 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
394 /* unlock the database. */
395 int tdb_spinunlock(TDB_CONTEXT *tdb, int list, int rw_type)
397 tdb_rwlock_t *rwlocks;
399 if (!tdb->map_ptr) return -1;
400 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
404 __read_unlock(&rwlocks[list+1]);
408 __write_unlock(&rwlocks[list+1]);
412 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
418 int tdb_create_rwlocks(int fd, unsigned int hash_size)
421 tdb_rwlock_t *rwlocks;
423 size = TDB_SPINLOCK_SIZE(hash_size);
424 rwlocks = malloc(size);
428 for(i = 0; i < hash_size+1; i++) {
429 __spin_lock_init(&rwlocks[i].lock);
430 rwlocks[i].count = 0;
433 /* Write it out (appending to end) */
434 if (write(fd, rwlocks, size) != size) {
438 smp_machine = this_is_smp();
443 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
445 tdb_rwlock_t *rwlocks;
448 if (tdb->header.rwlocks == 0) return 0;
449 if (!tdb->map_ptr) return -1;
451 /* We're mmapped here */
452 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
453 for(i = 0; i < tdb->header.hash_size+1; i++) {
454 __spin_lock_init(&rwlocks[i].lock);
455 rwlocks[i].count = 0;
460 int tdb_create_rwlocks(int fd, unsigned int hash_size) { return 0; }
461 int tdb_spinlock(TDB_CONTEXT *tdb, int list, int rw_type) { return -1; }
462 int tdb_spinunlock(TDB_CONTEXT *tdb, int list, int rw_type) { return -1; }
464 /* Non-spinlock version: remove spinlock pointer */
465 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
467 tdb_off off = (tdb_off)((char *)&tdb->header.rwlocks
468 - (char *)&tdb->header);
470 tdb->header.rwlocks = 0;
471 if (lseek(tdb->fd, off, SEEK_SET) != off
472 || write(tdb->fd, (void *)&tdb->header.rwlocks,
473 sizeof(tdb->header.rwlocks))
474 != sizeof(tdb->header.rwlocks))