blob: 4568ee4022df0f957fb6f42dac4e9390d579a4fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: rwsem.h,v 1.5 2001/11/18 00:12:56 davem Exp $
2 * rwsem.h: R/W semaphores implemented using CAS
3 *
4 * Written by David S. Miller (davem@redhat.com), 2001.
5 * Derived from asm-i386/rwsem.h
6 */
7#ifndef _SPARC64_RWSEM_H
8#define _SPARC64_RWSEM_H
9
10#ifndef _LINUX_RWSEM_H
11#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
12#endif
13
14#ifdef __KERNEL__
15
16#include <linux/list.h>
17#include <linux/spinlock.h>
18#include <asm/rwsem-const.h>
19
20struct rwsem_waiter;
21
22struct rw_semaphore {
23 signed int count;
24 spinlock_t wait_lock;
25 struct list_head wait_list;
26};
27
28#define __RWSEM_INITIALIZER(name) \
29{ RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) }
30
31#define DECLARE_RWSEM(name) \
32 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
33
34static __inline__ void init_rwsem(struct rw_semaphore *sem)
35{
36 sem->count = RWSEM_UNLOCKED_VALUE;
37 spin_lock_init(&sem->wait_lock);
38 INIT_LIST_HEAD(&sem->wait_list);
39}
40
41extern void __down_read(struct rw_semaphore *sem);
42extern int __down_read_trylock(struct rw_semaphore *sem);
43extern void __down_write(struct rw_semaphore *sem);
44extern int __down_write_trylock(struct rw_semaphore *sem);
45extern void __up_read(struct rw_semaphore *sem);
46extern void __up_write(struct rw_semaphore *sem);
47extern void __downgrade_write(struct rw_semaphore *sem);
48
David S. Miller620de542005-07-24 19:35:42 -070049static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
David S. Miller620de542005-07-24 19:35:42 -070051 return atomic_add_return(delta, (atomic_t *)(&sem->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
David S. Miller620de542005-07-24 19:35:42 -070054static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
David S. Miller620de542005-07-24 19:35:42 -070056 atomic_add(delta, (atomic_t *)(&sem->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59#endif /* __KERNEL__ */
60
61#endif /* _SPARC64_RWSEM_H */