shim_atomic.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_atomic.h
  17. *
  18. * This file contains functions and macros for atomic operations.
  19. */
  20. #ifndef _SHIM_ATOMIC_H_
  21. #define _SHIM_ATOMIC_H_
  22. #include "shim_types.h"
  23. /* Optimization barrier */
  24. /* The "volatile" is due to gcc bugs */
  25. #define barrier() __asm__ __volatile__("": : :"memory")
  26. #ifdef __x86_64__
  27. /*
  28. * Some non-Intel clones support of order store. wmb() ceases to be a
  29. * nop for these.
  30. */
  31. # define cpu_relax() asm volatile ("rep; nop" ::: "memory")
  32. # define mb() asm volatile ("mfence" ::: "memory")
  33. # define rmb() asm volatile ("lfence" ::: "memory")
  34. # define wmb() asm volatile ("sfence" ::: "memory")
  35. #endif
  36. #define LOCK_PREFIX "\n\tlock; "
  37. #define ATOMIC_INIT(i) { (i) }
  38. static inline int atomic_read (const struct shim_atomic * v)
  39. {
  40. return (*(volatile long *)&(v)->counter);
  41. }
  42. static inline void atomic_set (struct shim_atomic * v, int i)
  43. {
  44. v->counter = i;
  45. }
  46. static inline void atomic_add (int i, struct shim_atomic * v)
  47. {
  48. asm volatile(LOCK_PREFIX "addl %1,%0"
  49. : "+m" (v->counter)
  50. : "ir" (i));
  51. }
  52. static inline void atomic_sub (int i, struct shim_atomic * v)
  53. {
  54. asm volatile(LOCK_PREFIX "subl %1,%0"
  55. : "+m" (v->counter)
  56. : "ir" (i));
  57. }
  58. static inline int atomic_sub_and_test (int i, struct shim_atomic * v)
  59. {
  60. unsigned char c;
  61. asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
  62. : "+m" (v->counter), "=qm" (c)
  63. : "ir" (i) : "memory");
  64. return c;
  65. }
  66. static inline void atomic_inc (struct shim_atomic * v)
  67. {
  68. asm volatile(LOCK_PREFIX "incl %0"
  69. : "+m" (v->counter));
  70. }
  71. static inline int atomic_inc_and_test (struct shim_atomic * v)
  72. {
  73. unsigned char c;
  74. asm volatile(LOCK_PREFIX "incl %0; sete %1"
  75. : "+m" (v->counter), "=qm" (c)
  76. : : "memory");
  77. return c != 0;
  78. }
  79. static inline void atomic_dec (struct shim_atomic * v)
  80. {
  81. asm volatile(LOCK_PREFIX "decl %0"
  82. : "+m" (v->counter));
  83. }
  84. static inline int atomic_dec_and_test (struct shim_atomic * v)
  85. {
  86. unsigned char c;
  87. asm volatile(LOCK_PREFIX "decl %0; sete %1"
  88. : "+m" (v->counter), "=qm" (c)
  89. : : "memory");
  90. return c != 0;
  91. }
  92. #undef LOCK_PREFIX
  93. #ifndef __i386__
  94. # include "cmpxchg_64.h"
  95. #else
  96. # include "cmpxchg_32.h"
  97. #endif
  98. static inline int atomic_cmpxchg (struct shim_atomic * v, int old, int new)
  99. {
  100. return cmpxchg(&v->counter, old, new);
  101. }
  102. static inline int atomic_xchg (struct shim_atomic * v, int new)
  103. {
  104. return xchg(&v->counter, new);
  105. }
  106. #endif /* _SHIM_ATOMIC_H_ */