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 * 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
53 #include <libcfs/libcfs.h>
56 * Optional debugging (magic stamping and checking ownership) can be added.
64 * - spin_lock_nested(x, subclass)
68 * - spin_lock_irqsave(x, f)
69 * - spin_unlock_irqrestore(x, f)
71 * No-op implementation.
74 void spin_lock_init(spinlock_t *lock)
76 LASSERT(lock != NULL);
80 void spin_lock(spinlock_t *lock)
85 void spin_unlock(spinlock_t *lock)
90 int spin_trylock(spinlock_t *lock)
96 void spin_lock_bh_init(spinlock_t *lock)
98 LASSERT(lock != NULL);
102 void spin_lock_bh(spinlock_t *lock)
104 LASSERT(lock != NULL);
108 void spin_unlock_bh(spinlock_t *lock)
110 LASSERT(lock != NULL);
122 void sema_init(struct semaphore *s, int val)
129 void __down(struct semaphore *s)
135 void __up(struct semaphore *s)
145 * - init_completion(c)
147 * - wait_for_completion(c)
150 static cfs_wait_handler_t wait_handler;
152 void init_completion_module(cfs_wait_handler_t handler)
154 wait_handler = handler;
157 void init_completion(struct completion *c)
161 cfs_waitq_init(&c->wait);
164 void complete(struct completion *c)
168 cfs_waitq_signal(&c->wait);
171 void wait_for_completion(struct completion *c)
179 } while (c->done == 0);
182 int wait_for_completion_interruptible(struct completion *c)
190 } while (c->done == 0);
205 void init_rwsem(struct rw_semaphore *s)
211 void down_read(struct rw_semaphore *s)
217 int down_read_trylock(struct rw_semaphore *s)
224 void down_write(struct rw_semaphore *s)
230 int down_write_trylock(struct rw_semaphore *s)
237 void up_read(struct rw_semaphore *s)
243 void up_write(struct rw_semaphore *s)
249 void fini_rwsem(struct rw_semaphore *s)
255 #ifdef HAVE_LIBPTHREAD
261 void cfs_init_completion(struct cfs_completion *c)
265 pthread_mutex_init(&c->c_mut, NULL);
266 pthread_cond_init(&c->c_cond, NULL);
269 void cfs_fini_completion(struct cfs_completion *c)
272 pthread_mutex_destroy(&c->c_mut);
273 pthread_cond_destroy(&c->c_cond);
276 void cfs_complete(struct cfs_completion *c)
279 pthread_mutex_lock(&c->c_mut);
281 pthread_cond_signal(&c->c_cond);
282 pthread_mutex_unlock(&c->c_mut);
285 void cfs_wait_for_completion(struct cfs_completion *c)
288 pthread_mutex_lock(&c->c_mut);
289 while (c->c_done == 0)
290 pthread_cond_wait(&c->c_cond, &c->c_mut);
292 pthread_mutex_unlock(&c->c_mut);
299 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
301 int cfs_atomic_read(cfs_atomic_t *a)
305 pthread_mutex_lock(&atomic_guard_lock);
307 pthread_mutex_unlock(&atomic_guard_lock);
311 void cfs_atomic_set(cfs_atomic_t *a, int b)
313 pthread_mutex_lock(&atomic_guard_lock);
315 pthread_mutex_unlock(&atomic_guard_lock);
318 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
322 pthread_mutex_lock(&atomic_guard_lock);
324 pthread_mutex_unlock(&atomic_guard_lock);
328 void cfs_atomic_inc(cfs_atomic_t *a)
330 pthread_mutex_lock(&atomic_guard_lock);
332 pthread_mutex_unlock(&atomic_guard_lock);
335 void cfs_atomic_dec(cfs_atomic_t *a)
337 pthread_mutex_lock(&atomic_guard_lock);
339 pthread_mutex_unlock(&atomic_guard_lock);
341 void cfs_atomic_add(int b, cfs_atomic_t *a)
344 pthread_mutex_lock(&atomic_guard_lock);
346 pthread_mutex_unlock(&atomic_guard_lock);
349 void cfs_atomic_sub(int b, cfs_atomic_t *a)
351 pthread_mutex_lock(&atomic_guard_lock);
353 pthread_mutex_unlock(&atomic_guard_lock);
356 #endif /* HAVE_LIBPTHREAD */
364 * c-indentation-style: "K&R"