1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/libcfs/user-lock.c
38 * Implementations of portable synchronization APIs for liblustre
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
46 * XXX Liang: There are several branches share lnet with b_hd_newconfig,
47 * if we define lock APIs at here, there will be conflict with liblustre
54 #include <libcfs/libcfs.h>
55 #include <libcfs/kp30.h>
58 * Optional debugging (magic stamping and checking ownership) can be added.
70 * - spin_lock_irqsave(x, f)
71 * - spin_unlock_irqrestore(x, f)
73 * No-op implementation.
76 void spin_lock_init(spinlock_t *lock)
78 LASSERT(lock != NULL);
82 void spin_lock(spinlock_t *lock)
87 void spin_unlock(spinlock_t *lock)
92 int spin_trylock(spinlock_t *lock)
98 void spin_lock_bh_init(spinlock_t *lock)
100 LASSERT(lock != NULL);
104 void spin_lock_bh(spinlock_t *lock)
106 LASSERT(lock != NULL);
110 void spin_unlock_bh(spinlock_t *lock)
112 LASSERT(lock != NULL);
125 void sema_init(struct semaphore *s, int val)
132 void __down(struct semaphore *s)
138 void __up(struct semaphore *s)
148 * - init_mutex_locked(x)
153 #define mutex_up(s) __up(s)
154 #define mutex_down(s) __down(s)
156 #define init_mutex(x) sema_init(x, 1)
157 #define init_mutex_locked(x) sema_init(x, 0)
162 * - init_completion(c)
164 * - wait_for_completion(c)
166 struct completion {};
168 void init_completion(struct completion *c)
174 void complete(struct completion *c)
180 void wait_for_completion(struct completion *c)
196 struct rw_semaphore {};
198 void init_rwsem(struct rw_semaphore *s)
204 void down_read(struct rw_semaphore *s)
210 int down_read_trylock(struct rw_semaphore *s)
217 void down_write(struct rw_semaphore *s)
223 int down_write_trylock(struct rw_semaphore *s)
230 void up_read(struct rw_semaphore *s)
236 void up_write(struct rw_semaphore *s)
243 #ifdef HAVE_LIBPTHREAD
249 void cfs_init_completion(struct cfs_completion *c)
253 pthread_mutex_init(&c->c_mut, NULL);
254 pthread_cond_init(&c->c_cond, NULL);
257 void cfs_fini_completion(struct cfs_completion *c)
260 pthread_mutex_destroy(&c->c_mut);
261 pthread_cond_destroy(&c->c_cond);
264 void cfs_complete(struct cfs_completion *c)
267 pthread_mutex_lock(&c->c_mut);
269 pthread_cond_signal(&c->c_cond);
270 pthread_mutex_unlock(&c->c_mut);
273 void cfs_wait_for_completion(struct cfs_completion *c)
276 pthread_mutex_lock(&c->c_mut);
277 while (c->c_done == 0)
278 pthread_cond_wait(&c->c_cond, &c->c_mut);
280 pthread_mutex_unlock(&c->c_mut);
287 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
289 int cfs_atomic_read(cfs_atomic_t *a)
293 pthread_mutex_lock(&atomic_guard_lock);
295 pthread_mutex_unlock(&atomic_guard_lock);
299 void cfs_atomic_set(cfs_atomic_t *a, int b)
301 pthread_mutex_lock(&atomic_guard_lock);
303 pthread_mutex_unlock(&atomic_guard_lock);
306 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
310 pthread_mutex_lock(&atomic_guard_lock);
312 pthread_mutex_unlock(&atomic_guard_lock);
316 void cfs_atomic_inc(cfs_atomic_t *a)
318 pthread_mutex_lock(&atomic_guard_lock);
320 pthread_mutex_unlock(&atomic_guard_lock);
323 void cfs_atomic_dec(cfs_atomic_t *a)
325 pthread_mutex_lock(&atomic_guard_lock);
327 pthread_mutex_unlock(&atomic_guard_lock);
329 void cfs_atomic_add(int b, cfs_atomic_t *a)
332 pthread_mutex_lock(&atomic_guard_lock);
334 pthread_mutex_unlock(&atomic_guard_lock);
337 void cfs_atomic_sub(int b, cfs_atomic_t *a)
339 pthread_mutex_lock(&atomic_guard_lock);
341 pthread_mutex_unlock(&atomic_guard_lock);
344 #endif /* HAVE_LIBPTHREAD */
352 * c-indentation-style: "K&R"