ua_util.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef UA_UTIL_H_
  5. #define UA_UTIL_H_
  6. #include "ua_config.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /* Assert */
  11. #include <assert.h>
  12. #define UA_assert(ignore) assert(ignore)
  13. /* BSD Queue Macros */
  14. #include "queue.h"
  15. /* C++ Access to datatypes defined inside structs (for queue.h) */
  16. #ifdef __cplusplus
  17. # define memberstruct(container,member) container::member
  18. #else
  19. # define memberstruct(container,member) member
  20. #endif
  21. /* container_of */
  22. #define container_of(ptr, type, member) \
  23. (type *)((uintptr_t)ptr - offsetof(type,member))
  24. /* Thread Local Storage */
  25. #ifdef UA_ENABLE_MULTITHREADING
  26. # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  27. # define UA_THREAD_LOCAL _Thread_local /* C11 */
  28. # elif defined(__GNUC__)
  29. # define UA_THREAD_LOCAL __thread /* GNU extension */
  30. # elif defined(_MSC_VER)
  31. # define UA_THREAD_LOCAL __declspec(thread) /* MSVC extension */
  32. # else
  33. # warning The compiler does not allow thread-local variables. The library can be built, but will not be thread-safe.
  34. # endif
  35. #endif
  36. #ifndef UA_THREAD_LOCAL
  37. # define UA_THREAD_LOCAL
  38. #endif
  39. /* Atomic Operations
  40. * -----------------
  41. * Atomic operations that synchronize across processor cores (for
  42. * multithreading). Only the inline-functions defined next are used. Replace
  43. * with architecture-specific operations if necessary. */
  44. #ifndef UA_ENABLE_MULTITHREADING
  45. # define UA_atomic_sync()
  46. #else
  47. # ifdef _MSC_VER /* Visual Studio */
  48. # define UA_atomic_sync() _ReadWriteBarrier()
  49. # else /* GCC/Clang */
  50. # define UA_atomic_sync() __sync_synchronize()
  51. # endif
  52. #endif
  53. static UA_INLINE void *
  54. UA_atomic_xchg(void * volatile * addr, void *newptr) {
  55. #ifndef UA_ENABLE_MULTITHREADING
  56. void *old = *addr;
  57. *addr = newptr;
  58. return old;
  59. #else
  60. # ifdef _MSC_VER /* Visual Studio */
  61. return _InterlockedExchangePointer(addr, newptr);
  62. # else /* GCC/Clang */
  63. return __sync_lock_test_and_set(addr, newptr);
  64. # endif
  65. #endif
  66. }
  67. static UA_INLINE void *
  68. UA_atomic_cmpxchg(void * volatile * addr, void *expected, void *newptr) {
  69. #ifndef UA_ENABLE_MULTITHREADING
  70. void *old = *addr;
  71. if(old == expected) {
  72. *addr = newptr;
  73. }
  74. return old;
  75. #else
  76. # ifdef _MSC_VER /* Visual Studio */
  77. return _InterlockedCompareExchangePointer(addr, expected, newptr);
  78. # else /* GCC/Clang */
  79. return __sync_val_compare_and_swap(addr, expected, newptr);
  80. # endif
  81. #endif
  82. }
  83. static UA_INLINE uint32_t
  84. UA_atomic_add(volatile uint32_t *addr, uint32_t increase) {
  85. #ifndef UA_ENABLE_MULTITHREADING
  86. *addr += increase;
  87. return *addr;
  88. #else
  89. # ifdef _MSC_VER /* Visual Studio */
  90. return _InterlockedExchangeAdd(addr, increase) + increase;
  91. # else /* GCC/Clang */
  92. return __sync_add_and_fetch(addr, increase);
  93. # endif
  94. #endif
  95. }
  96. #ifdef __cplusplus
  97. } // extern "C"
  98. #endif
  99. #endif /* UA_UTIL_H_ */