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