Whamcloud - gitweb
LU-3963 libcfs: remove proc handler wrappers
[fs/lustre-release.git] / libcfs / libcfs / user-lock.c
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/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 int __down_interruptible(struct semaphore *s)
136 {
137         LASSERT(s != NULL);
138         (void)s;
139         return 0;
140 }
141
142 void __up(struct semaphore *s)
143 {
144         LASSERT(s != NULL);
145         (void)s;
146 }
147
148
149 /*
150  * Completion:
151  *
152  * - init_completion(c)
153  * - complete(c)
154  * - wait_for_completion(c)
155  */
156 static wait_handler_t wait_handler;
157
158 void init_completion_module(wait_handler_t handler)
159 {
160         wait_handler = handler;
161 }
162
163 int call_wait_handler(int timeout)
164 {
165         if (!wait_handler)
166                 return -ENOSYS;
167         return wait_handler(timeout);
168 }
169
170 #ifndef HAVE_LIBPTHREAD
171 void init_completion(struct completion *c)
172 {
173         LASSERT(c != NULL);
174         c->done = 0;
175         init_waitqueue_head(&c->wait);
176 }
177
178 void fini_completion(struct completion *c)
179 {
180 }
181
182 void complete(struct completion *c)
183 {
184         LASSERT(c != NULL);
185         c->done  = 1;
186         wake_up(&c->wait);
187 }
188
189 void wait_for_completion(struct completion *c)
190 {
191         LASSERT(c != NULL);
192         do {
193                 if (call_wait_handler(1000) < 0)
194                         break;
195         } while (c->done == 0);
196 }
197
198 int wait_for_completion_interruptible(struct completion *c)
199 {
200         LASSERT(c != NULL);
201         do {
202                 if (call_wait_handler(1000) < 0)
203                         break;
204         } while (c->done == 0);
205         return 0;
206 }
207 #endif /* HAVE_LIBPTHREAD */
208
209 /*
210  * rw_semaphore:
211  *
212  * - DECLARE_RWSEM(x)
213  * - init_rwsem(x)
214  * - down_read(x)
215  * - up_read(x)
216  * - down_write(x)
217  * - up_write(x)
218  */
219
220 void init_rwsem(struct rw_semaphore *s)
221 {
222         LASSERT(s != NULL);
223         (void)s;
224 }
225
226 void down_read(struct rw_semaphore *s)
227 {
228         LASSERT(s != NULL);
229         (void)s;
230 }
231
232 int down_read_trylock(struct rw_semaphore *s)
233 {
234         LASSERT(s != NULL);
235         (void)s;
236         return 1;
237 }
238
239 void down_write(struct rw_semaphore *s)
240 {
241         LASSERT(s != NULL);
242         (void)s;
243 }
244
245 void downgrade_write(struct rw_semaphore *s)
246 {
247         LASSERT(s != NULL);
248         (void)s;
249 }
250
251 int down_write_trylock(struct rw_semaphore *s)
252 {
253         LASSERT(s != NULL);
254         (void)s;
255         return 1;
256 }
257
258 void up_read(struct rw_semaphore *s)
259 {
260         LASSERT(s != NULL);
261         (void)s;
262 }
263
264 void up_write(struct rw_semaphore *s)
265 {
266         LASSERT(s != NULL);
267         (void)s;
268 }
269
270 void fini_rwsem(struct rw_semaphore *s)
271 {
272         LASSERT(s != NULL);
273         (void)s;
274 }
275
276 #ifdef HAVE_LIBPTHREAD
277
278 /*
279  * Multi-threaded user space completion
280  */
281
282 void init_completion(struct completion *c)
283 {
284         LASSERT(c != NULL);
285         c->c_done = 0;
286         pthread_mutex_init(&c->c_mut, NULL);
287         pthread_cond_init(&c->c_cond, NULL);
288 }
289
290 void fini_completion(struct completion *c)
291 {
292         LASSERT(c != NULL);
293         pthread_mutex_destroy(&c->c_mut);
294         pthread_cond_destroy(&c->c_cond);
295 }
296
297 void complete(struct completion *c)
298 {
299         LASSERT(c != NULL);
300         pthread_mutex_lock(&c->c_mut);
301         c->c_done++;
302         pthread_cond_signal(&c->c_cond);
303         pthread_mutex_unlock(&c->c_mut);
304 }
305
306 void wait_for_completion(struct completion *c)
307 {
308         LASSERT(c != NULL);
309         pthread_mutex_lock(&c->c_mut);
310         while (c->c_done == 0)
311                 pthread_cond_wait(&c->c_cond, &c->c_mut);
312         c->c_done--;
313         pthread_mutex_unlock(&c->c_mut);
314 }
315
316 /*
317  * Multi-threaded user space atomic primitives
318  */
319
320 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
321
322 int mt_atomic_read(mt_atomic_t *a)
323 {
324         int r;
325
326         pthread_mutex_lock(&atomic_guard_lock);
327         r = a->counter;
328         pthread_mutex_unlock(&atomic_guard_lock);
329         return r;
330 }
331
332 void mt_atomic_set(mt_atomic_t *a, int b)
333 {
334         pthread_mutex_lock(&atomic_guard_lock);
335         a->counter = b;
336         pthread_mutex_unlock(&atomic_guard_lock);
337 }
338
339 int mt_atomic_dec_and_test(mt_atomic_t *a)
340 {
341         int r;
342
343         pthread_mutex_lock(&atomic_guard_lock);
344         r = --a->counter;
345         pthread_mutex_unlock(&atomic_guard_lock);
346         return (r == 0);
347 }
348
349 void mt_atomic_inc(mt_atomic_t *a)
350 {
351         pthread_mutex_lock(&atomic_guard_lock);
352         ++a->counter;
353         pthread_mutex_unlock(&atomic_guard_lock);
354 }
355
356 void mt_atomic_dec(mt_atomic_t *a)
357 {
358         pthread_mutex_lock(&atomic_guard_lock);
359         --a->counter;
360         pthread_mutex_unlock(&atomic_guard_lock);
361 }
362 void mt_atomic_add(int b, mt_atomic_t *a)
363
364 {
365         pthread_mutex_lock(&atomic_guard_lock);
366         a->counter += b;
367         pthread_mutex_unlock(&atomic_guard_lock);
368 }
369
370 void mt_atomic_sub(int b, mt_atomic_t *a)
371 {
372         pthread_mutex_lock(&atomic_guard_lock);
373         a->counter -= b;
374         pthread_mutex_unlock(&atomic_guard_lock);
375 }
376
377 #endif /* HAVE_LIBPTHREAD */
378
379
380 /* !__KERNEL__ */
381 #endif
382
383 /*
384  * Local variables:
385  * c-indentation-style: "K&R"
386  * c-basic-offset: 8
387  * tab-width: 8
388  * fill-column: 80
389  * scroll-step: 1
390  * End:
391  */