1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Nikita Danilov <nikita@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or modify it under the
10 * terms of version 2 of the GNU General Public License as published by the
11 * Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License along
19 * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
20 * Ave, Cambridge, MA 02139, USA.
22 * Implementation of portable time API for user-level.
26 /* Implementations of portable synchronization APIs for liblustre */
29 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
31 * XXX Liang: There are several branches share lnet with b_hd_newconfig,
32 * if we define lock APIs at here, there will be conflict with liblustre
39 #include <libcfs/libcfs.h>
40 #include <libcfs/kp30.h>
43 * Optional debugging (magic stamping and checking ownership) can be added.
55 * - spin_lock_irqsave(x, f)
56 * - spin_unlock_irqrestore(x, f)
58 * No-op implementation.
61 void spin_lock_init(spinlock_t *lock)
63 LASSERT(lock != NULL);
67 void spin_lock(spinlock_t *lock)
72 void spin_unlock(spinlock_t *lock)
77 int spin_trylock(spinlock_t *lock)
83 void spin_lock_bh_init(spinlock_t *lock)
85 LASSERT(lock != NULL);
89 void spin_lock_bh(spinlock_t *lock)
91 LASSERT(lock != NULL);
95 void spin_unlock_bh(spinlock_t *lock)
97 LASSERT(lock != NULL);
110 void sema_init(struct semaphore *s, int val)
117 void __down(struct semaphore *s)
123 void __up(struct semaphore *s)
133 * - init_mutex_locked(x)
138 #define mutex_up(s) __up(s)
139 #define mutex_down(s) __down(s)
141 #define init_mutex(x) sema_init(x, 1)
142 #define init_mutex_locked(x) sema_init(x, 0)
147 * - init_completion(c)
149 * - wait_for_completion(c)
151 struct completion {};
153 void init_completion(struct completion *c)
159 void complete(struct completion *c)
165 void wait_for_completion(struct completion *c)
181 struct rw_semaphore {};
183 void init_rwsem(struct rw_semaphore *s)
189 void down_read(struct rw_semaphore *s)
195 int down_read_trylock(struct rw_semaphore *s)
202 void down_write(struct rw_semaphore *s)
208 int down_write_trylock(struct rw_semaphore *s)
215 void up_read(struct rw_semaphore *s)
221 void up_write(struct rw_semaphore *s)
228 #ifdef HAVE_LIBPTHREAD
234 void cfs_init_completion(struct cfs_completion *c)
238 pthread_mutex_init(&c->c_mut, NULL);
239 pthread_cond_init(&c->c_cond, NULL);
242 void cfs_fini_completion(struct cfs_completion *c)
245 pthread_mutex_destroy(&c->c_mut);
246 pthread_cond_destroy(&c->c_cond);
249 void cfs_complete(struct cfs_completion *c)
252 pthread_mutex_lock(&c->c_mut);
254 pthread_cond_signal(&c->c_cond);
255 pthread_mutex_unlock(&c->c_mut);
258 void cfs_wait_for_completion(struct cfs_completion *c)
261 pthread_mutex_lock(&c->c_mut);
262 while (c->c_done == 0)
263 pthread_cond_wait(&c->c_cond, &c->c_mut);
265 pthread_mutex_unlock(&c->c_mut);
272 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
274 int cfs_atomic_read(cfs_atomic_t *a)
278 pthread_mutex_lock(&atomic_guard_lock);
280 pthread_mutex_unlock(&atomic_guard_lock);
284 void cfs_atomic_set(cfs_atomic_t *a, int b)
286 pthread_mutex_lock(&atomic_guard_lock);
288 pthread_mutex_unlock(&atomic_guard_lock);
291 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
295 pthread_mutex_lock(&atomic_guard_lock);
297 pthread_mutex_unlock(&atomic_guard_lock);
301 void cfs_atomic_inc(cfs_atomic_t *a)
303 pthread_mutex_lock(&atomic_guard_lock);
305 pthread_mutex_unlock(&atomic_guard_lock);
308 void cfs_atomic_dec(cfs_atomic_t *a)
310 pthread_mutex_lock(&atomic_guard_lock);
312 pthread_mutex_unlock(&atomic_guard_lock);
314 void cfs_atomic_add(int b, cfs_atomic_t *a)
317 pthread_mutex_lock(&atomic_guard_lock);
319 pthread_mutex_unlock(&atomic_guard_lock);
322 void cfs_atomic_sub(int b, cfs_atomic_t *a)
324 pthread_mutex_lock(&atomic_guard_lock);
326 pthread_mutex_unlock(&atomic_guard_lock);
329 #endif /* HAVE_LIBPTHREAD */
337 * c-indentation-style: "K&R"