latomic_i.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2018 Agustina Arzille.
  3. * Copyright (c) 2018 Richard Braun.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. *
  19. * Architecture-specific code may override any of the type-specific
  20. * functions by defining a macro of the same name.
  21. */
  22. #ifndef KERN_LATOMIC_I_H
  23. #define KERN_LATOMIC_I_H
  24. #include <stdbool.h>
  25. #include <stdint.h>
  26. #include <kern/atomic_types.h>
  27. #include <kern/macros.h>
  28. #include <machine/latomic.h>
  29. #define LATOMIC_ALIGN(ptr) MIN(sizeof(*(ptr)), sizeof(ptr))
  30. #define latomic_ptr_aligned(ptr) P2ALIGNED((uintptr_t)(ptr), LATOMIC_ALIGN(ptr))
  31. /* See atomic_select */
  32. #define latomic_select(ptr, op) \
  33. _Generic(*(ptr), \
  34. float: latomic_invalid_type, \
  35. double: latomic_invalid_type, \
  36. long double: latomic_invalid_type, \
  37. bool: latomic_invalid_type, \
  38. char: latomic_invalid_type, \
  39. signed char: latomic_invalid_type, \
  40. unsigned char: latomic_invalid_type, \
  41. short: latomic_invalid_type, \
  42. unsigned short: latomic_invalid_type, \
  43. int: latomic_ ## op ## _32, \
  44. unsigned int: latomic_ ## op ## _32, \
  45. long: latomic_ ## op ## _ul, \
  46. unsigned long: latomic_ ## op ## _ul, \
  47. long long: latomic_ ## op ## _64, \
  48. unsigned long long: latomic_ ## op ## _64, \
  49. default: latomic_ ## op ## _ptr)
  50. void latomic_invalid_type(void);
  51. /* latomic_load */
  52. #ifndef latomic_load_32
  53. unsigned int latomic_load_32(union atomic_constptr_32 ptr, int memorder);
  54. #endif /* latomic_load_32 */
  55. #ifndef latomic_load_64
  56. #ifdef __LP64__
  57. unsigned long long latomic_load_64(union atomic_constptr_64 ptr, int memorder);
  58. #else /* __LP64__ */
  59. unsigned long long latomic_load_64(union atomic_constptr_64 ptr, int memorder);
  60. #endif /* __LP64__ */
  61. #endif /* latomic_load_64 */
  62. #ifdef __LP64__
  63. #define latomic_load_ul latomic_load_64
  64. #else /* __LP64__ */
  65. #define latomic_load_ul latomic_load_32
  66. #endif /* __LP64__ */
  67. #define latomic_load_ptr latomic_load_ul
  68. /* latomic_store */
  69. #ifndef latomic_store_32
  70. void latomic_store_32(union atomic_ptr_32 ptr, union atomic_val_32 val,
  71. int memorder);
  72. #endif /* latomic_store_32 */
  73. #ifndef latomic_store_64
  74. #ifdef __LP64__
  75. void latomic_store_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  76. int memorder);
  77. #else /* __LP64__ */
  78. void latomic_store_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  79. int memorder);
  80. #endif /* __LP64__ */
  81. #endif /* latomic_store_64 */
  82. #ifdef __LP64__
  83. #define latomic_store_ul latomic_store_64
  84. #else /* __LP64__ */
  85. #define latomic_store_ul latomic_store_32
  86. #endif /* __LP64__ */
  87. #define latomic_store_ptr latomic_store_ul
  88. /* latomic_swap */
  89. #ifndef latomic_swap_32
  90. unsigned int latomic_swap_32(union atomic_ptr_32 ptr,
  91. union atomic_val_32 val, int memorder);
  92. #endif /* latomic_swap_32 */
  93. #ifndef latomic_swap_64
  94. unsigned long long latomic_swap_64(union atomic_ptr_64 ptr,
  95. union atomic_val_64 val, int memorder);
  96. #endif /* latomic_swap_64 */
  97. #ifdef __LP64__
  98. #define latomic_swap_ul latomic_swap_64
  99. #else /* __LP64__ */
  100. #define latomic_swap_ul latomic_swap_32
  101. #endif /* __LP64__ */
  102. #define latomic_swap_ptr latomic_swap_ul
  103. /* latomic_cas */
  104. #ifndef latomic_cas_32
  105. unsigned int latomic_cas_32(union atomic_ptr_32 ptr, union atomic_val_32 oval,
  106. union atomic_val_32 nval, int memorder);
  107. #endif /* latomic_cas_32 */
  108. #ifndef latomic_cas_64
  109. unsigned long long latomic_cas_64(union atomic_ptr_64 ptr,
  110. union atomic_val_64 oval,
  111. union atomic_val_64 nval, int memorder);
  112. #endif /* latomic_cas_64 */
  113. #ifdef __LP64__
  114. #define latomic_cas_ul latomic_cas_64
  115. #else /* __LP64__ */
  116. #define latomic_cas_ul latomic_cas_32
  117. #endif /* __LP64__ */
  118. #define latomic_cas_ptr latomic_cas_ul
  119. /* latomic_fetch_add */
  120. #ifndef latomic_fetch_add_32
  121. unsigned int latomic_fetch_add_32(union atomic_ptr_32 ptr,
  122. union atomic_val_32 val, int memorder);
  123. #endif /* latomic_fetch_add_32 */
  124. #ifndef latomic_fetch_add_64
  125. unsigned long long latomic_fetch_add_64(union atomic_ptr_64 ptr,
  126. union atomic_val_64 val, int memorder);
  127. #endif /* latomic_fetch_add_64 */
  128. #ifdef __LP64__
  129. #define latomic_fetch_add_ul latomic_fetch_add_64
  130. #else /* __LP64__ */
  131. #define latomic_fetch_add_ul latomic_fetch_add_32
  132. #endif /* __LP64__ */
  133. #define latomic_fetch_add_ptr latomic_fetch_add_ul
  134. /* latomic_fetch_sub */
  135. #ifndef latomic_fetch_sub_32
  136. unsigned int latomic_fetch_sub_32(union atomic_ptr_32 ptr,
  137. union atomic_val_32 val, int memorder);
  138. #endif /* latomic_fetch_sub_32 */
  139. #ifndef latomic_fetch_sub_64
  140. unsigned long long latomic_fetch_sub_64(union atomic_ptr_64 ptr,
  141. union atomic_val_64 val, int memorder);
  142. #endif /* latomic_fetch_sub_64 */
  143. #ifdef __LP64__
  144. #define latomic_fetch_sub_ul latomic_fetch_sub_64
  145. #else /* __LP64__ */
  146. #define latomic_fetch_sub_ul latomic_fetch_sub_32
  147. #endif /* __LP64__ */
  148. #define latomic_fetch_sub_ptr latomic_fetch_sub_ul
  149. /* latomic_fetch_and */
  150. #ifndef latomic_fetch_and_32
  151. unsigned int latomic_fetch_and_32(union atomic_ptr_32 ptr,
  152. union atomic_val_32 val, int memorder);
  153. #endif /* latomic_fetch_and_32 */
  154. #ifndef latomic_fetch_and_64
  155. unsigned long long latomic_fetch_and_64(union atomic_ptr_64 ptr,
  156. union atomic_val_64 val, int memorder);
  157. #endif /* latomic_fetch_and_64 */
  158. #ifdef __LP64__
  159. #define latomic_fetch_and_ul latomic_fetch_and_64
  160. #else /* __LP64__ */
  161. #define latomic_fetch_and_ul latomic_fetch_and_32
  162. #endif /* __LP64__ */
  163. #define latomic_fetch_and_ptr latomic_fetch_and_ul
  164. /* latomic_fetch_or */
  165. #ifndef latomic_fetch_or_32
  166. unsigned int latomic_fetch_or_32(union atomic_ptr_32 ptr,
  167. union atomic_val_32 val, int memorder);
  168. #endif /* latomic_fetch_or_32 */
  169. #ifndef latomic_fetch_or_64
  170. unsigned long long latomic_fetch_or_64(union atomic_ptr_64 ptr,
  171. union atomic_val_64 val, int memorder);
  172. #endif /* latomic_fetch_or_64 */
  173. #ifdef __LP64__
  174. #define latomic_fetch_or_ul latomic_fetch_or_64
  175. #else /* __LP64__ */
  176. #define latomic_fetch_or_ul latomic_fetch_or_32
  177. #endif /* __LP64__ */
  178. #define latomic_fetch_or_ptr latomic_fetch_or_ul
  179. /* latomic_fetch_xor */
  180. #ifndef latomic_fetch_xor_32
  181. unsigned int latomic_fetch_xor_32(union atomic_ptr_32 ptr,
  182. union atomic_val_32 val, int memorder);
  183. #endif /* latomic_fetch_xor_32 */
  184. #ifndef latomic_fetch_xor_64
  185. unsigned long long latomic_fetch_xor_64(union atomic_ptr_64 ptr,
  186. union atomic_val_64 val, int memorder);
  187. #endif /* latomic_fetch_xor_64 */
  188. #ifdef __LP64__
  189. #define latomic_fetch_xor_ul latomic_fetch_xor_64
  190. #else /* __LP64__ */
  191. #define latomic_fetch_xor_ul latomic_fetch_xor_32
  192. #endif /* __LP64__ */
  193. #define latomic_fetch_xor_ptr latomic_fetch_xor_ul
  194. /* latomic_add */
  195. #ifndef latomic_add_32
  196. void latomic_add_32(union atomic_ptr_32 ptr, union atomic_val_32 val,
  197. int memorder);
  198. #endif /* latomic_add_32 */
  199. #ifndef latomic_add_64
  200. void latomic_add_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  201. int memorder);
  202. #endif /* latomic_add_64 */
  203. #ifdef __LP64__
  204. #define latomic_add_ul latomic_add_64
  205. #else /* __LP64__ */
  206. #define latomic_add_ul latomic_add_32
  207. #endif /* __LP64__ */
  208. #define latomic_add_ptr latomic_add_ul
  209. /* latomic_sub */
  210. #ifndef latomic_sub_32
  211. void latomic_sub_32(union atomic_ptr_32 ptr, union atomic_val_32 val,
  212. int memorder);
  213. #endif /* latomic_sub_32 */
  214. #ifndef latomic_sub_64
  215. void latomic_sub_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  216. int memorder);
  217. #endif /* latomic_sub_64 */
  218. #ifdef __LP64__
  219. #define latomic_sub_ul latomic_sub_64
  220. #else /* __LP64__ */
  221. #define latomic_sub_ul latomic_sub_32
  222. #endif /* __LP64__ */
  223. #define latomic_sub_ptr latomic_sub_ul
  224. /* latomic_and */
  225. #ifndef latomic_and_32
  226. void latomic_and_32(union atomic_ptr_32 ptr, union atomic_val_32 val,
  227. int memorder);
  228. #endif /* latomic_and_32 */
  229. #ifndef latomic_and_64
  230. void latomic_and_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  231. int memorder);
  232. #endif /* latomic_and_64 */
  233. #ifdef __LP64__
  234. #define latomic_and_ul latomic_and_64
  235. #else /* __LP64__ */
  236. #define latomic_and_ul latomic_and_32
  237. #endif /* __LP64__ */
  238. #define latomic_and_ptr latomic_and_ul
  239. /* latomic_or */
  240. #ifndef latomic_or_32
  241. void latomic_or_32(union atomic_ptr_32 ptr, union atomic_val_32 val,
  242. int memorder);
  243. #endif /* latomic_or_32 */
  244. #ifndef latomic_or_64
  245. void latomic_or_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  246. int memorder);
  247. #endif /* latomic_or_64 */
  248. #ifdef __LP64__
  249. #define latomic_or_ul latomic_or_64
  250. #else /* __LP64__ */
  251. #define latomic_or_ul latomic_or_32
  252. #endif /* __LP64__ */
  253. #define latomic_or_ptr latomic_or_ul
  254. /* latomic_xor */
  255. #ifndef latomic_xor_32
  256. void latomic_xor_32(union atomic_ptr_32 ptr, union atomic_val_32 val,
  257. int memorder);
  258. #endif /* latomic_xor_32 */
  259. #ifndef latomic_xor_64
  260. void latomic_xor_64(union atomic_ptr_64 ptr, union atomic_val_64 val,
  261. int memorder);
  262. #endif /* latomic_xor_64 */
  263. #ifdef __LP64__
  264. #define latomic_xor_ul latomic_xor_64
  265. #else /* __LP64__ */
  266. #define latomic_xor_ul latomic_xor_32
  267. #endif /* __LP64__ */
  268. #define latomic_xor_ptr latomic_xor_ul
  269. #endif /* KERN_LATOMIC_I_H */