Whamcloud - gitweb
split osc_cl.c into osc_object.c osc_lock.c osc_io.c osc_page.c osc_dev.c
[fs/lustre-release.git] / libcfs / include / libcfs / user-lock.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/user-lock.h
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 #ifndef __LIBCFS_USER_LOCK_H__
42 #define __LIBCFS_USER_LOCK_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 /* Implementations of portable synchronization APIs for liblustre */
49
50 /*
51  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
52  *
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
55  * in other branches.
56  */
57
58 #ifndef __KERNEL__
59
60 /*
61  * The userspace implementations of linux/spinlock.h vary; we just
62  * include our own for all of them
63  */
64 #define __LINUX_SPINLOCK_H
65
66 /*
67  * Optional debugging (magic stamping and checking ownership) can be added.
68  */
69
70 /*
71  * spin_lock
72  *
73  * - spin_lock_init(x)
74  * - spin_lock(x)
75  * - spin_unlock(x)
76  * - spin_trylock(x)
77  *
78  * - spin_lock_irqsave(x, f)
79  * - spin_unlock_irqrestore(x, f)
80  *
81  * No-op implementation.
82  */
83 struct spin_lock {int foo;};
84
85 typedef struct spin_lock spinlock_t;
86
87 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
88 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
89 #define LASSERT_SEM_LOCKED(sem) do {} while(0)
90
91 void spin_lock_init(spinlock_t *lock);
92 void spin_lock(spinlock_t *lock);
93 void spin_unlock(spinlock_t *lock);
94 int spin_trylock(spinlock_t *lock);
95 void spin_lock_bh_init(spinlock_t *lock);
96 void spin_lock_bh(spinlock_t *lock);
97 void spin_unlock_bh(spinlock_t *lock);
98
99 static inline int spin_is_locked(spinlock_t *l) {return 1;}
100 static inline void spin_lock_irqsave(spinlock_t *l, unsigned long f){}
101 static inline void spin_unlock_irqrestore(spinlock_t *l, unsigned long f){}
102
103 /*
104  * Semaphore
105  *
106  * - sema_init(x, v)
107  * - __down(x)
108  * - __up(x)
109  */
110 typedef struct semaphore {
111     int foo;
112 } mutex_t;
113
114 void sema_init(struct semaphore *s, int val);
115 void __down(struct semaphore *s);
116 void __up(struct semaphore *s);
117
118 /*
119  * Mutex:
120  *
121  * - init_mutex(x)
122  * - init_mutex_locked(x)
123  * - mutex_up(x)
124  * - mutex_down(x)
125  */
126 #define DECLARE_MUTEX(name)     \
127         struct semaphore name = { 1 }
128
129 #define mutex_up(s)                     __up(s)
130 #define up(s)                           mutex_up(s)
131 #define mutex_down(s)                   __down(s)
132 #define down(s)                         mutex_down(s)
133
134 #define init_MUTEX(x)                   sema_init(x, 1)
135 #define init_MUTEX_LOCKED(x)            sema_init(x, 0)
136 #define init_mutex(s)                   init_MUTEX(s)
137
138 /*
139  * Completion:
140  *
141  * - init_completion(c)
142  * - complete(c)
143  * - wait_for_completion(c)
144  */
145 struct completion {
146         unsigned int done;
147         cfs_waitq_t wait;
148 };
149 typedef int (cfs_wait_handler) (int timeout);
150 void set_completion_wait_handler(cfs_wait_handler *handler);
151 void init_completion(struct completion *c);
152 void complete(struct completion *c);
153 void wait_for_completion(struct completion *c);
154 int wait_for_completion_interruptible(struct completion *c);
155
156 #define COMPLETION_INITIALIZER(work) \
157         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
158
159 #define DECLARE_COMPLETION(work) \
160         struct completion work = COMPLETION_INITIALIZER(work)
161
162 #define INIT_COMPLETION(x)      ((x).done = 0)
163
164
165 /*
166  * rw_semaphore:
167  *
168  * - init_rwsem(x)
169  * - down_read(x)
170  * - up_read(x)
171  * - down_write(x)
172  * - up_write(x)
173  */
174 struct rw_semaphore {
175         int foo;
176 };
177
178 void init_rwsem(struct rw_semaphore *s);
179 void down_read(struct rw_semaphore *s);
180 int down_read_trylock(struct rw_semaphore *s);
181 void down_write(struct rw_semaphore *s);
182 int down_write_trylock(struct rw_semaphore *s);
183 void up_read(struct rw_semaphore *s);
184 void up_write(struct rw_semaphore *s);
185
186 /*
187  * read-write lock : Need to be investigated more!!
188  * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
189  *
190  * - DECLARE_RWLOCK(l)
191  * - rwlock_init(x)
192  * - read_lock(x)
193  * - read_unlock(x)
194  * - write_lock(x)
195  * - write_unlock(x)
196  */
197 typedef struct rw_semaphore rwlock_t;
198 #define RW_LOCK_UNLOCKED        (rwlock_t) { }
199
200 #define rwlock_init(pl)         init_rwsem(pl)
201
202 #define read_lock(l)            down_read(l)
203 #define read_unlock(l)          up_read(l)
204 #define write_lock(l)           down_write(l)
205 #define write_unlock(l)         up_write(l)
206
207 static inline void
208 write_lock_irqsave(rwlock_t *l, unsigned long f) { write_lock(l); }
209 static inline void
210 write_unlock_irqrestore(rwlock_t *l, unsigned long f) { write_unlock(l); }
211
212 static inline void
213 read_lock_irqsave(rwlock_t *l, unsigned long f) { read_lock(l); }
214 static inline void
215 read_unlock_irqrestore(rwlock_t *l, unsigned long f) { read_unlock(l); }
216
217 /*
218  * Atomic for user-space
219  * Copied from liblustre
220  */
221 typedef struct { volatile int counter; } atomic_t;
222
223 #define ATOMIC_INIT(i) { (i) }
224
225 #define atomic_read(a) ((a)->counter)
226 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
227 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
228 #define atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
229 #define atomic_inc(a)  (((a)->counter)++)
230 #define atomic_dec(a)  do { (a)->counter--; } while (0)
231 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
232 #define atomic_add_return(n,a) ((a)->counter += n)
233 #define atomic_inc_return(a) atomic_add_return(1,a)
234 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
235 #define atomic_sub_return(n,a) ((a)->counter -= n)
236 #define atomic_dec_return(a)  atomic_sub_return(1,a)
237
238
239 #ifdef HAVE_LIBPTHREAD
240 #include <pthread.h>
241
242 /*
243  * Completion
244  */
245
246 struct cfs_completion {
247         int c_done;
248         pthread_cond_t c_cond;
249         pthread_mutex_t c_mut;
250 };
251
252 void cfs_init_completion(struct cfs_completion *c);
253 void cfs_fini_completion(struct cfs_completion *c);
254 void cfs_complete(struct cfs_completion *c);
255 void cfs_wait_for_completion(struct cfs_completion *c);
256
257 /*
258  * atomic.h
259  */
260
261 typedef struct { volatile int counter; } cfs_atomic_t;
262
263 int cfs_atomic_read(cfs_atomic_t *a);
264 void cfs_atomic_set(cfs_atomic_t *a, int b);
265 int cfs_atomic_dec_and_test(cfs_atomic_t *a);
266 void cfs_atomic_inc(cfs_atomic_t *a);
267 void cfs_atomic_dec(cfs_atomic_t *a);
268 void cfs_atomic_add(int b, cfs_atomic_t *a);
269 void cfs_atomic_sub(int b, cfs_atomic_t *a);
270
271 #endif /* HAVE_LIBPTHREAD */
272
273 /**************************************************************************
274  *
275  * Mutex interface.
276  *
277  **************************************************************************/
278
279 struct mutex {
280         struct semaphore m_sem;
281 };
282
283 #define DEFINE_MUTEX(m) struct mutex m
284
285 static inline void mutex_init(struct mutex *mutex)
286 {
287         init_mutex(&mutex->m_sem);
288 }
289
290 static inline void mutex_lock(struct mutex *mutex)
291 {
292         mutex_down(&mutex->m_sem);
293 }
294
295 static inline void mutex_unlock(struct mutex *mutex)
296 {
297         mutex_up(&mutex->m_sem);
298 }
299
300 /**
301  * Try-lock this mutex.
302  *
303  *
304  * \retval 0 try-lock succeeded (lock acquired).
305  * \retval errno indicates lock contention.
306  */
307 static inline int mutex_down_trylock(struct mutex *mutex)
308 {
309         return 0;
310 }
311
312 /**
313  * Try-lock this mutex.
314  *
315  * Note, return values are negation of what is expected from down_trylock() or
316  * pthread_mutex_trylock().
317  *
318  * \retval 1 try-lock succeeded (lock acquired).
319  * \retval 0 indicates lock contention.
320  */
321 static inline int mutex_trylock(struct mutex *mutex)
322 {
323         return !mutex_down_trylock(mutex);
324 }
325
326 static inline void mutex_destroy(struct mutex *lock)
327 {
328 }
329
330 /*
331  * This is for use in assertions _only_, i.e., this function should always
332  * return 1.
333  *
334  * \retval 1 mutex is locked.
335  *
336  * \retval 0 mutex is not locked. This should never happen.
337  */
338 static inline int mutex_is_locked(struct mutex *lock)
339 {
340         return 1;
341 }
342
343
344 /**************************************************************************
345  *
346  * Lockdep "implementation". Also see lustre_compat25.h
347  *
348  **************************************************************************/
349
350 struct lock_class_key {
351         ;
352 };
353
354 static inline void lockdep_set_class(void *lock, struct lock_class_key *key)
355 {
356 }
357
358 /* This has to be a macro, so that can be undefined in kernels that do not
359  * support lockdep. */
360 #define mutex_lock_nested(mutex, subclass) mutex_lock(mutex)
361 #define spin_lock_nested(lock, subclass) spin_lock(lock)
362 #define down_read_nested(lock, subclass) down_read(lock)
363 #define down_write_nested(lock, subclass) down_write(lock)
364
365 /* !__KERNEL__ */
366 #endif
367
368 /* __LIBCFS_USER_LOCK_H__ */
369 #endif
370 /*
371  * Local variables:
372  * c-indentation-style: "K&R"
373  * c-basic-offset: 8
374  * tab-width: 8
375  * fill-column: 80
376  * scroll-step: 1
377  * End:
378  */