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