atomic.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * linux/include/asm-arm/atomic.h
  3. *
  4. * Copyright (c) 1996 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Changelog:
  11. * 27-06-1996 RMK Created
  12. * 13-04-1997 RMK Made functions atomic!
  13. * 07-12-1997 RMK Upgraded for v2.1.
  14. * 26-08-1998 PJB Added #ifdef __KERNEL__
  15. */
  16. #ifndef __ASM_ARM_ATOMIC_H
  17. #define __ASM_ARM_ATOMIC_H
  18. #include <linux/config.h>
  19. #ifdef CONFIG_SMP
  20. #error SMP not supported
  21. #endif
  22. typedef struct { volatile int counter; } atomic_t;
  23. #define ATOMIC_INIT(i) { (i) }
  24. #ifdef __KERNEL__
  25. #include <asm/proc/system.h>
  26. #define atomic_read(v) ((v)->counter)
  27. #define atomic_set(v,i) (((v)->counter) = (i))
  28. static inline void atomic_add(int i, volatile atomic_t *v)
  29. {
  30. unsigned long flags;
  31. local_irq_save(flags);
  32. v->counter += i;
  33. local_irq_restore(flags);
  34. }
  35. static inline void atomic_sub(int i, volatile atomic_t *v)
  36. {
  37. unsigned long flags;
  38. local_irq_save(flags);
  39. v->counter -= i;
  40. local_irq_restore(flags);
  41. }
  42. static inline void atomic_inc(volatile atomic_t *v)
  43. {
  44. unsigned long flags;
  45. local_irq_save(flags);
  46. v->counter += 1;
  47. local_irq_restore(flags);
  48. }
  49. static inline void atomic_dec(volatile atomic_t *v)
  50. {
  51. unsigned long flags;
  52. local_irq_save(flags);
  53. v->counter -= 1;
  54. local_irq_restore(flags);
  55. }
  56. static inline int atomic_dec_and_test(volatile atomic_t *v)
  57. {
  58. unsigned long flags;
  59. int val;
  60. local_irq_save(flags);
  61. val = v->counter;
  62. v->counter = val -= 1;
  63. local_irq_restore(flags);
  64. return val == 0;
  65. }
  66. static inline int atomic_add_negative(int i, volatile atomic_t *v)
  67. {
  68. unsigned long flags;
  69. int val;
  70. local_irq_save(flags);
  71. val = v->counter;
  72. v->counter = val += i;
  73. local_irq_restore(flags);
  74. return val < 0;
  75. }
  76. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  77. {
  78. unsigned long flags;
  79. local_irq_save(flags);
  80. *addr &= ~mask;
  81. local_irq_restore(flags);
  82. }
  83. /* Atomic operations are already serializing on ARM */
  84. #define smp_mb__before_atomic_dec() barrier()
  85. #define smp_mb__after_atomic_dec() barrier()
  86. #define smp_mb__before_atomic_inc() barrier()
  87. #define smp_mb__after_atomic_inc() barrier()
  88. #endif
  89. #endif