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