2 Unix SMB/CIFS implementation.
3 Samba database functions
4 Copyright (C) Anton Blanchard 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 #if defined(SPARC_SPINLOCKS)
50 static inline int __spin_trylock(spinlock_t *lock)
54 asm volatile("ldstub [%1], %0"
59 return (result == 0) ? 0 : EBUSY;
62 static inline void __spin_unlock(spinlock_t *lock)
64 asm volatile("":::"memory");
68 static inline void __spin_lock_init(spinlock_t *lock)
73 static inline int __spin_is_locked(spinlock_t *lock)
78 #elif defined(POWERPC_SPINLOCKS)
80 static inline int __spin_trylock(spinlock_t *lock)
97 return (result == 1) ? 0 : EBUSY;
100 static inline void __spin_unlock(spinlock_t *lock)
102 asm volatile("eieio":::"memory");
106 static inline void __spin_lock_init(spinlock_t *lock)
111 static inline int __spin_is_locked(spinlock_t *lock)
116 #elif defined(INTEL_SPINLOCKS)
118 static inline int __spin_trylock(spinlock_t *lock)
122 asm volatile("xchgl %0,%1"
123 : "=r" (oldval), "=m" (*lock)
127 return oldval > 0 ? 0 : EBUSY;
130 static inline void __spin_unlock(spinlock_t *lock)
132 asm volatile("":::"memory");
136 static inline void __spin_lock_init(spinlock_t *lock)
141 static inline int __spin_is_locked(spinlock_t *lock)
146 #elif defined(MIPS_SPINLOCKS)
148 static inline unsigned int load_linked(unsigned long addr)
152 __asm__ __volatile__("ll\t%0,(%1)"
159 static inline unsigned int store_conditional(unsigned long addr, unsigned int value)
163 __asm__ __volatile__("sc\t%0,(%2)"
165 : "0" (value), "r" (addr));
169 static inline int __spin_trylock(spinlock_t *lock)
174 mw = load_linked(lock);
177 } while (!store_conditional(lock, 1));
179 asm volatile("":::"memory");
184 static inline void __spin_unlock(spinlock_t *lock)
186 asm volatile("":::"memory");
190 static inline void __spin_lock_init(spinlock_t *lock)
195 static inline int __spin_is_locked(spinlock_t *lock)
201 #error Need to implement spinlock code in spinlock.c
208 static void yield_cpu(void)
212 #ifdef USE_SCHED_YIELD
215 /* Linux will busy loop for delays < 2ms on real time tasks */
217 tm.tv_nsec = 2000000L + 1;
218 nanosleep(&tm, NULL);
222 static int this_is_smp(void)
231 static int smp_machine = 0;
233 static inline void __spin_lock(spinlock_t *lock)
237 while(__spin_trylock(lock)) {
238 while(__spin_is_locked(lock)) {
239 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
246 static void __read_lock(tdb_rwlock_t *rwlock)
251 __spin_lock(&rwlock->lock);
253 if (!(rwlock->count & RWLOCK_BIAS)) {
255 __spin_unlock(&rwlock->lock);
259 __spin_unlock(&rwlock->lock);
261 while(rwlock->count & RWLOCK_BIAS) {
262 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
269 static void __write_lock(tdb_rwlock_t *rwlock)
274 __spin_lock(&rwlock->lock);
276 if (rwlock->count == 0) {
277 rwlock->count |= RWLOCK_BIAS;
278 __spin_unlock(&rwlock->lock);
282 __spin_unlock(&rwlock->lock);
284 while(rwlock->count != 0) {
285 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
292 static void __write_unlock(tdb_rwlock_t *rwlock)
294 __spin_lock(&rwlock->lock);
297 if (!(rwlock->count & RWLOCK_BIAS))
298 fprintf(stderr, "bug: write_unlock\n");
301 rwlock->count &= ~RWLOCK_BIAS;
302 __spin_unlock(&rwlock->lock);
305 static void __read_unlock(tdb_rwlock_t *rwlock)
307 __spin_lock(&rwlock->lock);
311 fprintf(stderr, "bug: read_unlock\n");
313 if (rwlock->count & RWLOCK_BIAS)
314 fprintf(stderr, "bug: read_unlock\n");
318 __spin_unlock(&rwlock->lock);
323 /* lock a list in the database. list -1 is the alloc list */
324 int tdb_spinlock(TDB_CONTEXT *tdb, int list, int rw_type)
326 tdb_rwlock_t *rwlocks;
328 if (!tdb->map_ptr) return -1;
329 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
333 __read_lock(&rwlocks[list+1]);
337 __write_lock(&rwlocks[list+1]);
341 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
346 /* unlock the database. */
347 int tdb_spinunlock(TDB_CONTEXT *tdb, int list, int rw_type)
349 tdb_rwlock_t *rwlocks;
351 if (!tdb->map_ptr) return -1;
352 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
356 __read_unlock(&rwlocks[list+1]);
360 __write_unlock(&rwlocks[list+1]);
364 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
370 int tdb_create_rwlocks(int fd, unsigned int hash_size)
373 tdb_rwlock_t *rwlocks;
375 size = (hash_size + 1) * sizeof(tdb_rwlock_t);
376 rwlocks = malloc(size);
380 for(i = 0; i < hash_size+1; i++) {
381 __spin_lock_init(&rwlocks[i].lock);
382 rwlocks[i].count = 0;
385 /* Write it out (appending to end) */
386 if (write(fd, rwlocks, size) != size) {
390 smp_machine = this_is_smp();
395 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
397 tdb_rwlock_t *rwlocks;
400 if (tdb->header.rwlocks == 0) return 0;
401 if (!tdb->map_ptr) return -1;
403 /* We're mmapped here */
404 rwlocks = (tdb_rwlock_t *)((char *)tdb->map_ptr + tdb->header.rwlocks);
405 for(i = 0; i < tdb->header.hash_size+1; i++) {
406 __spin_lock_init(&rwlocks[i].lock);
407 rwlocks[i].count = 0;
412 int tdb_create_rwlocks(int fd, unsigned int hash_size) { return 0; }
413 int tdb_spinlock(TDB_CONTEXT *tdb, int list, int rw_type) { return -1; }
414 int tdb_spinunlock(TDB_CONTEXT *tdb, int list, int rw_type) { return -1; }
416 /* Non-spinlock version: remove spinlock pointer */
417 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
419 tdb_off off = (tdb_off)((char *)&tdb->header.rwlocks
420 - (char *)&tdb->header);
422 tdb->header.rwlocks = 0;
423 if (lseek(tdb->fd, off, SEEK_SET) != off
424 || write(tdb->fd, (void *)&tdb->header.rwlocks,
425 sizeof(tdb->header.rwlocks))
426 != sizeof(tdb->header.rwlocks))