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