1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Nikita Danilov <nikita@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
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.
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
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.
22 * Implementation of portable time API for user-level.
26 #ifndef __LIBCFS_USER_LOCK_H__
27 #define __LIBCFS_USER_LOCK_H__
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
33 /* Implementations of portable synchronization APIs for liblustre */
36 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
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
49 * Optional debugging (magic stamping and checking ownership) can be added.
60 * - spin_lock_irqsave(x, f)
61 * - spin_unlock_irqrestore(x, f)
63 * No-op implementation.
65 struct spin_lock {int foo;};
67 typedef struct spin_lock spinlock_t;
69 #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
70 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
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;}
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){}
91 typedef struct semaphore {
95 void sema_init(struct semaphore *s, int val);
96 void __down(struct semaphore *s);
97 void __up(struct semaphore *s);
103 * - init_mutex_locked(x)
107 #define mutex_up(s) __up(s)
108 #define mutex_down(s) __down(s)
110 #define init_mutex(x) sema_init(x, 1)
111 #define init_mutex_locked(x) sema_init(x, 0)
116 * - init_completion(c)
118 * - wait_for_completion(c)
121 struct completion {};
123 void init_completion(struct completion *c);
124 void complete(struct completion *c);
125 void wait_for_completion(struct completion *c);
137 struct rw_semaphore {};
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);
148 * read-write lock : Need to be investigated more!!
149 * XXX nikita: for now, let rwlock_t to be identical to rw_semaphore
151 * - DECLARE_RWLOCK(l)
158 typedef struct rw_semaphore rwlock_t;
160 #define rwlock_init(pl) init_rwsem(pl)
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)
168 write_lock_irqsave(rwlock_t *l, unsigned long f) { write_lock(l); }
170 write_unlock_irqrestore(rwlock_t *l, unsigned long f) { write_unlock(l); }
173 read_lock_irqsave(rwlock_t *l, unsigned long f) { read_lock(l); }
175 read_unlock_irqrestore(rwlock_t *l, unsigned long f) { read_unlock(l); }
178 * Atomic for user-space
179 * Copied from liblustre
181 typedef struct { volatile int counter; } atomic_t;
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)
197 /* __LIBCFS_USER_LOCK_H__ */
201 * c-indentation-style: "K&R"