OSDN Git Service

8f43ba234787592af487b82503187ab413a888d4
[uclinux-h8/linux.git] / kernel / locking / rwsem.h
1 /*
2  * The owner field of the rw_semaphore structure will be set to
3  * RWSEM_READ_OWNED when a reader grabs the lock. A writer will clear
4  * the owner field when it unlocks. A reader, on the other hand, will
5  * not touch the owner field when it unlocks.
6  *
7  * In essence, the owner field now has the following 3 states:
8  *  1) 0
9  *     - lock is free or the owner hasn't set the field yet
10  *  2) RWSEM_READER_OWNED
11  *     - lock is currently or previously owned by readers (lock is free
12  *       or not set by owner yet)
13  *  3) Other non-zero value
14  *     - a writer owns the lock
15  */
16 #define RWSEM_READER_OWNED      ((struct task_struct *)1UL)
17
18 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
19 static inline void rwsem_set_owner(struct rw_semaphore *sem)
20 {
21         sem->owner = current;
22 }
23
24 static inline void rwsem_clear_owner(struct rw_semaphore *sem)
25 {
26         sem->owner = NULL;
27 }
28
29 static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
30 {
31         /*
32          * We check the owner value first to make sure that we will only
33          * do a write to the rwsem cacheline when it is really necessary
34          * to minimize cacheline contention.
35          */
36         if (sem->owner != RWSEM_READER_OWNED)
37                 sem->owner = RWSEM_READER_OWNED;
38 }
39
40 static inline bool rwsem_owner_is_writer(struct task_struct *owner)
41 {
42         return owner && owner != RWSEM_READER_OWNED;
43 }
44
45 static inline bool rwsem_owner_is_reader(struct task_struct *owner)
46 {
47         return owner == RWSEM_READER_OWNED;
48 }
49 #else
50 static inline void rwsem_set_owner(struct rw_semaphore *sem)
51 {
52 }
53
54 static inline void rwsem_clear_owner(struct rw_semaphore *sem)
55 {
56 }
57
58 static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
59 {
60 }
61 #endif