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>
42 * Optional debugging (magic stamping and checking ownership) can be added.
54 * - spin_lock_irqsave(x, f)
55 * - spin_unlock_irqrestore(x, f)
57 * No-op implementation.
60 void spin_lock_init(spinlock_t *lock)
62 LASSERT(lock != NULL);
66 void spin_lock(spinlock_t *lock)
71 void spin_unlock(spinlock_t *lock)
76 int spin_trylock(spinlock_t *lock)
82 void spin_lock_bh_init(spinlock_t *lock)
84 LASSERT(lock != NULL);
88 void spin_lock_bh(spinlock_t *lock)
90 LASSERT(lock != NULL);
94 void spin_unlock_bh(spinlock_t *lock)
96 LASSERT(lock != NULL);
109 void sema_init(struct semaphore *s, int val)
116 void __down(struct semaphore *s)
122 void __up(struct semaphore *s)
132 * - init_mutex_locked(x)
137 #define mutex_up(s) __up(s)
138 #define mutex_down(s) __down(s)
140 #define init_mutex(x) sema_init(x, 1)
141 #define init_mutex_locked(x) sema_init(x, 0)
146 * - init_completion(c)
148 * - wait_for_completion(c)
150 struct completion {};
152 void init_completion(struct completion *c)
158 void complete(struct completion *c)
164 void wait_for_completion(struct completion *c)
180 struct rw_semaphore {};
182 void init_rwsem(struct rw_semaphore *s)
188 void down_read(struct rw_semaphore *s)
194 int down_read_trylock(struct rw_semaphore *s)
201 void down_write(struct rw_semaphore *s)
207 int down_write_trylock(struct rw_semaphore *s)
214 void up_read(struct rw_semaphore *s)
220 void up_write(struct rw_semaphore *s)
227 #ifdef HAVE_LIBPTHREAD
233 void cfs_init_completion(struct cfs_completion *c)
237 pthread_mutex_init(&c->c_mut, NULL);
238 pthread_cond_init(&c->c_cond, NULL);
241 void cfs_fini_completion(struct cfs_completion *c)
244 pthread_mutex_destroy(&c->c_mut);
245 pthread_cond_destroy(&c->c_cond);
248 void cfs_complete(struct cfs_completion *c)
251 pthread_mutex_lock(&c->c_mut);
253 pthread_cond_signal(&c->c_cond);
254 pthread_mutex_unlock(&c->c_mut);
257 void cfs_wait_for_completion(struct cfs_completion *c)
260 pthread_mutex_lock(&c->c_mut);
261 while (c->c_done == 0)
262 pthread_cond_wait(&c->c_cond, &c->c_mut);
264 pthread_mutex_unlock(&c->c_mut);
271 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
273 int cfs_atomic_read(cfs_atomic_t *a)
277 pthread_mutex_lock(&atomic_guard_lock);
279 pthread_mutex_unlock(&atomic_guard_lock);
283 void cfs_atomic_set(cfs_atomic_t *a, int b)
285 pthread_mutex_lock(&atomic_guard_lock);
287 pthread_mutex_unlock(&atomic_guard_lock);
290 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
294 pthread_mutex_lock(&atomic_guard_lock);
296 pthread_mutex_unlock(&atomic_guard_lock);
300 void cfs_atomic_inc(cfs_atomic_t *a)
302 pthread_mutex_lock(&atomic_guard_lock);
304 pthread_mutex_unlock(&atomic_guard_lock);
307 void cfs_atomic_dec(cfs_atomic_t *a)
309 pthread_mutex_lock(&atomic_guard_lock);
311 pthread_mutex_unlock(&atomic_guard_lock);
313 void cfs_atomic_add(int b, cfs_atomic_t *a)
316 pthread_mutex_lock(&atomic_guard_lock);
318 pthread_mutex_unlock(&atomic_guard_lock);
321 void cfs_atomic_sub(int b, cfs_atomic_t *a)
323 pthread_mutex_lock(&atomic_guard_lock);
325 pthread_mutex_unlock(&atomic_guard_lock);
328 #endif /* HAVE_LIBPTHREAD */
336 * c-indentation-style: "K&R"