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.
47 #if defined(SPARC_SPINLOCKS)
49 static inline int __spin_trylock(spinlock_t
*lock
)
53 asm volatile("ldstub [%1], %0"
58 return (result
== 0) ? 0 : EBUSY
;
61 static inline void __spin_unlock(spinlock_t
*lock
)
63 asm volatile("":::"memory");
67 static inline void __spin_lock_init(spinlock_t
*lock
)
72 static inline int __spin_is_locked(spinlock_t
*lock
)
77 #elif defined(POWERPC_SPINLOCKS)
79 static inline int __spin_trylock(spinlock_t
*lock
)
96 return (result
== 1) ? 0 : EBUSY
;
99 static inline void __spin_unlock(spinlock_t
*lock
)
101 asm volatile("eieio":::"memory");
105 static inline void __spin_lock_init(spinlock_t
*lock
)
110 static inline int __spin_is_locked(spinlock_t
*lock
)
115 #elif defined(INTEL_SPINLOCKS)
117 static inline int __spin_trylock(spinlock_t
*lock
)
121 asm volatile("xchgl %0,%1"
122 : "=r" (oldval
), "=m" (*lock
)
126 return oldval
> 0 ? 0 : EBUSY
;
129 static inline void __spin_unlock(spinlock_t
*lock
)
131 asm volatile("":::"memory");
135 static inline void __spin_lock_init(spinlock_t
*lock
)
140 static inline int __spin_is_locked(spinlock_t
*lock
)
145 #elif defined(MIPS_SPINLOCKS)
147 static inline unsigned int load_linked(unsigned long addr
)
151 __asm__
__volatile__("ll\t%0,(%1)"
158 static inline unsigned int store_conditional(unsigned long addr
, unsigned int value
)
162 __asm__
__volatile__("sc\t%0,(%2)"
164 : "0" (value
), "r" (addr
));
168 static inline int __spin_trylock(spinlock_t
*lock
)
173 mw
= load_linked(lock
);
176 } while (!store_conditional(lock
, 1));
178 asm volatile("":::"memory");
183 static inline void __spin_unlock(spinlock_t
*lock
)
185 asm volatile("":::"memory");
189 static inline void __spin_lock_init(spinlock_t
*lock
)
194 static inline int __spin_is_locked(spinlock_t
*lock
)
200 #error Need to implement spinlock code in spinlock.c
207 static void yield_cpu(void)
211 #ifdef USE_SCHED_YIELD
214 /* Linux will busy loop for delays < 2ms on real time tasks */
216 tm
.tv_nsec
= 2000000L + 1;
217 nanosleep(&tm
, NULL
);
221 static int this_is_smp(void)
230 static int smp_machine
= 0;
232 static inline void __spin_lock(spinlock_t
*lock
)
236 while(__spin_trylock(lock
)) {
237 while(__spin_is_locked(lock
)) {
238 if (smp_machine
&& ntries
++ < MAX_BUSY_LOOPS
)
245 static void __read_lock(tdb_rwlock_t
*rwlock
)
250 __spin_lock(&rwlock
->lock
);
252 if (!(rwlock
->count
& RWLOCK_BIAS
)) {
254 __spin_unlock(&rwlock
->lock
);
258 __spin_unlock(&rwlock
->lock
);
260 while(rwlock
->count
& RWLOCK_BIAS
) {
261 if (smp_machine
&& ntries
++ < MAX_BUSY_LOOPS
)
268 static void __write_lock(tdb_rwlock_t
*rwlock
)
273 __spin_lock(&rwlock
->lock
);
275 if (rwlock
->count
== 0) {
276 rwlock
->count
|= RWLOCK_BIAS
;
277 __spin_unlock(&rwlock
->lock
);
281 __spin_unlock(&rwlock
->lock
);
283 while(rwlock
->count
!= 0) {
284 if (smp_machine
&& ntries
++ < MAX_BUSY_LOOPS
)
291 static void __write_unlock(tdb_rwlock_t
*rwlock
)
293 __spin_lock(&rwlock
->lock
);
296 if (!(rwlock
->count
& RWLOCK_BIAS
))
297 fprintf(stderr
, "bug: write_unlock\n");
300 rwlock
->count
&= ~RWLOCK_BIAS
;
301 __spin_unlock(&rwlock
->lock
);
304 static void __read_unlock(tdb_rwlock_t
*rwlock
)
306 __spin_lock(&rwlock
->lock
);
310 fprintf(stderr
, "bug: read_unlock\n");
312 if (rwlock
->count
& RWLOCK_BIAS
)
313 fprintf(stderr
, "bug: read_unlock\n");
317 __spin_unlock(&rwlock
->lock
);
322 /* lock a list in the database. list -1 is the alloc list */
323 int tdb_spinlock(TDB_CONTEXT
*tdb
, int list
, int rw_type
)
325 tdb_rwlock_t
*rwlocks
;
327 if (!tdb
->map_ptr
) return -1;
328 rwlocks
= (tdb_rwlock_t
*)((char *)tdb
->map_ptr
+ tdb
->header
.rwlocks
);
332 __read_lock(&rwlocks
[list
+1]);
336 __write_lock(&rwlocks
[list
+1]);
340 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
345 /* unlock the database. */
346 int tdb_spinunlock(TDB_CONTEXT
*tdb
, int list
, int rw_type
)
348 tdb_rwlock_t
*rwlocks
;
350 if (!tdb
->map_ptr
) return -1;
351 rwlocks
= (tdb_rwlock_t
*)((char *)tdb
->map_ptr
+ tdb
->header
.rwlocks
);
355 __read_unlock(&rwlocks
[list
+1]);
359 __write_unlock(&rwlocks
[list
+1]);
363 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
369 int tdb_create_rwlocks(int fd
, unsigned int hash_size
)
372 tdb_rwlock_t
*rwlocks
;
374 size
= (hash_size
+ 1) * sizeof(tdb_rwlock_t
);
375 rwlocks
= malloc(size
);
379 for(i
= 0; i
< hash_size
+1; i
++) {
380 __spin_lock_init(&rwlocks
[i
].lock
);
381 rwlocks
[i
].count
= 0;
384 /* Write it out (appending to end) */
385 if (write(fd
, rwlocks
, size
) != size
) {
389 smp_machine
= this_is_smp();
394 int tdb_clear_spinlocks(TDB_CONTEXT
*tdb
)
396 tdb_rwlock_t
*rwlocks
;
399 if (tdb
->header
.rwlocks
== 0) return 0;
400 if (!tdb
->map_ptr
) return -1;
402 /* We're mmapped here */
403 rwlocks
= (tdb_rwlock_t
*)((char *)tdb
->map_ptr
+ tdb
->header
.rwlocks
);
404 for(i
= 0; i
< tdb
->header
.hash_size
+1; i
++) {
405 __spin_lock_init(&rwlocks
[i
].lock
);
406 rwlocks
[i
].count
= 0;
411 int tdb_create_rwlocks(int fd
, unsigned int hash_size
) { return 0; }
412 int tdb_spinlock(TDB_CONTEXT
*tdb
, int list
, int rw_type
) { return -1; }
413 int tdb_spinunlock(TDB_CONTEXT
*tdb
, int list
, int rw_type
) { return -1; }
415 /* Non-spinlock version: remove spinlock pointer */
416 int tdb_clear_spinlocks(TDB_CONTEXT
*tdb
)
418 tdb_off off
= (tdb_off
)((char *)&tdb
->header
.rwlocks
419 - (char *)&tdb
->header
);
421 tdb
->header
.rwlocks
= 0;
422 if (lseek(tdb
->fd
, off
, SEEK_SET
) != off
423 || write(tdb
->fd
, (void *)&tdb
->header
.rwlocks
,
424 sizeof(tdb
->header
.rwlocks
))
425 != sizeof(tdb
->header
.rwlocks
))