2005-04-16 22:20:36 +00:00
|
|
|
/* rwsem.h: R/W semaphores, public interface
|
|
|
|
*
|
|
|
|
* Written by David Howells (dhowells@redhat.com).
|
|
|
|
* Derived from asm-i386/semaphore.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_RWSEM_H
|
|
|
|
#define _LINUX_RWSEM_H
|
|
|
|
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
2011-01-26 20:05:50 +00:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
2011-07-26 23:09:06 +00:00
|
|
|
#include <linux/atomic.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
struct rw_semaphore;
|
|
|
|
|
|
|
|
#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
|
|
|
|
#include <linux/rwsem-spinlock.h> /* use a generic implementation */
|
|
|
|
#else
|
2011-01-26 20:05:56 +00:00
|
|
|
/* All arch specific implementations share the same struct */
|
|
|
|
struct rw_semaphore {
|
|
|
|
long count;
|
2010-02-24 08:54:54 +00:00
|
|
|
raw_spinlock_t wait_lock;
|
2011-01-26 20:05:56 +00:00
|
|
|
struct list_head wait_list;
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
struct lockdep_map dep_map;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-01-26 20:32:01 +00:00
|
|
|
extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
|
|
|
|
extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
|
|
|
|
extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
|
|
|
|
extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
|
2011-01-26 20:06:06 +00:00
|
|
|
|
2011-01-26 20:05:56 +00:00
|
|
|
/* Include the arch specific part */
|
|
|
|
#include <asm/rwsem.h>
|
2011-01-26 20:06:03 +00:00
|
|
|
|
|
|
|
/* In all implementations count != 0 means locked */
|
|
|
|
static inline int rwsem_is_locked(struct rw_semaphore *sem)
|
|
|
|
{
|
|
|
|
return sem->count != 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-26 20:06:00 +00:00
|
|
|
/* Common initializer macros and functions */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
|
|
|
|
#else
|
|
|
|
# define __RWSEM_DEP_MAP_INIT(lockname)
|
|
|
|
#endif
|
|
|
|
|
2010-02-24 08:54:54 +00:00
|
|
|
#define __RWSEM_INITIALIZER(name) \
|
|
|
|
{ RWSEM_UNLOCKED_VALUE, \
|
|
|
|
__RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
|
|
|
|
LIST_HEAD_INIT((name).wait_list) \
|
|
|
|
__RWSEM_DEP_MAP_INIT(name) }
|
2011-01-26 20:06:00 +00:00
|
|
|
|
|
|
|
#define DECLARE_RWSEM(name) \
|
|
|
|
struct rw_semaphore name = __RWSEM_INITIALIZER(name)
|
|
|
|
|
|
|
|
extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
|
|
|
|
struct lock_class_key *key);
|
|
|
|
|
|
|
|
#define init_rwsem(sem) \
|
|
|
|
do { \
|
|
|
|
static struct lock_class_key __key; \
|
|
|
|
\
|
|
|
|
__init_rwsem((sem), #sem, &__key); \
|
|
|
|
} while (0)
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* lock for reading
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern void down_read(struct rw_semaphore *sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* trylock for reading -- returns 1 if successful, 0 if contention
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern int down_read_trylock(struct rw_semaphore *sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lock for writing
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern void down_write(struct rw_semaphore *sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* trylock for writing -- returns 1 if successful, 0 if contention
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern int down_write_trylock(struct rw_semaphore *sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* release a read lock
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern void up_read(struct rw_semaphore *sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* release a write lock
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern void up_write(struct rw_semaphore *sem);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* downgrade write lock to read lock
|
|
|
|
*/
|
2006-07-03 07:24:53 +00:00
|
|
|
extern void downgrade_write(struct rw_semaphore *sem);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
/*
|
2006-07-10 11:44:02 +00:00
|
|
|
* nested locking. NOTE: rwsems are not allowed to recurse
|
|
|
|
* (which occurs if the same task tries to acquire the same
|
|
|
|
* lock instance multiple times), but multiple locks of the
|
|
|
|
* same lock class might be taken, if the order of the locks
|
|
|
|
* is always the same. This ordering rule can be expressed
|
|
|
|
* to lockdep via the _nested() APIs, but enumerating the
|
|
|
|
* subclasses that are used. (If the nesting relationship is
|
|
|
|
* static then another method for expressing nested locking is
|
|
|
|
* the explicit definition of lock class keys and the use of
|
|
|
|
* lockdep_set_class() at lock initialization time.
|
|
|
|
* See Documentation/lockdep-design.txt for more details.)
|
2006-07-03 07:24:53 +00:00
|
|
|
*/
|
|
|
|
extern void down_read_nested(struct rw_semaphore *sem, int subclass);
|
|
|
|
extern void down_write_nested(struct rw_semaphore *sem, int subclass);
|
|
|
|
#else
|
|
|
|
# define down_read_nested(sem, subclass) down_read(sem)
|
|
|
|
# define down_write_nested(sem, subclass) down_write(sem)
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#endif /* _LINUX_RWSEM_H */
|