Whamcloud - gitweb
land portals part of b_hd_sec on HEAD.
[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
39 #ifndef __KERNEL__
40
41 /*
42  * Optional debugging (magic stamping and checking ownership) can be added.
43  */
44
45 /*
46  * spin_lock
47  *
48  * - spin_lock_init(x)
49  * - spin_lock(x)
50  * - spin_unlock(x)
51  * - spin_trylock(x)
52  *
53  * - spin_lock_irqsave(x, f)
54  * - spin_unlock_irqrestore(x, f)
55  *
56  * No-op implementation.
57  */
58 struct spin_lock {};
59
60 typedef struct spin_lock spinlock_t;
61
62 void spin_lock_init(spinlock_t *lock);
63 void spin_lock(spinlock_t *lock);
64 void spin_unlock(spinlock_t *lock);
65 int spin_trylock(spinlock_t *lock);
66 void spin_lock_bh_init(spinlock_t *lock);
67 void spin_lock_bh(spinlock_t *lock);
68 void spin_unlock_bh(spinlock_t *lock);
69
70 #define spin_lock_irqsave(l, flags) ({ spin_lock(l); (void)flags; })
71 #define spin_unlock_irqrestore(l, flags)  ({ spin_unlock(l); (void)flags; })
72
73 /*
74  * Semaphore
75  *
76  * - sema_init(x, v)
77  * - __down(x)
78  * - __up(x)
79  */
80 struct semaphore {};
81
82 void sema_init(struct semaphore *s, int val);
83 void __down(struct semaphore *s);
84 void __up(struct semaphore *s);
85
86 /*
87  * Mutex:
88  *
89  * - init_mutex(x)
90  * - init_mutex_locked(x)
91  * - mutex_up(x)
92  * - mutex_down(x)
93  */
94 #define mutex_up(s)                     __up(s)
95 #define mutex_down(s)                   __down(s)
96
97 #define init_mutex(x)                   sema_init(x, 1)
98 #define init_mutex_locked(x)            sema_init(x, 0)
99
100 /*
101  * Completion:
102  *
103  * - init_completion(c)
104  * - complete(c)
105  * - wait_for_completion(c)
106  */
107 struct completion {};
108
109 void init_completion(struct completion *c);
110 void complete(struct completion *c);
111 void wait_for_completion(struct completion *c);
112
113 /*
114  * rw_semaphore:
115  *
116  * - init_rwsem(x)
117  * - down_read(x)
118  * - up_read(x)
119  * - down_write(x)
120  * - up_write(x)
121  */
122 struct rw_semaphore {};
123
124 void init_rwsem(struct rw_semaphore *s);
125 void down_read(struct rw_semaphore *s);
126 int down_read_trylock(struct rw_semaphore *s);
127 void down_write(struct rw_semaphore *s);
128 int down_write_trylock(struct rw_semaphore *s);
129 void up_read(struct rw_semaphore *s);
130 void up_write(struct rw_semaphore *s);
131
132 /*
133  * read-write lock : Need to be investigated more!!
134  * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
135  *
136  * - DECLARE_RWLOCK(l)
137  * - rwlock_init(x)
138  * - read_lock(x)
139  * - read_unlock(x)
140  * - write_lock(x)
141  * - write_unlock(x)
142  */
143 typedef struct rw_semaphore rwlock_t;
144
145 #define rwlock_init(pl)         init_rwsem(pl)
146
147 #define read_lock(l)            down_read(l)
148 #define read_unlock(l)          up_read(l)
149 #define write_lock(l)           down_write(l)
150 #define write_unlock(l)         up_write(l)
151
152 #define write_lock_irqsave(l, f)        write_lock(l)
153 #define write_unlock_irqrestore(l, f)   write_unlock(l)
154
155 #define read_lock_irqsave(l, f)         read_lock(l)
156 #define read_unlock_irqrestore(l, f)    read_unlock(l)
157
158 /* !__KERNEL__ */
159 #endif
160
161 /* __LIBCFS_USER_LOCK_H__ */
162 #endif
163 /*
164  * Local variables:
165  * c-indentation-style: "K&R"
166  * c-basic-offset: 8
167  * tab-width: 8
168  * fill-column: 80
169  * scroll-step: 1
170  * End:
171  */