macos_common.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #if !defined(__TBB_machine_H) || defined(__TBB_machine_macos_common_H)
  14. #error Do not #include this internal file directly; use public TBB headers instead.
  15. #endif
  16. #define __TBB_machine_macos_common_H
  17. #include <sched.h>
  18. #define __TBB_Yield() sched_yield()
  19. // __TBB_HardwareConcurrency
  20. #include <sys/types.h>
  21. #include <sys/sysctl.h>
  22. static inline int __TBB_macos_available_cpu() {
  23. int name[2] = {CTL_HW, HW_AVAILCPU};
  24. int ncpu;
  25. size_t size = sizeof(ncpu);
  26. sysctl( name, 2, &ncpu, &size, NULL, 0 );
  27. return ncpu;
  28. }
  29. #define __TBB_HardwareConcurrency() __TBB_macos_available_cpu()
  30. #ifndef __TBB_full_memory_fence
  31. // TBB has not recognized the architecture (none of the architecture abstraction
  32. // headers was included).
  33. #define __TBB_UnknownArchitecture 1
  34. #endif
  35. #if __TBB_UnknownArchitecture
  36. // Implementation of atomic operations based on OS provided primitives
  37. #include <libkern/OSAtomic.h>
  38. static inline int64_t __TBB_machine_cmpswp8_OsX(volatile void *ptr, int64_t value, int64_t comparand)
  39. {
  40. __TBB_ASSERT( tbb::internal::is_aligned(ptr,8), "address not properly aligned for macOS* atomics");
  41. int64_t* address = (int64_t*)ptr;
  42. while( !OSAtomicCompareAndSwap64Barrier(comparand, value, address) ){
  43. #if __TBB_WORDSIZE==8
  44. int64_t snapshot = *address;
  45. #else
  46. int64_t snapshot = OSAtomicAdd64( 0, address );
  47. #endif
  48. if( snapshot!=comparand ) return snapshot;
  49. }
  50. return comparand;
  51. }
  52. #define __TBB_machine_cmpswp8 __TBB_machine_cmpswp8_OsX
  53. #endif /* __TBB_UnknownArchitecture */
  54. #if __TBB_UnknownArchitecture
  55. #ifndef __TBB_WORDSIZE
  56. #define __TBB_WORDSIZE __SIZEOF_POINTER__
  57. #endif
  58. #ifdef __TBB_ENDIANNESS
  59. // Already determined based on hardware architecture.
  60. #elif __BIG_ENDIAN__
  61. #define __TBB_ENDIANNESS __TBB_ENDIAN_BIG
  62. #elif __LITTLE_ENDIAN__
  63. #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
  64. #else
  65. #define __TBB_ENDIANNESS __TBB_ENDIAN_UNSUPPORTED
  66. #endif
  67. /** As this generic implementation has absolutely no information about underlying
  68. hardware, its performance most likely will be sub-optimal because of full memory
  69. fence usages where a more lightweight synchronization means (or none at all)
  70. could suffice. Thus if you use this header to enable TBB on a new platform,
  71. consider forking it and relaxing below helpers as appropriate. **/
  72. #define __TBB_control_consistency_helper() OSMemoryBarrier()
  73. #define __TBB_acquire_consistency_helper() OSMemoryBarrier()
  74. #define __TBB_release_consistency_helper() OSMemoryBarrier()
  75. #define __TBB_full_memory_fence() OSMemoryBarrier()
  76. static inline int32_t __TBB_machine_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand)
  77. {
  78. __TBB_ASSERT( tbb::internal::is_aligned(ptr,4), "address not properly aligned for macOS atomics");
  79. int32_t* address = (int32_t*)ptr;
  80. while( !OSAtomicCompareAndSwap32Barrier(comparand, value, address) ){
  81. int32_t snapshot = *address;
  82. if( snapshot!=comparand ) return snapshot;
  83. }
  84. return comparand;
  85. }
  86. static inline int32_t __TBB_machine_fetchadd4(volatile void *ptr, int32_t addend)
  87. {
  88. __TBB_ASSERT( tbb::internal::is_aligned(ptr,4), "address not properly aligned for macOS atomics");
  89. return OSAtomicAdd32Barrier(addend, (int32_t*)ptr) - addend;
  90. }
  91. static inline int64_t __TBB_machine_fetchadd8(volatile void *ptr, int64_t addend)
  92. {
  93. __TBB_ASSERT( tbb::internal::is_aligned(ptr,8), "address not properly aligned for macOS atomics");
  94. return OSAtomicAdd64Barrier(addend, (int64_t*)ptr) - addend;
  95. }
  96. #define __TBB_USE_GENERIC_PART_WORD_CAS 1
  97. #define __TBB_USE_GENERIC_PART_WORD_FETCH_ADD 1
  98. #define __TBB_USE_GENERIC_FETCH_STORE 1
  99. #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE 1
  100. #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE 1
  101. #if __TBB_WORDSIZE == 4
  102. #define __TBB_USE_GENERIC_DWORD_LOAD_STORE 1
  103. #endif
  104. #define __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE 1
  105. #endif /* __TBB_UnknownArchitecture */