Whamcloud - gitweb
b=18728 allow queued write syscall termination by a signal
[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 (c) 2008, 2010, Oracle and/or its affiliates. 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 cfs_spin_lock_init(cfs_spinlock_t *lock)
75 {
76         LASSERT(lock != NULL);
77         (void)lock;
78 }
79
80 void cfs_spin_lock(cfs_spinlock_t *lock)
81 {
82         (void)lock;
83 }
84
85 void cfs_spin_unlock(cfs_spinlock_t *lock)
86 {
87         (void)lock;
88 }
89
90 int cfs_spin_trylock(cfs_spinlock_t *lock)
91 {
92         (void)lock;
93         return 1;
94 }
95
96 void cfs_spin_lock_bh_init(cfs_spinlock_t *lock)
97 {
98         LASSERT(lock != NULL);
99         (void)lock;
100 }
101
102 void cfs_spin_lock_bh(cfs_spinlock_t *lock)
103 {
104         LASSERT(lock != NULL);
105         (void)lock;
106 }
107
108 void cfs_spin_unlock_bh(cfs_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 cfs_sema_init(cfs_semaphore_t *s, int val)
123 {
124         LASSERT(s != NULL);
125         (void)s;
126         (void)val;
127 }
128
129 void __down(cfs_semaphore_t *s)
130 {
131         LASSERT(s != NULL);
132         (void)s;
133 }
134
135 int __down_interruptible(cfs_semaphore_t *s)
136 {
137         LASSERT(s != NULL);
138         (void)s;
139         return 0;
140 }
141
142 void __up(cfs_semaphore_t *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 cfs_wait_handler_t wait_handler;
158
159 void cfs_init_completion_module(cfs_wait_handler_t handler)
160 {
161         wait_handler = handler;
162 }
163
164 int cfs_call_wait_handler(int timeout)
165 {
166         if (!wait_handler)
167                 return -ENOSYS;
168         return wait_handler(timeout);
169 }
170
171 void cfs_init_completion(cfs_completion_t *c)
172 {
173         LASSERT(c != NULL);
174         c->done = 0;
175         cfs_waitq_init(&c->wait);
176 }
177
178 void cfs_complete(cfs_completion_t *c)
179 {
180         LASSERT(c != NULL);
181         c->done  = 1;
182         cfs_waitq_signal(&c->wait);
183 }
184
185 void cfs_wait_for_completion(cfs_completion_t *c)
186 {
187         LASSERT(c != NULL);
188         do {
189                 if (cfs_call_wait_handler(1000) < 0)
190                         break;
191         } while (c->done == 0);
192 }
193
194 int cfs_wait_for_completion_interruptible(cfs_completion_t *c)
195 {
196         LASSERT(c != NULL);
197         do {
198                 if (cfs_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 cfs_init_rwsem(cfs_rw_semaphore_t *s)
216 {
217         LASSERT(s != NULL);
218         (void)s;
219 }
220
221 void cfs_down_read(cfs_rw_semaphore_t *s)
222 {
223         LASSERT(s != NULL);
224         (void)s;
225 }
226
227 int cfs_down_read_trylock(cfs_rw_semaphore_t *s)
228 {
229         LASSERT(s != NULL);
230         (void)s;
231         return 1;
232 }
233
234 void cfs_down_write(cfs_rw_semaphore_t *s)
235 {
236         LASSERT(s != NULL);
237         (void)s;
238 }
239
240 int cfs_down_write_trylock(cfs_rw_semaphore_t *s)
241 {
242         LASSERT(s != NULL);
243         (void)s;
244         return 1;
245 }
246
247 void cfs_up_read(cfs_rw_semaphore_t *s)
248 {
249         LASSERT(s != NULL);
250         (void)s;
251 }
252
253 void cfs_up_write(cfs_rw_semaphore_t *s)
254 {
255         LASSERT(s != NULL);
256         (void)s;
257 }
258
259 void cfs_fini_rwsem(cfs_rw_semaphore_t *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 cfs_mt_init_completion(cfs_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 cfs_mt_fini_completion(cfs_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 cfs_mt_complete(cfs_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 cfs_mt_wait_for_completion(cfs_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 cfs_mt_atomic_read(cfs_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 cfs_mt_atomic_set(cfs_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 cfs_mt_atomic_dec_and_test(cfs_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 cfs_mt_atomic_inc(cfs_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 cfs_mt_atomic_dec(cfs_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 cfs_mt_atomic_add(int b, cfs_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 cfs_mt_atomic_sub(int b, cfs_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  */