os_kernel_lab/code/lab8/libs/atomic.h

81 lines
2.5 KiB
C
Raw Normal View History

2012-08-22 12:32:13 +08:00
#ifndef __LIBS_ATOMIC_H__
#define __LIBS_ATOMIC_H__
/* Atomic operations that C can't guarantee us. Useful for resource counting etc.. */
static inline void set_bit(int nr, volatile void *addr) __attribute__((always_inline));
static inline void clear_bit(int nr, volatile void *addr) __attribute__((always_inline));
static inline void change_bit(int nr, volatile void *addr) __attribute__((always_inline));
static inline bool test_bit(int nr, volatile void *addr) __attribute__((always_inline));
/* *
* set_bit - Atomically set a bit in memory
* @nr: the bit to set
* @addr: the address to start counting from
*
* Note that @nr may be almost arbitrarily large; this function is not
* restricted to acting on a single-word quantity.
* */
static inline void
set_bit(int nr, volatile void *addr) {
asm volatile ("btsl %1, %0" :"=m" (*(volatile long *)addr) : "Ir" (nr));
}
/* *
* clear_bit - Atomically clears a bit in memory
* @nr: the bit to clear
* @addr: the address to start counting from
* */
static inline void
clear_bit(int nr, volatile void *addr) {
asm volatile ("btrl %1, %0" :"=m" (*(volatile long *)addr) : "Ir" (nr));
}
/* *
* change_bit - Atomically toggle a bit in memory
* @nr: the bit to change
* @addr: the address to start counting from
* */
static inline void
change_bit(int nr, volatile void *addr) {
asm volatile ("btcl %1, %0" :"=m" (*(volatile long *)addr) : "Ir" (nr));
}
/* *
2012-09-14 21:36:57 +08:00
* test_bit - Determine whether a bit is set
* @nr: the bit to test
2012-08-22 12:32:13 +08:00
* @addr: the address to count from
* */
static inline bool
2012-09-14 21:36:57 +08:00
test_bit(int nr, volatile void *addr) {
2012-08-22 12:32:13 +08:00
int oldbit;
2012-09-14 21:36:57 +08:00
asm volatile ("btl %2, %1; sbbl %0,%0" : "=r" (oldbit) : "m" (*(volatile long *)addr), "Ir" (nr));
2012-08-22 12:32:13 +08:00
return oldbit != 0;
}
/* *
2012-09-14 21:36:57 +08:00
* test_and_set_bit - Atomically set a bit and return its old value
* @nr: the bit to set
2012-08-22 12:32:13 +08:00
* @addr: the address to count from
* */
static inline bool
2012-09-14 21:36:57 +08:00
test_and_set_bit(int nr, volatile void *addr) {
2012-08-22 12:32:13 +08:00
int oldbit;
2012-09-14 21:36:57 +08:00
asm volatile ("btsl %2, %1; sbbl %0, %0" : "=r" (oldbit), "=m" (*(volatile long *)addr) : "Ir" (nr) : "memory");
2012-08-22 12:32:13 +08:00
return oldbit != 0;
}
/* *
2012-09-14 21:36:57 +08:00
* test_and_clear_bit - Atomically clear a bit and return its old value
* @nr: the bit to clear
2012-08-22 12:32:13 +08:00
* @addr: the address to count from
* */
static inline bool
2012-09-14 21:36:57 +08:00
test_and_clear_bit(int nr, volatile void *addr) {
2012-08-22 12:32:13 +08:00
int oldbit;
2012-09-14 21:36:57 +08:00
asm volatile ("btrl %2, %1; sbbl %0, %0" : "=r" (oldbit), "=m" (*(volatile long *)addr) : "Ir" (nr) : "memory");
2012-08-22 12:32:13 +08:00
return oldbit != 0;
}
#endif /* !__LIBS_ATOMIC_H__ */