]> git.sesse.net Git - casparcg/blob - tbb/include/tbb/machine/macos_common.h
2.0. Updated tbb library.
[casparcg] / tbb / include / tbb / machine / macos_common.h
1 /*
2     Copyright 2005-2011 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of 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 Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #if !defined(__TBB_machine_H) || defined(__TBB_machine_macos_common_H)
30 #error Do not include this file directly; include tbb_machine.h instead
31 #endif
32
33 #define __TBB_machine_macos_common_H
34
35 #include <sched.h>
36 #define __TBB_Yield()  sched_yield()
37
38 // __TBB_HardwareConcurrency
39
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
42
43 static inline int __TBB_macos_available_cpu() {
44     int name[2] = {CTL_HW, HW_AVAILCPU};
45     int ncpu;
46     size_t size = sizeof(ncpu);
47     sysctl( name, 2, &ncpu, &size, NULL, 0 );
48     return ncpu;
49 }
50
51 #define __TBB_HardwareConcurrency() __TBB_macos_available_cpu()
52
53 #ifndef __TBB_full_memory_fence
54     // TBB has not recognized the architecture (none of the architecture abstraction
55     // headers was included).
56     #define __TBB_UnknownArchitecture 1
57 #endif
58
59 #if __TBB_UnknownArchitecture || __TBB_WORDSIZE==4
60 // In case of IA32 this is a workaround for compiler bugs triggered by inline
61 // assembly implementation of __TBB_machine_cmpswp8 in linux_ia32.h, which may
62 // lead to incorrect codegen (gcc) or compilation failures (any icc including 12.0.4).
63
64 // Implementation of atomic operations based on OS provided primitives
65 #include <libkern/OSAtomic.h>
66
67 static inline int64_t __TBB_machine_cmpswp8_OsX(volatile void *ptr, int64_t value, int64_t comparand)
68 {
69     __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
70     int64_t* address = (int64_t*)ptr;
71     while( !OSAtomicCompareAndSwap64Barrier(comparand, value, address) ){
72 #if __TBB_WORDSIZE==8
73         int64_t snapshot = *address;
74 #else
75         int64_t snapshot = OSAtomicAdd64( 0, address );
76 #endif
77         if( snapshot!=comparand ) return snapshot;
78     }
79     return comparand;
80 }
81
82 #define __TBB_machine_cmpswp8 __TBB_machine_cmpswp8_OsX
83
84 #endif /* __TBB_UnknownArchitecture || __TBB_WORDSIZE==4 */
85
86 #if __TBB_UnknownArchitecture
87
88 #ifndef __TBB_WORDSIZE
89 #define __TBB_WORDSIZE 4
90 #endif
91
92 #define __TBB_BIG_ENDIAN __BIG_ENDIAN__
93
94 /** As this generic implementation has absolutely no information about underlying
95     hardware, its performance most likely will be sub-optimal because of full memory
96     fence usages where a more lightweight synchronization means (or none at all)
97     could suffice. Thus if you use this header to enable TBB on a new platform,
98     consider forking it and relaxing below helpers as appropriate. **/
99 #define __TBB_control_consistency_helper() OSMemoryBarrier()
100 #define __TBB_acquire_consistency_helper() OSMemoryBarrier()
101 #define __TBB_release_consistency_helper() OSMemoryBarrier()
102 #define __TBB_full_memory_fence()          OSMemoryBarrier()
103
104 static inline int32_t __TBB_machine_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand)
105 {
106     __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
107     int32_t* address = (int32_t*)ptr;
108     while( !OSAtomicCompareAndSwap32Barrier(comparand, value, address) ){
109         int32_t snapshot = *address;
110         if( snapshot!=comparand ) return snapshot;
111     }
112     return comparand;
113 }
114
115 static inline int32_t __TBB_machine_fetchadd4(volatile void *ptr, int32_t addend)
116 {
117     __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
118     return OSAtomicAdd32Barrier(addend, (int32_t*)ptr) - addend;
119 }
120
121 static inline int64_t __TBB_machine_fetchadd8(volatile void *ptr, int64_t addend)
122 {
123     __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
124     return OSAtomicAdd64Barrier(addend, (int64_t*)ptr) - addend;
125 }
126
127 #define __TBB_USE_GENERIC_PART_WORD_CAS             1
128 #define __TBB_USE_GENERIC_PART_WORD_FETCH_ADD       1
129 #define __TBB_USE_GENERIC_FETCH_STORE               1
130 #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE    1
131 #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE        1
132 #if __TBB_WORDSIZE == 4
133     #define __TBB_USE_GENERIC_DWORD_LOAD_STORE      1
134 #endif
135
136 #endif /* __TBB_UnknownArchitecture */