atomic.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* atomic.h: These still suck, but the I-cache hit rate is higher.
  2.  *
  3.  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  4.  * Copyright (C) 2000 Anton Blanchard (anton@linuxcare.com.au)
  5.  */
  6. #ifndef __ARCH_SPARC_ATOMIC__
  7. #define __ARCH_SPARC_ATOMIC__
  8. #include <linux/config.h>
  9. typedef struct { volatile int counter; } atomic_t;
  10. #ifdef __KERNEL__
  11. #ifndef CONFIG_SMP
  12. #define ATOMIC_INIT(i)  { (i) }
  13. #define atomic_read(v)          ((v)->counter)
  14. #define atomic_set(v, i)        (((v)->counter) = i)
  15. #else
  16. /* We do the bulk of the actual work out of line in two common
  17.  * routines in assembler, see arch/sparc/lib/atomic.S for the
  18.  * "fun" details.
  19.  *
  20.  * For SMP the trick is you embed the spin lock byte within
  21.  * the word, use the low byte so signedness is easily retained
  22.  * via a quick arithmetic shift.  It looks like this:
  23.  *
  24.  * ----------------------------------------
  25.  * | signed 24-bit counter value |  lock  |  atomic_t
  26.  * ----------------------------------------
  27.  *  31                          8 7      0
  28.  */
  29. #define ATOMIC_INIT(i) { (i << 8) }
  30. static __inline__ int atomic_read(atomic_t *v)
  31. {
  32. int ret = v->counter;
  33. while(ret & 0xff)
  34. ret = v->counter;
  35. return ret >> 8;
  36. }
  37. #define atomic_set(v, i) (((v)->counter) = ((i) << 8))
  38. #endif
  39. static __inline__ int __atomic_add(int i, atomic_t *v)
  40. {
  41. register volatile int *ptr asm("g1");
  42. register int increment asm("g2");
  43. ptr = &v->counter;
  44. increment = i;
  45. __asm__ __volatile__(
  46. "mov %%o7, %%g4nt"
  47. "call ___atomic_addnt"
  48. " add %%o7, 8, %%o7n"
  49. : "=&r" (increment)
  50. : "0" (increment), "r" (ptr)
  51. : "g3", "g4", "g7", "memory", "cc");
  52. return increment;
  53. }
  54. static __inline__ int __atomic_sub(int i, atomic_t *v)
  55. {
  56. register volatile int *ptr asm("g1");
  57. register int increment asm("g2");
  58. ptr = &v->counter;
  59. increment = i;
  60. __asm__ __volatile__(
  61. "mov %%o7, %%g4nt"
  62. "call ___atomic_subnt"
  63. " add %%o7, 8, %%o7n"
  64. : "=&r" (increment)
  65. : "0" (increment), "r" (ptr)
  66. : "g3", "g4", "g7", "memory", "cc");
  67. return increment;
  68. }
  69. #define atomic_add(i, v) ((void)__atomic_add((i), (v)))
  70. #define atomic_sub(i, v) ((void)__atomic_sub((i), (v)))
  71. #define atomic_dec_return(v) __atomic_sub(1, (v))
  72. #define atomic_inc_return(v) __atomic_add(1, (v))
  73. #define atomic_sub_and_test(i, v) (__atomic_sub((i), (v)) == 0)
  74. #define atomic_dec_and_test(v) (__atomic_sub(1, (v)) == 0)
  75. #define atomic_inc(v) ((void)__atomic_add(1, (v)))
  76. #define atomic_dec(v) ((void)__atomic_sub(1, (v)))
  77. #define atomic_add_negative(i, v) (__atomic_add((i), (v)) < 0)
  78. /* Atomic operations are already serializing */
  79. #define smp_mb__before_atomic_dec() barrier()
  80. #define smp_mb__after_atomic_dec() barrier()
  81. #define smp_mb__before_atomic_inc() barrier()
  82. #define smp_mb__after_atomic_inc() barrier()
  83. #endif /* !(__KERNEL__) */
  84. #endif /* !(__ARCH_SPARC_ATOMIC__) */