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/include/libcfs/user-lock.h
38 * Author: Nikita Danilov <nikita@clusterfs.com>
41 #ifndef __LIBCFS_USER_LOCK_H__
42 #define __LIBCFS_USER_LOCK_H__
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
48 /* Implementations of portable synchronization APIs for liblustre */
51 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
53 * XXX Liang: There are several branches share lnet with b_hd_newconfig,
54 * if we define lock APIs at here, there will be conflict with liblustre
64 * Optional debugging (magic stamping and checking ownership) can be added.
75 * - spin_lock_irqsave(x, f)
76 * - spin_unlock_irqrestore(x, f)
78 * No-op implementation.
80 struct spin_lock {int foo;};
82 typedef struct spin_lock spinlock_t;
84 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
85 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
87 void spin_lock_init(spinlock_t *lock);
88 void spin_lock(spinlock_t *lock);
89 void spin_unlock(spinlock_t *lock);
90 int spin_trylock(spinlock_t *lock);
91 void spin_lock_bh_init(spinlock_t *lock);
92 void spin_lock_bh(spinlock_t *lock);
93 void spin_unlock_bh(spinlock_t *lock);
94 static inline int spin_is_locked(spinlock_t *l) {return 1;}
96 static inline void spin_lock_irqsave(spinlock_t *l, unsigned long f){}
97 static inline void spin_unlock_irqrestore(spinlock_t *l, unsigned long f){}
106 typedef struct semaphore {
110 void sema_init(struct semaphore *s, int val);
111 void __down(struct semaphore *s);
112 void __up(struct semaphore *s);
118 * - init_mutex_locked(x)
122 #define mutex_up(s) __up(s)
123 #define mutex_down(s) __down(s)
125 #define init_mutex(x) sema_init(x, 1)
126 #define init_mutex_locked(x) sema_init(x, 0)
131 * - init_completion(c)
133 * - wait_for_completion(c)
136 struct completion {};
138 void init_completion(struct completion *c);
139 void complete(struct completion *c);
140 void wait_for_completion(struct completion *c);
152 struct rw_semaphore {};
154 void init_rwsem(struct rw_semaphore *s);
155 void down_read(struct rw_semaphore *s);
156 int down_read_trylock(struct rw_semaphore *s);
157 void down_write(struct rw_semaphore *s);
158 int down_write_trylock(struct rw_semaphore *s);
159 void up_read(struct rw_semaphore *s);
160 void up_write(struct rw_semaphore *s);
163 * read-write lock : Need to be investigated more!!
164 * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
166 * - DECLARE_RWLOCK(l)
173 typedef struct rw_semaphore rwlock_t;
175 #define rwlock_init(pl) init_rwsem(pl)
177 #define read_lock(l) down_read(l)
178 #define read_unlock(l) up_read(l)
179 #define write_lock(l) down_write(l)
180 #define write_unlock(l) up_write(l)
183 write_lock_irqsave(rwlock_t *l, unsigned long f) { write_lock(l); }
185 write_unlock_irqrestore(rwlock_t *l, unsigned long f) { write_unlock(l); }
188 read_lock_irqsave(rwlock_t *l, unsigned long f) { read_lock(l); }
190 read_unlock_irqrestore(rwlock_t *l, unsigned long f) { read_unlock(l); }
193 * Atomic for user-space
194 * Copied from liblustre
196 typedef struct { volatile int counter; } atomic_t;
198 #define ATOMIC_INIT(i) { (i) }
199 #define atomic_read(a) ((a)->counter)
200 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
201 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
202 #define atomic_inc(a) (((a)->counter)++)
203 #define atomic_dec(a) do { (a)->counter--; } while (0)
204 #define atomic_add(b,a) do {(a)->counter += b;} while (0)
205 #define atomic_add_return(n,a) ((a)->counter = n)
206 #define atomic_inc_return(a) atomic_add_return(1,a)
207 #define atomic_sub(b,a) do {(a)->counter -= b;} while (0)
211 #ifdef HAVE_LIBPTHREAD
218 struct cfs_completion {
220 pthread_cond_t c_cond;
221 pthread_mutex_t c_mut;
224 void cfs_init_completion(struct cfs_completion *c);
225 void cfs_fini_completion(struct cfs_completion *c);
226 void cfs_complete(struct cfs_completion *c);
227 void cfs_wait_for_completion(struct cfs_completion *c);
233 typedef struct { volatile int counter; } cfs_atomic_t;
235 int cfs_atomic_read(cfs_atomic_t *a);
236 void cfs_atomic_set(cfs_atomic_t *a, int b);
237 int cfs_atomic_dec_and_test(cfs_atomic_t *a);
238 void cfs_atomic_inc(cfs_atomic_t *a);
239 void cfs_atomic_dec(cfs_atomic_t *a);
240 void cfs_atomic_add(int b, cfs_atomic_t *a);
241 void cfs_atomic_sub(int b, cfs_atomic_t *a);
243 #endif /* HAVE_LIBPTHREAD */
248 /* __LIBCFS_USER_LOCK_H__ */
252 * c-indentation-style: "K&R"