]> git.sesse.net Git - casparcg/blob - tbb/include/tbb/machine/macos_common.h
Added missing ffmpeg file.
[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 #ifndef __TBB_machine_H
30 #error Do not include this file directly; include tbb_machine.h instead
31 #endif
32
33 #include <sched.h>
34 #define __TBB_Yield()  sched_yield()
35
36
37 // __TBB_HardwareConcurrency
38
39 #include <sys/types.h>
40 #include <sys/sysctl.h>
41
42 static inline int __TBB_macos_available_cpu() {
43     int name[2] = {CTL_HW, HW_AVAILCPU};
44     int ncpu;
45     size_t size = sizeof(ncpu);
46     sysctl( name, 2, &ncpu, &size, NULL, 0 );
47     return ncpu;
48 }
49
50 #define __TBB_HardwareConcurrency() __TBB_macos_available_cpu()
51
52
53 #ifndef __TBB_WORDSIZE
54 #define __TBB_WORDSIZE 4
55 #endif
56
57 #ifndef __TBB_BIG_ENDIAN
58 #if __BIG_ENDIAN__
59 #define __TBB_BIG_ENDIAN 1
60 #else
61 #define __TBB_BIG_ENDIAN 0
62 #endif
63 #endif
64
65
66 #if !defined(__TBB_CompareAndSwap4) || !defined(__TBB_CompareAndSwap8)
67
68 // Implementation of atomic operations based on OS provided primitives
69 #include <libkern/OSAtomic.h>
70
71 #define __TBB_release_consistency_helper() OSMemoryBarrier()
72 #define __TBB_full_memory_fence()          OSMemoryBarrier()
73
74 static inline int32_t __TBB_macos_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand)
75 {
76     __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
77     int32_t* address = (int32_t*)ptr;
78     while( !OSAtomicCompareAndSwap32Barrier(comparand, value, address) ){
79         int32_t snapshot = *address;
80         if( snapshot!=comparand ) return snapshot;
81     }
82     return comparand;
83 }
84
85 static inline int64_t __TBB_macos_cmpswp8(volatile void *ptr, int64_t value, int64_t comparand)
86 {
87     __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
88     int64_t* address = (int64_t*)ptr;
89     while( !OSAtomicCompareAndSwap64Barrier(comparand, value, address) ){
90 #if __TBB_WORDSIZE==8
91         int64_t snapshot = *address;
92 #else
93         int64_t snapshot = OSAtomicAdd64( 0, address );
94 #endif
95         if( snapshot!=comparand ) return snapshot;
96     }
97     return comparand;
98 }
99
100 #define __TBB_CompareAndSwap4(P,V,C) __TBB_macos_cmpswp4(P,V,C)
101 #define __TBB_CompareAndSwap8(P,V,C) __TBB_macos_cmpswp8(P,V,C)
102
103 static inline int32_t __TBB_macos_fetchadd4(volatile void *ptr, int32_t addend)
104 {
105     __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
106     return OSAtomicAdd32Barrier(addend, (int32_t*)ptr) - addend;
107 }
108
109 static inline int64_t __TBB_macos_fetchadd8(volatile void *ptr, int64_t addend)
110 {
111     __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
112     return OSAtomicAdd64Barrier(addend, (int64_t*)ptr) - addend;
113 }
114
115 #define __TBB_FetchAndAdd4(P,V) __TBB_macos_fetchadd4(P,V)
116 #define __TBB_FetchAndAdd8(P,V) __TBB_macos_fetchadd8(P,V)
117
118 #if __TBB_WORDSIZE==4
119 #define __TBB_CompareAndSwapW(P,V,C) __TBB_CompareAndSwap4(P,V,C)
120 #define __TBB_FetchAndAddW(P,V) __TBB_FetchAndAdd4(P,V)
121 #else
122 #define __TBB_CompareAndSwapW(P,V,C) __TBB_CompareAndSwap8(P,V,C)
123 #define __TBB_FetchAndAddW(P,V) __TBB_FetchAndAdd8(P,V)
124 #endif
125
126 #endif /* !defined(__TBB_CompareAndSwap4) || !defined(__TBB_CompareAndSwap8) */