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 [sun.com URL with a
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 * libcfs/libcfs/user-lock.c
38 * Author: Nikita Danilov <nikita@clusterfs.com>
41 /* Implementations of portable synchronization APIs for liblustre */
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>
57 * Optional debugging (magic stamping and checking ownership) can be added.
69 * - spin_lock_irqsave(x, f)
70 * - spin_unlock_irqrestore(x, f)
72 * No-op implementation.
75 void spin_lock_init(spinlock_t *lock)
77 LASSERT(lock != NULL);
81 void spin_lock(spinlock_t *lock)
86 void spin_unlock(spinlock_t *lock)
91 int spin_trylock(spinlock_t *lock)
97 void spin_lock_bh_init(spinlock_t *lock)
99 LASSERT(lock != NULL);
103 void spin_lock_bh(spinlock_t *lock)
105 LASSERT(lock != NULL);
109 void spin_unlock_bh(spinlock_t *lock)
111 LASSERT(lock != NULL);
124 void sema_init(struct semaphore *s, int val)
131 void __down(struct semaphore *s)
137 void __up(struct semaphore *s)
147 * - init_mutex_locked(x)
152 #define mutex_up(s) __up(s)
153 #define mutex_down(s) __down(s)
155 #define init_mutex(x) sema_init(x, 1)
156 #define init_mutex_locked(x) sema_init(x, 0)
161 * - init_completion(c)
163 * - wait_for_completion(c)
165 struct completion {};
167 void init_completion(struct completion *c)
173 void complete(struct completion *c)
179 void wait_for_completion(struct completion *c)
195 struct rw_semaphore {};
197 void init_rwsem(struct rw_semaphore *s)
203 void down_read(struct rw_semaphore *s)
209 int down_read_trylock(struct rw_semaphore *s)
216 void down_write(struct rw_semaphore *s)
222 int down_write_trylock(struct rw_semaphore *s)
229 void up_read(struct rw_semaphore *s)
235 void up_write(struct rw_semaphore *s)
242 #ifdef HAVE_LIBPTHREAD
248 void cfs_init_completion(struct cfs_completion *c)
252 pthread_mutex_init(&c->c_mut, NULL);
253 pthread_cond_init(&c->c_cond, NULL);
256 void cfs_fini_completion(struct cfs_completion *c)
259 pthread_mutex_destroy(&c->c_mut);
260 pthread_cond_destroy(&c->c_cond);
263 void cfs_complete(struct cfs_completion *c)
266 pthread_mutex_lock(&c->c_mut);
268 pthread_cond_signal(&c->c_cond);
269 pthread_mutex_unlock(&c->c_mut);
272 void cfs_wait_for_completion(struct cfs_completion *c)
275 pthread_mutex_lock(&c->c_mut);
276 while (c->c_done == 0)
277 pthread_cond_wait(&c->c_cond, &c->c_mut);
279 pthread_mutex_unlock(&c->c_mut);
286 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
288 int cfs_atomic_read(cfs_atomic_t *a)
292 pthread_mutex_lock(&atomic_guard_lock);
294 pthread_mutex_unlock(&atomic_guard_lock);
298 void cfs_atomic_set(cfs_atomic_t *a, int b)
300 pthread_mutex_lock(&atomic_guard_lock);
302 pthread_mutex_unlock(&atomic_guard_lock);
305 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
309 pthread_mutex_lock(&atomic_guard_lock);
311 pthread_mutex_unlock(&atomic_guard_lock);
315 void cfs_atomic_inc(cfs_atomic_t *a)
317 pthread_mutex_lock(&atomic_guard_lock);
319 pthread_mutex_unlock(&atomic_guard_lock);
322 void cfs_atomic_dec(cfs_atomic_t *a)
324 pthread_mutex_lock(&atomic_guard_lock);
326 pthread_mutex_unlock(&atomic_guard_lock);
328 void cfs_atomic_add(int b, cfs_atomic_t *a)
331 pthread_mutex_lock(&atomic_guard_lock);
333 pthread_mutex_unlock(&atomic_guard_lock);
336 void cfs_atomic_sub(int b, cfs_atomic_t *a)
338 pthread_mutex_lock(&atomic_guard_lock);
340 pthread_mutex_unlock(&atomic_guard_lock);
343 #endif /* HAVE_LIBPTHREAD */
351 * c-indentation-style: "K&R"