Whamcloud - gitweb
b=12302
[fs/lustre-release.git] / lnet / include / libcfs / user-lock.h
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 #ifndef __LIBCFS_USER_LOCK_H__
27 #define __LIBCFS_USER_LOCK_H__
28
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
31 #endif
32
33 /* Implementations of portable synchronization APIs for liblustre */
34
35 /*
36  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
37  *
38  * XXX Liang: There are several branches share lnet with b_hd_newconfig,
39  * if we define lock APIs at here, there will be conflict with liblustre
40  * in other branches.
41  */
42
43 #ifndef __KERNEL__
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 #if 0
48 /*
49  * Optional debugging (magic stamping and checking ownership) can be added.
50  */
51
52 /*
53  * spin_lock
54  *
55  * - spin_lock_init(x)
56  * - spin_lock(x)
57  * - spin_unlock(x)
58  * - spin_trylock(x)
59  *
60  * - spin_lock_irqsave(x, f)
61  * - spin_unlock_irqrestore(x, f)
62  *
63  * No-op implementation.
64  */
65 struct spin_lock {int foo;};
66
67 typedef struct spin_lock spinlock_t;
68
69 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
70 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
71
72 void spin_lock_init(spinlock_t *lock);
73 void spin_lock(spinlock_t *lock);
74 void spin_unlock(spinlock_t *lock);
75 int spin_trylock(spinlock_t *lock);
76 void spin_lock_bh_init(spinlock_t *lock);
77 void spin_lock_bh(spinlock_t *lock);
78 void spin_unlock_bh(spinlock_t *lock);
79 static inline int spin_is_locked(spinlock_t *l) {return 1;}
80
81 static inline void spin_lock_irqsave(spinlock_t *l, unsigned long f){}
82 static inline void spin_unlock_irqrestore(spinlock_t *l, unsigned long f){}
83
84 /*
85  * Semaphore
86  *
87  * - sema_init(x, v)
88  * - __down(x)
89  * - __up(x)
90  */
91 typedef struct semaphore {
92     int foo;
93 } mutex_t;
94
95 void sema_init(struct semaphore *s, int val);
96 void __down(struct semaphore *s);
97 void __up(struct semaphore *s);
98
99 /*
100  * Mutex:
101  *
102  * - init_mutex(x)
103  * - init_mutex_locked(x)
104  * - mutex_up(x)
105  * - mutex_down(x)
106  */
107 #define mutex_up(s)                     __up(s)
108 #define mutex_down(s)                   __down(s)
109
110 #define init_mutex(x)                   sema_init(x, 1)
111 #define init_mutex_locked(x)            sema_init(x, 0)
112
113 /*
114  * Completion:
115  *
116  * - init_completion(c)
117  * - complete(c)
118  * - wait_for_completion(c)
119  */
120 #if 0
121 struct completion {};
122
123 void init_completion(struct completion *c);
124 void complete(struct completion *c);
125 void wait_for_completion(struct completion *c);
126 #endif
127
128 /*
129  * rw_semaphore:
130  *
131  * - init_rwsem(x)
132  * - down_read(x)
133  * - up_read(x)
134  * - down_write(x)
135  * - up_write(x)
136  */
137 struct rw_semaphore {};
138
139 void init_rwsem(struct rw_semaphore *s);
140 void down_read(struct rw_semaphore *s);
141 int down_read_trylock(struct rw_semaphore *s);
142 void down_write(struct rw_semaphore *s);
143 int down_write_trylock(struct rw_semaphore *s);
144 void up_read(struct rw_semaphore *s);
145 void up_write(struct rw_semaphore *s);
146
147 /*
148  * read-write lock : Need to be investigated more!!
149  * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
150  *
151  * - DECLARE_RWLOCK(l)
152  * - rwlock_init(x)
153  * - read_lock(x)
154  * - read_unlock(x)
155  * - write_lock(x)
156  * - write_unlock(x)
157  */
158 typedef struct rw_semaphore rwlock_t;
159
160 #define rwlock_init(pl)         init_rwsem(pl)
161
162 #define read_lock(l)            down_read(l)
163 #define read_unlock(l)          up_read(l)
164 #define write_lock(l)           down_write(l)
165 #define write_unlock(l)         up_write(l)
166
167 static inline void
168 write_lock_irqsave(rwlock_t *l, unsigned long f) { write_lock(l); }
169 static inline void
170 write_unlock_irqrestore(rwlock_t *l, unsigned long f) { write_unlock(l); }
171
172 static inline void 
173 read_lock_irqsave(rwlock_t *l, unsigned long f) { read_lock(l); }
174 static inline void
175 read_unlock_irqrestore(rwlock_t *l, unsigned long f) { read_unlock(l); }
176
177 /*
178  * Atomic for user-space
179  * Copied from liblustre
180  */
181 typedef struct { volatile int counter; } atomic_t;
182
183 #define ATOMIC_INIT(i) { (i) }
184 #define atomic_read(a) ((a)->counter)
185 #define atomic_set(a,b) do {(a)->counter = b; } while (0)
186 #define atomic_dec_and_test(a) ((--((a)->counter)) == 0)
187 #define atomic_inc(a)  (((a)->counter)++)
188 #define atomic_dec(a)  do { (a)->counter--; } while (0)
189 #define atomic_add(b,a)  do {(a)->counter += b;} while (0)
190 #define atomic_sub(b,a)  do {(a)->counter -= b;} while (0)
191
192 #endif
193
194 #ifdef HAVE_LIBPTHREAD
195 #include <pthread.h>
196
197 /*
198  * Completion
199  */
200
201 struct cfs_completion {
202         int c_done;
203         pthread_cond_t c_cond;
204         pthread_mutex_t c_mut;
205 };
206
207 void cfs_init_completion(struct cfs_completion *c);
208 void cfs_fini_completion(struct cfs_completion *c);
209 void cfs_complete(struct cfs_completion *c);
210 void cfs_wait_for_completion(struct cfs_completion *c);
211
212 /*
213  * atomic.h
214  */
215
216 typedef struct { volatile int counter; } cfs_atomic_t;
217
218 int cfs_atomic_read(cfs_atomic_t *a);
219 void cfs_atomic_set(cfs_atomic_t *a, int b);
220 int cfs_atomic_dec_and_test(cfs_atomic_t *a);
221 void cfs_atomic_inc(cfs_atomic_t *a);
222 void cfs_atomic_dec(cfs_atomic_t *a);
223 void cfs_atomic_add(int b, cfs_atomic_t *a);
224 void cfs_atomic_sub(int b, cfs_atomic_t *a);
225
226 #endif /* HAVE_LIBPTHREAD */
227
228 /* !__KERNEL__ */
229 #endif
230
231 /* __LIBCFS_USER_LOCK_H__ */
232 #endif
233 /*
234  * Local variables:
235  * c-indentation-style: "K&R"
236  * c-basic-offset: 8
237  * tab-width: 8
238  * fill-column: 80
239  * scroll-step: 1
240  * End:
241  */