Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / libcfs / libcfs / user-lock.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  * Author: Nikita Danilov <nikita@clusterfs.com>
6  *
7  * This file is part of Lustre, http://www.lustre.org.
8  *
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.
12  *
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
16  * details.
17  *
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.
21  *
22  * Implementation of portable time API for user-level.
23  *
24  */
25
26 /* Implementations of portable synchronization APIs for liblustre */
27
28 /*
29  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
30  *
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
33  * in other branches.
34  */
35
36 #ifndef __KERNEL__
37
38 #include <stdlib.h>
39 #include <libcfs/libcfs.h>
40 #include <libcfs/kp30.h>
41
42 /*
43  * Optional debugging (magic stamping and checking ownership) can be added.
44  */
45
46 #if 0
47 /*
48  * spin_lock
49  *
50  * - spin_lock_init(x)
51  * - spin_lock(x)
52  * - spin_unlock(x)
53  * - spin_trylock(x)
54  *
55  * - spin_lock_irqsave(x, f)
56  * - spin_unlock_irqrestore(x, f)
57  *
58  * No-op implementation.
59  */
60
61 void spin_lock_init(spinlock_t *lock)
62 {
63         LASSERT(lock != NULL);
64         (void)lock;
65 }
66
67 void spin_lock(spinlock_t *lock)
68 {
69         (void)lock;
70 }
71
72 void spin_unlock(spinlock_t *lock)
73 {
74         (void)lock;
75 }
76
77 int spin_trylock(spinlock_t *lock)
78 {
79         (void)lock;
80         return 1;
81 }
82
83 void spin_lock_bh_init(spinlock_t *lock)
84 {
85         LASSERT(lock != NULL);
86         (void)lock;
87 }
88
89 void spin_lock_bh(spinlock_t *lock)
90 {
91         LASSERT(lock != NULL);
92         (void)lock;
93 }
94
95 void spin_unlock_bh(spinlock_t *lock)
96 {
97         LASSERT(lock != NULL);
98         (void)lock;
99 }
100
101 /*
102  * Semaphore
103  *
104  * - sema_init(x, v)
105  * - __down(x)
106  * - __up(x)
107  */
108 struct semaphore {};
109
110 void sema_init(struct semaphore *s, int val)
111 {
112         LASSERT(s != NULL);
113         (void)s;
114         (void)val;
115 }
116
117 void __down(struct semaphore *s)
118 {
119         LASSERT(s != NULL);
120         (void)s;
121 }
122
123 void __up(struct semaphore *s)
124 {
125         LASSERT(s != NULL);
126         (void)s;
127 }
128
129 /*
130  * Mutex:
131  *
132  * - init_mutex(x)
133  * - init_mutex_locked(x)
134  * - mutex_up(x)
135  * - mutex_down(x)
136  */
137
138 #define mutex_up(s)                     __up(s)
139 #define mutex_down(s)                   __down(s)
140
141 #define init_mutex(x)                   sema_init(x, 1)
142 #define init_mutex_locked(x)            sema_init(x, 0)
143
144 /*
145  * Completion:
146  *
147  * - init_completion(c)
148  * - complete(c)
149  * - wait_for_completion(c)
150  */
151 struct completion {};
152
153 void init_completion(struct completion *c)
154 {
155         LASSERT(c != NULL);
156         (void)c;
157 }
158
159 void complete(struct completion *c)
160 {
161         LASSERT(c != NULL);
162         (void)c;
163 }
164
165 void wait_for_completion(struct completion *c)
166 {
167         LASSERT(c != NULL);
168         (void)c;
169 }
170
171 /*
172  * rw_semaphore:
173  *
174  * - DECLARE_RWSEM(x)
175  * - init_rwsem(x)
176  * - down_read(x)
177  * - up_read(x)
178  * - down_write(x)
179  * - up_write(x)
180  */
181 struct rw_semaphore {};
182
183 void init_rwsem(struct rw_semaphore *s)
184 {
185         LASSERT(s != NULL);
186         (void)s;
187 }
188
189 void down_read(struct rw_semaphore *s)
190 {
191         LASSERT(s != NULL);
192         (void)s;
193 }
194
195 int down_read_trylock(struct rw_semaphore *s)
196 {
197         LASSERT(s != NULL);
198         (void)s;
199         return 1;
200 }
201
202 void down_write(struct rw_semaphore *s)
203 {
204         LASSERT(s != NULL);
205         (void)s;
206 }
207
208 int down_write_trylock(struct rw_semaphore *s)
209 {
210         LASSERT(s != NULL);
211         (void)s;
212         return 1;
213 }
214
215 void up_read(struct rw_semaphore *s)
216 {
217         LASSERT(s != NULL);
218         (void)s;
219 }
220
221 void up_write(struct rw_semaphore *s)
222 {
223         LASSERT(s != NULL);
224         (void)s;
225 }
226 #endif
227
228 #ifdef HAVE_LIBPTHREAD
229
230 /*
231  * Completion
232  */
233
234 void cfs_init_completion(struct cfs_completion *c)
235 {
236         LASSERT(c != NULL);
237         c->c_done = 0;
238         pthread_mutex_init(&c->c_mut, NULL);
239         pthread_cond_init(&c->c_cond, NULL);
240 }
241
242 void cfs_fini_completion(struct cfs_completion *c)
243 {
244         LASSERT(c != NULL);
245         pthread_mutex_destroy(&c->c_mut);
246         pthread_cond_destroy(&c->c_cond);
247 }
248
249 void cfs_complete(struct cfs_completion *c)
250 {
251         LASSERT(c != NULL);
252         pthread_mutex_lock(&c->c_mut);
253         c->c_done++;
254         pthread_cond_signal(&c->c_cond);
255         pthread_mutex_unlock(&c->c_mut);
256 }
257
258 void cfs_wait_for_completion(struct cfs_completion *c)
259 {
260         LASSERT(c != NULL);
261         pthread_mutex_lock(&c->c_mut);
262         while (c->c_done == 0)
263                 pthread_cond_wait(&c->c_cond, &c->c_mut);
264         c->c_done--;
265         pthread_mutex_unlock(&c->c_mut);
266 }
267
268 /*
269  * atomic primitives
270  */
271
272 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
273
274 int cfs_atomic_read(cfs_atomic_t *a)
275 {
276         int r;
277
278         pthread_mutex_lock(&atomic_guard_lock);
279         r = a->counter;
280         pthread_mutex_unlock(&atomic_guard_lock);
281         return r;
282 }
283
284 void cfs_atomic_set(cfs_atomic_t *a, int b)
285 {
286         pthread_mutex_lock(&atomic_guard_lock);
287         a->counter = b;
288         pthread_mutex_unlock(&atomic_guard_lock);
289 }
290
291 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
292 {
293         int r;
294
295         pthread_mutex_lock(&atomic_guard_lock);
296         r = --a->counter;
297         pthread_mutex_unlock(&atomic_guard_lock);
298         return (r == 0);
299 }
300
301 void cfs_atomic_inc(cfs_atomic_t *a)
302 {
303         pthread_mutex_lock(&atomic_guard_lock);
304         ++a->counter;
305         pthread_mutex_unlock(&atomic_guard_lock);
306 }
307
308 void cfs_atomic_dec(cfs_atomic_t *a)
309 {
310         pthread_mutex_lock(&atomic_guard_lock);
311         --a->counter;
312         pthread_mutex_unlock(&atomic_guard_lock);
313 }
314 void cfs_atomic_add(int b, cfs_atomic_t *a)
315
316 {
317         pthread_mutex_lock(&atomic_guard_lock);
318         a->counter += b;
319         pthread_mutex_unlock(&atomic_guard_lock);
320 }
321
322 void cfs_atomic_sub(int b, cfs_atomic_t *a)
323 {
324         pthread_mutex_lock(&atomic_guard_lock);
325         a->counter -= b;
326         pthread_mutex_unlock(&atomic_guard_lock);
327 }
328
329 #endif /* HAVE_LIBPTHREAD */
330
331
332 /* !__KERNEL__ */
333 #endif
334
335 /*
336  * Local variables:
337  * c-indentation-style: "K&R"
338  * c-basic-offset: 8
339  * tab-width: 8
340  * fill-column: 80
341  * scroll-step: 1
342  * End:
343  */