Whamcloud - gitweb
4759e8216d94cf201bcd46ca1433a9b2863af623
[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 [sun.com URL with a
20  * copy of GPLv2].
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 #include <stdio.h>
60 #include <stdlib.h>
61
62 #if 0
63 /*
64  * Optional debugging (magic stamping and checking ownership) can be added.
65  */
66
67 /*
68  * spin_lock
69  *
70  * - spin_lock_init(x)
71  * - spin_lock(x)
72  * - spin_unlock(x)
73  * - spin_trylock(x)
74  *
75  * - spin_lock_irqsave(x, f)
76  * - spin_unlock_irqrestore(x, f)
77  *
78  * No-op implementation.
79  */
80 struct spin_lock {int foo;};
81
82 typedef struct spin_lock spinlock_t;
83
84 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
85 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
86
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;}
95
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){}
98
99 /*
100  * Semaphore
101  *
102  * - sema_init(x, v)
103  * - __down(x)
104  * - __up(x)
105  */
106 typedef struct semaphore {
107     int foo;
108 } mutex_t;
109
110 void sema_init(struct semaphore *s, int val);
111 void __down(struct semaphore *s);
112 void __up(struct semaphore *s);
113
114 /*
115  * Mutex:
116  *
117  * - init_mutex(x)
118  * - init_mutex_locked(x)
119  * - mutex_up(x)
120  * - mutex_down(x)
121  */
122 #define mutex_up(s)                     __up(s)
123 #define mutex_down(s)                   __down(s)
124
125 #define init_mutex(x)                   sema_init(x, 1)
126 #define init_mutex_locked(x)            sema_init(x, 0)
127
128 /*
129  * Completion:
130  *
131  * - init_completion(c)
132  * - complete(c)
133  * - wait_for_completion(c)
134  */
135 #if 0
136 struct completion {};
137
138 void init_completion(struct completion *c);
139 void complete(struct completion *c);
140 void wait_for_completion(struct completion *c);
141 #endif
142
143 /*
144  * rw_semaphore:
145  *
146  * - init_rwsem(x)
147  * - down_read(x)
148  * - up_read(x)
149  * - down_write(x)
150  * - up_write(x)
151  */
152 struct rw_semaphore {};
153
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);
161
162 /*
163  * read-write lock : Need to be investigated more!!
164  * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
165  *
166  * - DECLARE_RWLOCK(l)
167  * - rwlock_init(x)
168  * - read_lock(x)
169  * - read_unlock(x)
170  * - write_lock(x)
171  * - write_unlock(x)
172  */
173 typedef struct rw_semaphore rwlock_t;
174
175 #define rwlock_init(pl)         init_rwsem(pl)
176
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)
181
182 static inline void
183 write_lock_irqsave(rwlock_t *l, unsigned long f) { write_lock(l); }
184 static inline void
185 write_unlock_irqrestore(rwlock_t *l, unsigned long f) { write_unlock(l); }
186
187 static inline void 
188 read_lock_irqsave(rwlock_t *l, unsigned long f) { read_lock(l); }
189 static inline void
190 read_unlock_irqrestore(rwlock_t *l, unsigned long f) { read_unlock(l); }
191
192 /*
193  * Atomic for user-space
194  * Copied from liblustre
195  */
196 typedef struct { volatile int counter; } atomic_t;
197
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)
208
209 #endif
210
211 #ifdef HAVE_LIBPTHREAD
212 #include <pthread.h>
213
214 /*
215  * Completion
216  */
217
218 struct cfs_completion {
219         int c_done;
220         pthread_cond_t c_cond;
221         pthread_mutex_t c_mut;
222 };
223
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);
228
229 /*
230  * atomic.h
231  */
232
233 typedef struct { volatile int counter; } cfs_atomic_t;
234
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);
242
243 #endif /* HAVE_LIBPTHREAD */
244
245 /* !__KERNEL__ */
246 #endif
247
248 /* __LIBCFS_USER_LOCK_H__ */
249 #endif
250 /*
251  * Local variables:
252  * c-indentation-style: "K&R"
253  * c-basic-offset: 8
254  * tab-width: 8
255  * fill-column: 80
256  * scroll-step: 1
257  * End:
258  */