2 * include/asm-xtensa/rwsem.h
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Largely copied from include/asm-ppc/rwsem.h
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
13 #ifndef _XTENSA_RWSEM_H
14 #define _XTENSA_RWSEM_H
16 #ifndef _LINUX_RWSEM_H
17 #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
20 #define RWSEM_UNLOCKED_VALUE 0x00000000
21 #define RWSEM_ACTIVE_BIAS 0x00000001
22 #define RWSEM_ACTIVE_MASK 0x0000ffff
23 #define RWSEM_WAITING_BIAS (-0x00010000)
24 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
25 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
30 static inline void __down_read(struct rw_semaphore
*sem
)
32 if (atomic_add_return(1,(atomic_t
*)(&sem
->count
)) > 0)
35 rwsem_down_read_failed(sem
);
38 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
42 while ((tmp
= sem
->count
) >= 0) {
43 if (tmp
== cmpxchg(&sem
->count
, tmp
,
44 tmp
+ RWSEM_ACTIVE_READ_BIAS
)) {
55 static inline void __down_write(struct rw_semaphore
*sem
)
59 tmp
= atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS
,
60 (atomic_t
*)(&sem
->count
));
61 if (tmp
== RWSEM_ACTIVE_WRITE_BIAS
)
64 rwsem_down_write_failed(sem
);
67 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
71 tmp
= cmpxchg(&sem
->count
, RWSEM_UNLOCKED_VALUE
,
72 RWSEM_ACTIVE_WRITE_BIAS
);
74 return tmp
== RWSEM_UNLOCKED_VALUE
;
78 * unlock after reading
80 static inline void __up_read(struct rw_semaphore
*sem
)
85 tmp
= atomic_sub_return(1,(atomic_t
*)(&sem
->count
));
86 if (tmp
< -1 && (tmp
& RWSEM_ACTIVE_MASK
) == 0)
91 * unlock after writing
93 static inline void __up_write(struct rw_semaphore
*sem
)
96 if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS
,
97 (atomic_t
*)(&sem
->count
)) < 0)
102 * implement atomic add functionality
104 static inline void rwsem_atomic_add(int delta
, struct rw_semaphore
*sem
)
106 atomic_add(delta
, (atomic_t
*)(&sem
->count
));
110 * downgrade write lock to read lock
112 static inline void __downgrade_write(struct rw_semaphore
*sem
)
117 tmp
= atomic_add_return(-RWSEM_WAITING_BIAS
, (atomic_t
*)(&sem
->count
));
119 rwsem_downgrade_wake(sem
);
123 * implement exchange and add functionality
125 static inline int rwsem_atomic_update(int delta
, struct rw_semaphore
*sem
)
128 return atomic_add_return(delta
, (atomic_t
*)(&sem
->count
));
131 #endif /* _XTENSA_RWSEM_H */