《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB

10 years ago
  1. #ifndef __LIBS_ATOMIC_H__
  2. #define __LIBS_ATOMIC_H__
  3. /* Atomic operations that C can't guarantee us. Useful for resource counting etc.. */
  4. static inline void set_bit(int nr, volatile void *addr) __attribute__((always_inline));
  5. static inline void clear_bit(int nr, volatile void *addr) __attribute__((always_inline));
  6. static inline void change_bit(int nr, volatile void *addr) __attribute__((always_inline));
  7. static inline bool test_and_set_bit(int nr, volatile void *addr) __attribute__((always_inline));
  8. static inline bool test_and_clear_bit(int nr, volatile void *addr) __attribute__((always_inline));
  9. static inline bool test_bit(int nr, volatile void *addr) __attribute__((always_inline));
  10. /* *
  11. * set_bit - Atomically set a bit in memory
  12. * @nr: the bit to set
  13. * @addr: the address to start counting from
  14. *
  15. * Note that @nr may be almost arbitrarily large; this function is not
  16. * restricted to acting on a single-word quantity.
  17. * */
  18. static inline void
  19. set_bit(int nr, volatile void *addr) {
  20. asm volatile ("btsl %1, %0" :"=m" (*(volatile long *)addr) : "Ir" (nr));
  21. }
  22. /* *
  23. * clear_bit - Atomically clears a bit in memory
  24. * @nr: the bit to clear
  25. * @addr: the address to start counting from
  26. * */
  27. static inline void
  28. clear_bit(int nr, volatile void *addr) {
  29. asm volatile ("btrl %1, %0" :"=m" (*(volatile long *)addr) : "Ir" (nr));
  30. }
  31. /* *
  32. * change_bit - Atomically toggle a bit in memory
  33. * @nr: the bit to change
  34. * @addr: the address to start counting from
  35. * */
  36. static inline void
  37. change_bit(int nr, volatile void *addr) {
  38. asm volatile ("btcl %1, %0" :"=m" (*(volatile long *)addr) : "Ir" (nr));
  39. }
  40. /* *
  41. * test_bit - Determine whether a bit is set
  42. * @nr: the bit to test
  43. * @addr: the address to count from
  44. * */
  45. static inline bool
  46. test_bit(int nr, volatile void *addr) {
  47. int oldbit;
  48. asm volatile ("btl %2, %1; sbbl %0,%0" : "=r" (oldbit) : "m" (*(volatile long *)addr), "Ir" (nr));
  49. return oldbit != 0;
  50. }
  51. /* *
  52. * test_and_set_bit - Atomically set a bit and return its old value
  53. * @nr: the bit to set
  54. * @addr: the address to count from
  55. * */
  56. static inline bool
  57. test_and_set_bit(int nr, volatile void *addr) {
  58. int oldbit;
  59. asm volatile ("btsl %2, %1; sbbl %0, %0" : "=r" (oldbit), "=m" (*(volatile long *)addr) : "Ir" (nr) : "memory");
  60. return oldbit != 0;
  61. }
  62. /* *
  63. * test_and_clear_bit - Atomically clear a bit and return its old value
  64. * @nr: the bit to clear
  65. * @addr: the address to count from
  66. * */
  67. static inline bool
  68. test_and_clear_bit(int nr, volatile void *addr) {
  69. int oldbit;
  70. asm volatile ("btrl %2, %1; sbbl %0, %0" : "=r" (oldbit), "=m" (*(volatile long *)addr) : "Ir" (nr) : "memory");
  71. return oldbit != 0;
  72. }
  73. #endif /* !__LIBS_ATOMIC_H__ */