Whamcloud - gitweb
b=20339
[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  * 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/libcfs/user-lock.c
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 /* Implementations of portable synchronization APIs for liblustre */
42
43 /*
44  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
45  *
46  * XXX Liang: There are several branches share lnet with b_hd_newconfig,
47  * if we define lock APIs at here, there will be conflict with liblustre
48  * in other branches.
49  */
50
51 #ifndef __KERNEL__
52
53 #include <libcfs/libcfs.h>
54
55 /*
56  * Optional debugging (magic stamping and checking ownership) can be added.
57  */
58
59 /*
60  * spin_lock
61  *
62  * - spin_lock_init(x)
63  * - spin_lock(x)
64  * - spin_lock_nested(x, subclass)
65  * - spin_unlock(x)
66  * - spin_trylock(x)
67  *
68  * - spin_lock_irqsave(x, f)
69  * - spin_unlock_irqrestore(x, f)
70  *
71  * No-op implementation.
72  */
73
74 void spin_lock_init(spinlock_t *lock)
75 {
76         LASSERT(lock != NULL);
77         (void)lock;
78 }
79
80 void spin_lock(spinlock_t *lock)
81 {
82         (void)lock;
83 }
84
85 void spin_unlock(spinlock_t *lock)
86 {
87         (void)lock;
88 }
89
90 int spin_trylock(spinlock_t *lock)
91 {
92         (void)lock;
93         return 1;
94 }
95
96 void spin_lock_bh_init(spinlock_t *lock)
97 {
98         LASSERT(lock != NULL);
99         (void)lock;
100 }
101
102 void spin_lock_bh(spinlock_t *lock)
103 {
104         LASSERT(lock != NULL);
105         (void)lock;
106 }
107
108 void spin_unlock_bh(spinlock_t *lock)
109 {
110         LASSERT(lock != NULL);
111         (void)lock;
112 }
113
114 /*
115  * Semaphore
116  *
117  * - sema_init(x, v)
118  * - __down(x)
119  * - __up(x)
120  */
121
122 void sema_init(struct semaphore *s, int val)
123 {
124         LASSERT(s != NULL);
125         (void)s;
126         (void)val;
127 }
128
129 void __down(struct semaphore *s)
130 {
131         LASSERT(s != NULL);
132         (void)s;
133 }
134
135 void __up(struct semaphore *s)
136 {
137         LASSERT(s != NULL);
138         (void)s;
139 }
140
141
142 /*
143  * Completion:
144  *
145  * - init_completion(c)
146  * - complete(c)
147  * - wait_for_completion(c)
148  */
149
150 static cfs_wait_handler_t wait_handler;
151
152 void init_completion_module(cfs_wait_handler_t handler)
153 {
154         wait_handler = handler;
155 }
156
157 int call_wait_handler(int timeout)
158 {
159         if (!wait_handler)
160                 return -ENOSYS;
161         return wait_handler(timeout);
162 }
163
164 void init_completion(struct completion *c)
165 {
166         LASSERT(c != NULL);
167         c->done = 0;
168         cfs_waitq_init(&c->wait);
169 }
170
171 void complete(struct completion *c)
172 {
173         LASSERT(c != NULL);
174         c->done  = 1;
175         cfs_waitq_signal(&c->wait);
176 }
177
178 void wait_for_completion(struct completion *c)
179 {
180         LASSERT(c != NULL);
181         do {
182                 if (call_wait_handler(1000) < 0)
183                         break;
184         } while (c->done == 0);
185 }
186
187 int wait_for_completion_interruptible(struct completion *c)
188 {
189         LASSERT(c != NULL);
190         do {
191                 if (call_wait_handler(1000) < 0)
192                         break;
193         } while (c->done == 0);
194         return 0;
195 }
196
197 /*
198  * rw_semaphore:
199  *
200  * - DECLARE_RWSEM(x)
201  * - init_rwsem(x)
202  * - down_read(x)
203  * - up_read(x)
204  * - down_write(x)
205  * - up_write(x)
206  */
207
208 void init_rwsem(struct rw_semaphore *s)
209 {
210         LASSERT(s != NULL);
211         (void)s;
212 }
213
214 void down_read(struct rw_semaphore *s)
215 {
216         LASSERT(s != NULL);
217         (void)s;
218 }
219
220 int down_read_trylock(struct rw_semaphore *s)
221 {
222         LASSERT(s != NULL);
223         (void)s;
224         return 1;
225 }
226
227 void down_write(struct rw_semaphore *s)
228 {
229         LASSERT(s != NULL);
230         (void)s;
231 }
232
233 int down_write_trylock(struct rw_semaphore *s)
234 {
235         LASSERT(s != NULL);
236         (void)s;
237         return 1;
238 }
239
240 void up_read(struct rw_semaphore *s)
241 {
242         LASSERT(s != NULL);
243         (void)s;
244 }
245
246 void up_write(struct rw_semaphore *s)
247 {
248         LASSERT(s != NULL);
249         (void)s;
250 }
251
252 void fini_rwsem(struct rw_semaphore *s)
253 {
254         LASSERT(s != NULL);
255         (void)s;
256 }
257
258 #ifdef HAVE_LIBPTHREAD
259
260 /*
261  * Completion
262  */
263
264 void cfs_init_completion(struct cfs_completion *c)
265 {
266         LASSERT(c != NULL);
267         c->c_done = 0;
268         pthread_mutex_init(&c->c_mut, NULL);
269         pthread_cond_init(&c->c_cond, NULL);
270 }
271
272 void cfs_fini_completion(struct cfs_completion *c)
273 {
274         LASSERT(c != NULL);
275         pthread_mutex_destroy(&c->c_mut);
276         pthread_cond_destroy(&c->c_cond);
277 }
278
279 void cfs_complete(struct cfs_completion *c)
280 {
281         LASSERT(c != NULL);
282         pthread_mutex_lock(&c->c_mut);
283         c->c_done++;
284         pthread_cond_signal(&c->c_cond);
285         pthread_mutex_unlock(&c->c_mut);
286 }
287
288 void cfs_wait_for_completion(struct cfs_completion *c)
289 {
290         LASSERT(c != NULL);
291         pthread_mutex_lock(&c->c_mut);
292         while (c->c_done == 0)
293                 pthread_cond_wait(&c->c_cond, &c->c_mut);
294         c->c_done--;
295         pthread_mutex_unlock(&c->c_mut);
296 }
297
298 /*
299  * atomic primitives
300  */
301
302 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
303
304 int cfs_atomic_read(cfs_atomic_t *a)
305 {
306         int r;
307
308         pthread_mutex_lock(&atomic_guard_lock);
309         r = a->counter;
310         pthread_mutex_unlock(&atomic_guard_lock);
311         return r;
312 }
313
314 void cfs_atomic_set(cfs_atomic_t *a, int b)
315 {
316         pthread_mutex_lock(&atomic_guard_lock);
317         a->counter = b;
318         pthread_mutex_unlock(&atomic_guard_lock);
319 }
320
321 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
322 {
323         int r;
324
325         pthread_mutex_lock(&atomic_guard_lock);
326         r = --a->counter;
327         pthread_mutex_unlock(&atomic_guard_lock);
328         return (r == 0);
329 }
330
331 void cfs_atomic_inc(cfs_atomic_t *a)
332 {
333         pthread_mutex_lock(&atomic_guard_lock);
334         ++a->counter;
335         pthread_mutex_unlock(&atomic_guard_lock);
336 }
337
338 void cfs_atomic_dec(cfs_atomic_t *a)
339 {
340         pthread_mutex_lock(&atomic_guard_lock);
341         --a->counter;
342         pthread_mutex_unlock(&atomic_guard_lock);
343 }
344 void cfs_atomic_add(int b, cfs_atomic_t *a)
345
346 {
347         pthread_mutex_lock(&atomic_guard_lock);
348         a->counter += b;
349         pthread_mutex_unlock(&atomic_guard_lock);
350 }
351
352 void cfs_atomic_sub(int b, cfs_atomic_t *a)
353 {
354         pthread_mutex_lock(&atomic_guard_lock);
355         a->counter -= b;
356         pthread_mutex_unlock(&atomic_guard_lock);
357 }
358
359 #endif /* HAVE_LIBPTHREAD */
360
361
362 /* !__KERNEL__ */
363 #endif
364
365 /*
366  * Local variables:
367  * c-indentation-style: "K&R"
368  * c-basic-offset: 8
369  * tab-width: 8
370  * fill-column: 80
371  * scroll-step: 1
372  * End:
373  */