| 
									
										
										
										
											2005-04-16 15:20:36 -07: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 16:09:06 -07:00
										 |  |  | #include <linux/atomic.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07: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 09:54:54 +01: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 21:32:01 +01: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 15:20:36 -07: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 09:54:54 +01: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 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * lock for reading | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern void down_read(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * trylock for reading -- returns 1 if successful, 0 if contention | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern int down_read_trylock(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * lock for writing | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern void down_write(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * trylock for writing -- returns 1 if successful, 0 if contention | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern int down_write_trylock(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * release a read lock | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern void up_read(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * release a write lock | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern void up_write(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * downgrade write lock to read lock | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | extern void downgrade_write(struct rw_semaphore *sem); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_DEBUG_LOCK_ALLOC
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2006-07-10 04:44:02 -07: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 00:24:53 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | extern void down_read_nested(struct rw_semaphore *sem, int subclass); | 
					
						
							|  |  |  | extern void down_write_nested(struct rw_semaphore *sem, int subclass); | 
					
						
							| 
									
										
										
										
											2013-01-11 14:31:56 -08:00
										 |  |  | extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # define down_write_nest_lock(sem, nest_lock)			\
 | 
					
						
							|  |  |  | do {								\ | 
					
						
							|  |  |  | 	typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\ | 
					
						
							|  |  |  | 	_down_write_nest_lock(sem, &(nest_lock)->dep_map);	\ | 
					
						
							|  |  |  | } while (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-21 21:43:05 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Take/release a lock when not the owner will release it. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * [ This API should be avoided as much as possible - the | 
					
						
							|  |  |  |  *   proper abstraction for this case is completions. ] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | extern void down_read_non_owner(struct rw_semaphore *sem); | 
					
						
							|  |  |  | extern void up_read_non_owner(struct rw_semaphore *sem); | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | #else
 | 
					
						
							|  |  |  | # define down_read_nested(sem, subclass)		down_read(sem)
 | 
					
						
							| 
									
										
										
										
											2013-01-15 20:12:37 +01:00
										 |  |  | # define down_write_nest_lock(sem, nest_lock)	down_write(sem)
 | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | # define down_write_nested(sem, subclass)	down_write(sem)
 | 
					
						
							| 
									
										
										
										
											2011-09-21 21:43:05 -07:00
										 |  |  | # define down_read_non_owner(sem)		down_read(sem)
 | 
					
						
							|  |  |  | # define up_read_non_owner(sem)			up_read(sem)
 | 
					
						
							| 
									
										
										
										
											2006-07-03 00:24:53 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* _LINUX_RWSEM_H */
 |