Whamcloud - gitweb
LU-2869 llite: extended attribute cache
[fs/lustre-release.git] / libcfs / include / libcfs / user-lock.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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  * - spin_lock_bh_init(x)
78  * - spin_lock_bh(x)
79  * - spin_unlock_bh(x)
80  *
81  * - spin_is_locked(x)
82  * - spin_lock_irqsave(x, f)
83  * - spin_unlock_irqrestore(x, f)
84  *
85  * No-op implementation.
86  */
87 struct spin_lock { int foo; };
88
89 typedef struct spin_lock spinlock_t;
90
91 #define DEFINE_SPINLOCK(lock)           spinlock_t lock = { }
92 #define LASSERT_SPIN_LOCKED(lock)       do { (void)sizeof(lock); } while (0)
93 #define LINVRNT_SPIN_LOCKED(lock)       do { (void)sizeof(lock); } while (0)
94 #define LASSERT_SEM_LOCKED(sem)         do { (void)sizeof(sem); } while (0)
95 #define LASSERT_MUTEX_LOCKED(x)         do { (void)sizeof(x); } while (0)
96 #define __SPIN_LOCK_UNLOCKED(x)         ((spinlock_t) {})
97
98 void spin_lock_init(spinlock_t *lock);
99 void spin_lock(spinlock_t *lock);
100 void spin_unlock(spinlock_t *lock);
101 int  spin_trylock(spinlock_t *lock);
102 void spin_lock_bh_init(spinlock_t *lock);
103 void spin_lock_bh(spinlock_t *lock);
104 void spin_unlock_bh(spinlock_t *lock);
105
106 static inline int spin_is_locked(spinlock_t *l) { return 1; }
107 static inline void spin_lock_irqsave(spinlock_t *l, unsigned long f) {}
108 static inline void spin_unlock_irqrestore(spinlock_t *l, unsigned long f) {}
109
110 /*
111  * Semaphore
112  *
113  * - sema_init(x, v)
114  * - __down(x)
115  * - __up(x)
116  */
117 struct semaphore {
118         int foo;
119 };
120
121 void sema_init(struct semaphore *s, int val);
122 void __up(struct semaphore *s);
123 void __down(struct semaphore *s);
124 int __down_interruptible(struct semaphore *s);
125
126 #define DEFINE_SEMAPHORE(name)      struct semaphore name = { 1 }
127
128 #define up(s)                           __up(s)
129 #define down(s)                 __down(s)
130 #define down_interruptible(s)           __down_interruptible(s)
131
132 static inline int down_trylock(struct semaphore *sem)
133 {
134         return 0;
135 }
136
137 /*
138  * Completion:
139  *
140  * - init_completion_module(c)
141  * - call_wait_handler(t)
142  * - init_completion(c)
143  * - complete(c)
144  * - wait_for_completion(c)
145  * - wait_for_completion_interruptible(c)
146  */
147 #ifdef HAVE_LIBPTHREAD
148 #include <pthread.h>
149
150 /*
151  * Multi-threaded user space completion APIs
152  */
153
154 struct completion {
155         int             c_done;
156         pthread_cond_t  c_cond;
157         pthread_mutex_t c_mut;
158 };
159
160 #else /* !HAVE_LIBPTHREAD */
161
162 struct completion {
163         unsigned int    done;
164         cfs_waitq_t     wait;
165 };
166 #endif /* HAVE_LIBPTHREAD */
167
168 typedef int (*wait_handler_t) (int timeout);
169 void init_completion_module(wait_handler_t handler);
170 int  call_wait_handler(int timeout);
171 void init_completion(struct completion *c);
172 void fini_completion(struct completion *c);
173 void complete(struct completion *c);
174 void wait_for_completion(struct completion *c);
175 int wait_for_completion_interruptible(struct completion *c);
176
177 #define COMPLETION_INITIALIZER(work) \
178         { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
179
180
181 #define INIT_COMPLETION(x)      ((x).done = 0)
182
183
184 /*
185  * rw_semaphore:
186  *
187  * - init_rwsem(x)
188  * - down_read(x)
189  * - down_read_trylock(x)
190  * - down_write(struct rw_semaphore *s);
191  * - down_write_trylock(struct rw_semaphore *s);
192  * - up_read(x)
193  * - up_write(x)
194  * - fini_rwsem(x)
195  */
196 struct rw_semaphore {
197         int foo;
198 };
199
200 void init_rwsem(struct rw_semaphore *s);
201 void down_read(struct rw_semaphore *s);
202 int down_read_trylock(struct rw_semaphore *s);
203 void down_write(struct rw_semaphore *s);
204 void downgrade_write(struct rw_semaphore *s);
205 int down_write_trylock(struct rw_semaphore *s);
206 void up_read(struct rw_semaphore *s);
207 void up_write(struct rw_semaphore *s);
208 void fini_rwsem(struct rw_semaphore *s);
209 #define DECLARE_RWSEM(name)  struct rw_semaphore name = { }
210
211 /*
212  * read-write lock : Need to be investigated more!!
213  * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
214  *
215  * - rwlock_init(x)
216  * - read_lock(x)
217  * - read_unlock(x)
218  * - write_lock(x)
219  * - write_unlock(x)
220  * - write_lock_irqsave(x)
221  * - write_unlock_irqrestore(x)
222  * - read_lock_irqsave(x)
223  * - read_unlock_irqrestore(x)
224  */
225 #define rwlock_t                struct rw_semaphore
226 #define DEFINE_RWLOCK(lock)     rwlock_t lock = { }
227
228 #define rwlock_init(pl)         init_rwsem(pl)
229
230 #define read_lock(l)            down_read(l)
231 #define read_unlock(l)          up_read(l)
232 #define write_lock(l)           down_write(l)
233 #define write_unlock(l)         up_write(l)
234
235 static inline void write_lock_irqsave(rwlock_t *l, unsigned long f)
236 {
237         write_lock(l);
238 }
239
240 static inline void write_unlock_irqrestore(rwlock_t *l, unsigned long f)
241 {
242         write_unlock(l);
243 }
244
245 static inline void read_lock_irqsave(rwlock_t *l, unsigned long f)
246 {
247         read_lock(l);
248 }
249
250 static inline void read_unlock_irqrestore(rwlock_t *l, unsigned long f)
251 {
252         read_unlock(l);
253 }
254
255 /*
256  * Atomic for single-threaded user-space
257  */
258 typedef struct { volatile int counter; } cfs_atomic_t;
259
260 #define CFS_ATOMIC_INIT(i) { (i) }
261
262 #define cfs_atomic_read(a) ((a)->counter)
263 #define cfs_atomic_set(a,b) do {(a)->counter = b; } while (0)
264 #define cfs_atomic_dec_and_test(a) ((--((a)->counter)) == 0)
265 #define cfs_atomic_dec_and_lock(a,b) ((--((a)->counter)) == 0)
266 #define cfs_atomic_inc(a)  (((a)->counter)++)
267 #define cfs_atomic_dec(a)  do { (a)->counter--; } while (0)
268 #define cfs_atomic_add(b,a)  do {(a)->counter += b;} while (0)
269 #define cfs_atomic_add_return(n,a) ((a)->counter += n)
270 #define cfs_atomic_inc_return(a) cfs_atomic_add_return(1,a)
271 #define cfs_atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
272 #define cfs_atomic_sub_return(n,a) ((a)->counter -= n)
273 #define cfs_atomic_dec_return(a)  cfs_atomic_sub_return(1,a)
274 #define cfs_atomic_add_unless(v, a, u) \
275         ((v)->counter != u ? (v)->counter += a : 0)
276 #define cfs_atomic_inc_not_zero(v) cfs_atomic_add_unless((v), 1, 0)
277 #define cfs_atomic_cmpxchg(v, ov, nv) \
278         ((v)->counter == ov ? ((v)->counter = nv, ov) : (v)->counter)
279
280 #ifdef HAVE_LIBPTHREAD
281 #include <pthread.h>
282
283 /*
284  * Multi-threaded user space atomic APIs
285  */
286
287 typedef struct { volatile int counter; } mt_atomic_t;
288
289 int mt_atomic_read(mt_atomic_t *a);
290 void mt_atomic_set(mt_atomic_t *a, int b);
291 int mt_atomic_dec_and_test(mt_atomic_t *a);
292 void mt_atomic_inc(mt_atomic_t *a);
293 void mt_atomic_dec(mt_atomic_t *a);
294 void mt_atomic_add(int b, mt_atomic_t *a);
295 void mt_atomic_sub(int b, mt_atomic_t *a);
296
297 #endif /* HAVE_LIBPTHREAD */
298
299 /**************************************************************************
300  *
301  * Mutex interface.
302  *
303  **************************************************************************/
304 #define mutex semaphore
305
306 #define DEFINE_MUTEX(m) DEFINE_SEMAPHORE(m)
307
308 static inline void mutex_init(struct mutex *mutex)
309 {
310         sema_init(mutex, 1);
311 }
312
313 static inline void mutex_lock(struct mutex *mutex)
314 {
315         down(mutex);
316 }
317
318 static inline void mutex_unlock(struct mutex *mutex)
319 {
320         up(mutex);
321 }
322
323 static inline int mutex_lock_interruptible(struct mutex *mutex)
324 {
325         return down_interruptible(mutex);
326 }
327
328 /**
329  * Try-lock this mutex.
330  *
331  * Note, return values are negation of what is expected from down_trylock() or
332  * pthread_mutex_trylock().
333  *
334  * \retval 1 try-lock succeeded (lock acquired).
335  * \retval 0 indicates lock contention.
336  */
337 static inline int mutex_trylock(struct mutex *mutex)
338 {
339         return !down_trylock(mutex);
340 }
341
342 static inline void mutex_destroy(struct mutex *lock)
343 {
344 }
345
346 /*
347  * This is for use in assertions _only_, i.e., this function should always
348  * return 1.
349  *
350  * \retval 1 mutex is locked.
351  *
352  * \retval 0 mutex is not locked. This should never happen.
353  */
354 static inline int mutex_is_locked(struct mutex *lock)
355 {
356         return 1;
357 }
358
359
360 /**************************************************************************
361  *
362  * Lockdep "implementation". Also see lustre_compat25.h
363  *
364  **************************************************************************/
365
366 struct lock_class_key {
367         int foo;
368 };
369
370 static inline void lockdep_set_class(void *lock, struct lock_class_key *key)
371 {
372 }
373
374 static inline void lockdep_off(void)
375 {
376 }
377
378 static inline void lockdep_on(void)
379 {
380 }
381
382 #define mutex_lock_nested(mutex, subclass) mutex_lock(mutex)
383 #define spin_lock_nested(lock, subclass) spin_lock(lock)
384 #define down_read_nested(lock, subclass) down_read(lock)
385 #define down_write_nested(lock, subclass) down_write(lock)
386
387
388 /* !__KERNEL__ */
389 #endif
390
391 /* __LIBCFS_USER_LOCK_H__ */
392 #endif
393 /*
394  * Local variables:
395  * c-indentation-style: "K&R"
396  * c-basic-offset: 8
397  * tab-width: 8
398  * fill-column: 80
399  * scroll-step: 1
400  * End:
401  */