Whamcloud - gitweb
Branch HEAD
[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  * Copyright (C) 2004 Cluster File Systems, Inc.
5  * Author: Nikita Danilov <nikita@clusterfs.com>
6  *
7  * This file is part of Lustre, http://www.lustre.org.
8  *
9  * Lustre is free software; you can redistribute it and/or modify it under the
10  * terms of version 2 of the GNU General Public License as published by the
11  * Free Software Foundation.
12  *
13  * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
20  * Ave, Cambridge, MA 02139, USA.
21  *
22  * Implementation of portable time API for user-level.
23  *
24  */
25
26 /* Implementations of portable synchronization APIs for liblustre */
27
28 /*
29  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
30  *
31  * XXX Liang: There are several branches share lnet with b_hd_newconfig,
32  * if we define lock APIs at here, there will be conflict with liblustre
33  * in other branches.
34  */
35
36 #ifndef __KERNEL__
37
38 #include <stdlib.h>
39 #include <libcfs/libcfs.h>
40
41 /*
42  * Optional debugging (magic stamping and checking ownership) can be added.
43  */
44
45 #if 0
46 /*
47  * spin_lock
48  *
49  * - spin_lock_init(x)
50  * - spin_lock(x)
51  * - spin_unlock(x)
52  * - spin_trylock(x)
53  *
54  * - spin_lock_irqsave(x, f)
55  * - spin_unlock_irqrestore(x, f)
56  *
57  * No-op implementation.
58  */
59
60 void spin_lock_init(spinlock_t *lock)
61 {
62         LASSERT(lock != NULL);
63         (void)lock;
64 }
65
66 void spin_lock(spinlock_t *lock)
67 {
68         (void)lock;
69 }
70
71 void spin_unlock(spinlock_t *lock)
72 {
73         (void)lock;
74 }
75
76 int spin_trylock(spinlock_t *lock)
77 {
78         (void)lock;
79         return 1;
80 }
81
82 void spin_lock_bh_init(spinlock_t *lock)
83 {
84         LASSERT(lock != NULL);
85         (void)lock;
86 }
87
88 void spin_lock_bh(spinlock_t *lock)
89 {
90         LASSERT(lock != NULL);
91         (void)lock;
92 }
93
94 void spin_unlock_bh(spinlock_t *lock)
95 {
96         LASSERT(lock != NULL);
97         (void)lock;
98 }
99
100 /*
101  * Semaphore
102  *
103  * - sema_init(x, v)
104  * - __down(x)
105  * - __up(x)
106  */
107 struct semaphore {};
108
109 void sema_init(struct semaphore *s, int val)
110 {
111         LASSERT(s != NULL);
112         (void)s;
113         (void)val;
114 }
115
116 void __down(struct semaphore *s)
117 {
118         LASSERT(s != NULL);
119         (void)s;
120 }
121
122 void __up(struct semaphore *s)
123 {
124         LASSERT(s != NULL);
125         (void)s;
126 }
127
128 /*
129  * Mutex:
130  *
131  * - init_mutex(x)
132  * - init_mutex_locked(x)
133  * - mutex_up(x)
134  * - mutex_down(x)
135  */
136
137 #define mutex_up(s)                     __up(s)
138 #define mutex_down(s)                   __down(s)
139
140 #define init_mutex(x)                   sema_init(x, 1)
141 #define init_mutex_locked(x)            sema_init(x, 0)
142
143 /*
144  * Completion:
145  *
146  * - init_completion(c)
147  * - complete(c)
148  * - wait_for_completion(c)
149  */
150 struct completion {};
151
152 void init_completion(struct completion *c)
153 {
154         LASSERT(c != NULL);
155         (void)c;
156 }
157
158 void complete(struct completion *c)
159 {
160         LASSERT(c != NULL);
161         (void)c;
162 }
163
164 void wait_for_completion(struct completion *c)
165 {
166         LASSERT(c != NULL);
167         (void)c;
168 }
169
170 /*
171  * rw_semaphore:
172  *
173  * - DECLARE_RWSEM(x)
174  * - init_rwsem(x)
175  * - down_read(x)
176  * - up_read(x)
177  * - down_write(x)
178  * - up_write(x)
179  */
180 struct rw_semaphore {};
181
182 void init_rwsem(struct rw_semaphore *s)
183 {
184         LASSERT(s != NULL);
185         (void)s;
186 }
187
188 void down_read(struct rw_semaphore *s)
189 {
190         LASSERT(s != NULL);
191         (void)s;
192 }
193
194 int down_read_trylock(struct rw_semaphore *s)
195 {
196         LASSERT(s != NULL);
197         (void)s;
198         return 1;
199 }
200
201 void down_write(struct rw_semaphore *s)
202 {
203         LASSERT(s != NULL);
204         (void)s;
205 }
206
207 int down_write_trylock(struct rw_semaphore *s)
208 {
209         LASSERT(s != NULL);
210         (void)s;
211         return 1;
212 }
213
214 void up_read(struct rw_semaphore *s)
215 {
216         LASSERT(s != NULL);
217         (void)s;
218 }
219
220 void up_write(struct rw_semaphore *s)
221 {
222         LASSERT(s != NULL);
223         (void)s;
224 }
225 #endif
226
227 #ifdef HAVE_LIBPTHREAD
228
229 /*
230  * Completion
231  */
232
233 void cfs_init_completion(struct cfs_completion *c)
234 {
235         LASSERT(c != NULL);
236         c->c_done = 0;
237         pthread_mutex_init(&c->c_mut, NULL);
238         pthread_cond_init(&c->c_cond, NULL);
239 }
240
241 void cfs_fini_completion(struct cfs_completion *c)
242 {
243         LASSERT(c != NULL);
244         pthread_mutex_destroy(&c->c_mut);
245         pthread_cond_destroy(&c->c_cond);
246 }
247
248 void cfs_complete(struct cfs_completion *c)
249 {
250         LASSERT(c != NULL);
251         pthread_mutex_lock(&c->c_mut);
252         c->c_done++;
253         pthread_cond_signal(&c->c_cond);
254         pthread_mutex_unlock(&c->c_mut);
255 }
256
257 void cfs_wait_for_completion(struct cfs_completion *c)
258 {
259         LASSERT(c != NULL);
260         pthread_mutex_lock(&c->c_mut);
261         while (c->c_done == 0)
262                 pthread_cond_wait(&c->c_cond, &c->c_mut);
263         c->c_done--;
264         pthread_mutex_unlock(&c->c_mut);
265 }
266
267 /*
268  * atomic primitives
269  */
270
271 static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER;
272
273 int cfs_atomic_read(cfs_atomic_t *a)
274 {
275         int r;
276
277         pthread_mutex_lock(&atomic_guard_lock);
278         r = a->counter;
279         pthread_mutex_unlock(&atomic_guard_lock);
280         return r;
281 }
282
283 void cfs_atomic_set(cfs_atomic_t *a, int b)
284 {
285         pthread_mutex_lock(&atomic_guard_lock);
286         a->counter = b;
287         pthread_mutex_unlock(&atomic_guard_lock);
288 }
289
290 int cfs_atomic_dec_and_test(cfs_atomic_t *a)
291 {
292         int r;
293
294         pthread_mutex_lock(&atomic_guard_lock);
295         r = --a->counter;
296         pthread_mutex_unlock(&atomic_guard_lock);
297         return (r == 0);
298 }
299
300 void cfs_atomic_inc(cfs_atomic_t *a)
301 {
302         pthread_mutex_lock(&atomic_guard_lock);
303         ++a->counter;
304         pthread_mutex_unlock(&atomic_guard_lock);
305 }
306
307 void cfs_atomic_dec(cfs_atomic_t *a)
308 {
309         pthread_mutex_lock(&atomic_guard_lock);
310         --a->counter;
311         pthread_mutex_unlock(&atomic_guard_lock);
312 }
313 void cfs_atomic_add(int b, cfs_atomic_t *a)
314
315 {
316         pthread_mutex_lock(&atomic_guard_lock);
317         a->counter += b;
318         pthread_mutex_unlock(&atomic_guard_lock);
319 }
320
321 void cfs_atomic_sub(int b, cfs_atomic_t *a)
322 {
323         pthread_mutex_lock(&atomic_guard_lock);
324         a->counter -= b;
325         pthread_mutex_unlock(&atomic_guard_lock);
326 }
327
328 #endif /* HAVE_LIBPTHREAD */
329
330
331 /* !__KERNEL__ */
332 #endif
333
334 /*
335  * Local variables:
336  * c-indentation-style: "K&R"
337  * c-basic-offset: 8
338  * tab-width: 8
339  * fill-column: 80
340  * scroll-step: 1
341  * End:
342  */