preparing for release of 2.2.3a
[Samba.git] / source / tdb / spinlock.c
blobb00d115dde7538875ae6617f34f58c40a6be1979
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 Samba database functions
5 Copyright (C) Anton Blanchard 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #if STANDALONE
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <time.h>
34 #include "tdb.h"
35 #include "spinlock.h"
37 #define DEBUG
38 #else
39 #include "includes.h"
40 #endif
42 #ifdef USE_SPINLOCKS
45 * ARCH SPECIFIC
48 #if defined(SPARC_SPINLOCKS)
50 static inline int __spin_trylock(spinlock_t *lock)
52 unsigned int result;
54 asm volatile("ldstub [%1], %0"
55 : "=r" (result)
56 : "r" (lock)
57 : "memory");
59 return (result == 0) ? 0 : EBUSY;
62 static inline void __spin_unlock(spinlock_t *lock)
64 asm volatile("":::"memory");
65 *lock = 0;
68 static inline void __spin_lock_init(spinlock_t *lock)
70 *lock = 0;
73 static inline int __spin_is_locked(spinlock_t *lock)
75 return (*lock != 0);
78 #elif defined(POWERPC_SPINLOCKS)
80 static inline int __spin_trylock(spinlock_t *lock)
82 unsigned int result;
84 __asm__ __volatile__(
85 "1: lwarx %0,0,%1\n\
86 cmpwi 0,%0,0\n\
87 li %0,0\n\
88 bne- 2f\n\
89 li %0,1\n\
90 stwcx. %0,0,%1\n\
91 bne- 1b\n\
92 isync\n\
93 2:" : "=&r"(result)
94 : "r"(lock)
95 : "cr0", "memory");
97 return (result == 1) ? 0 : EBUSY;
100 static inline void __spin_unlock(spinlock_t *lock)
102 asm volatile("eieio":::"memory");
103 *lock = 0;
106 static inline void __spin_lock_init(spinlock_t *lock)
108 *lock = 0;
111 static inline int __spin_is_locked(spinlock_t *lock)
113 return (*lock != 0);
116 #elif defined(INTEL_SPINLOCKS)
118 static inline int __spin_trylock(spinlock_t *lock)
120 int oldval;
122 asm volatile("xchgl %0,%1"
123 : "=r" (oldval), "=m" (*lock)
124 : "0" (0)
125 : "memory");
127 return oldval > 0 ? 0 : EBUSY;
130 static inline void __spin_unlock(spinlock_t *lock)
132 asm volatile("":::"memory");
133 *lock = 1;
136 static inline void __spin_lock_init(spinlock_t *lock)
138 *lock = 1;
141 static inline int __spin_is_locked(spinlock_t *lock)
143 return (*lock != 1);
146 #elif defined(MIPS_SPINLOCKS)
148 static inline unsigned int load_linked(unsigned long addr)
150 unsigned int res;
152 __asm__ __volatile__("ll\t%0,(%1)"
153 : "=r" (res)
154 : "r" (addr));
156 return res;
159 static inline unsigned int store_conditional(unsigned long addr, unsigned int value)
161 unsigned int res;
163 __asm__ __volatile__("sc\t%0,(%2)"
164 : "=r" (res)
165 : "0" (value), "r" (addr));
166 return res;
169 static inline int __spin_trylock(spinlock_t *lock)
171 unsigned int mw;
173 do {
174 mw = load_linked(lock);
175 if (mw)
176 return EBUSY;
177 } while (!store_conditional(lock, 1));
179 asm volatile("":::"memory");
181 return 0;
184 static inline void __spin_unlock(spinlock_t *lock)
186 asm volatile("":::"memory");
187 *lock = 0;
190 static inline void __spin_lock_init(spinlock_t *lock)
192 *lock = 0;
195 static inline int __spin_is_locked(spinlock_t *lock)
197 return (*lock != 0);
200 #else
201 #error Need to implement spinlock code in spinlock.c
202 #endif
205 * OS SPECIFIC
208 static void yield_cpu(void)
210 struct timespec tm;
212 #ifdef USE_SCHED_YIELD
213 sched_yield();
214 #else
215 /* Linux will busy loop for delays < 2ms on real time tasks */
216 tm.tv_sec = 0;
217 tm.tv_nsec = 2000000L + 1;
218 nanosleep(&tm, NULL);
219 #endif
222 static int this_is_smp(void)
224 return 0;
228 * GENERIC
231 static int smp_machine = 0;
233 static inline void __spin_lock(spinlock_t *lock)
235 int ntries = 0;
237 while(__spin_trylock(lock)) {
238 while(__spin_is_locked(lock)) {
239 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
240 continue;
241 yield_cpu();
246 static void __read_lock(tdb_rwlock_t *rwlock)
248 int ntries = 0;
250 while(1) {
251 __spin_lock(&rwlock->lock);
253 if (!(rwlock->count & RWLOCK_BIAS)) {
254 rwlock->count++;
255 __spin_unlock(&rwlock->lock);
256 return;
259 __spin_unlock(&rwlock->lock);
261 while(rwlock->count & RWLOCK_BIAS) {
262 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
263 continue;
264 yield_cpu();
269 static void __write_lock(tdb_rwlock_t *rwlock)
271 int ntries = 0;
273 while(1) {
274 __spin_lock(&rwlock->lock);
276 if (rwlock->count == 0) {
277 rwlock->count |= RWLOCK_BIAS;
278 __spin_unlock(&rwlock->lock);
279 return;
282 __spin_unlock(&rwlock->lock);
284 while(rwlock->count != 0) {
285 if (smp_machine && ntries++ < MAX_BUSY_LOOPS)
286 continue;
287 yield_cpu();
292 static void __write_unlock(tdb_rwlock_t *rwlock)
294 __spin_lock(&rwlock->lock);
296 #ifdef DEBUG
297 if (!(rwlock->count & RWLOCK_BIAS))
298 fprintf(stderr, "bug: write_unlock\n");
299 #endif
301 rwlock->count &= ~RWLOCK_BIAS;
302 __spin_unlock(&rwlock->lock);
305 static void __read_unlock(tdb_rwlock_t *rwlock)
307 __spin_lock(&rwlock->lock);
309 #ifdef DEBUG
310 if (!rwlock->count)
311 fprintf(stderr, "bug: read_unlock\n");
313 if (rwlock->count & RWLOCK_BIAS)
314 fprintf(stderr, "bug: read_unlock\n");
315 #endif
317 rwlock->count--;
318 __spin_unlock(&rwlock->lock);
321 /* TDB SPECIFIC */
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);
331 switch(rw_type) {
332 case F_RDLCK:
333 __read_lock(&rwlocks[list+1]);
334 break;
336 case F_WRLCK:
337 __write_lock(&rwlocks[list+1]);
338 break;
340 default:
341 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
343 return 0;
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);
354 switch(rw_type) {
355 case F_RDLCK:
356 __read_unlock(&rwlocks[list+1]);
357 break;
359 case F_WRLCK:
360 __write_unlock(&rwlocks[list+1]);
361 break;
363 default:
364 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
367 return 0;
370 int tdb_create_rwlocks(int fd, unsigned int hash_size)
372 unsigned size, i;
373 tdb_rwlock_t *rwlocks;
375 size = (hash_size + 1) * sizeof(tdb_rwlock_t);
376 rwlocks = malloc(size);
377 if (!rwlocks)
378 return -1;
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) {
387 SAFE_FREE(rwlocks);
388 return -1;
390 smp_machine = this_is_smp();
391 SAFE_FREE(rwlocks);
392 return 0;
395 int tdb_clear_spinlocks(TDB_CONTEXT *tdb)
397 tdb_rwlock_t *rwlocks;
398 unsigned i;
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;
409 return 0;
411 #else
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))
427 return -1;
428 return 0;
430 #endif