]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2001,2003-2004,2011,2013-2014 Apple Inc. All Rights Reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | ||
25 | // | |
26 | // threading_internal - internal support classes and functions for threading implementation | |
27 | // | |
28 | #ifndef _H_THREADING_INTERNAL | |
29 | #define _H_THREADING_INTERNAL | |
30 | ||
31 | #include <security_utilities/utilities.h> | |
32 | #include <libkern/OSAtomic.h> | |
33 | ||
34 | ||
35 | namespace Security { | |
36 | ||
37 | ||
38 | // | |
39 | // Do we have 64-bit atomic operations? | |
40 | // | |
41 | #if (defined(__ppc64__) || defined(__i386__) || defined(__x86_64__)) | |
42 | #define _HAVE_64BIT_ATOMIC 1 | |
43 | #else | |
44 | #define _HAVE_64BIT_ATOMIC 0 | |
45 | #endif | |
46 | ||
47 | ||
48 | // | |
49 | // The AtomicTypes class is an implementation detail. | |
50 | // Do not use it. | |
51 | // | |
52 | template <unsigned wordsize> | |
53 | struct AtomicTypes { | |
54 | // unsupported word size (this will cause compilation errors if used) | |
55 | }; | |
56 | ||
57 | template <> | |
58 | struct AtomicTypes<32> { | |
59 | typedef int32_t Integer; | |
60 | ||
61 | static Integer add(int delta, Integer &base) | |
62 | { return OSAtomicAdd32(delta, &base); } | |
63 | static Integer addb(int delta, Integer &base) { return OSAtomicAdd32Barrier(delta, &base); } | |
64 | ||
65 | static bool cas(Integer oldValue, Integer newValue, Integer &base) | |
66 | { return OSAtomicCompareAndSwap32(oldValue, newValue, &base); } | |
67 | static bool casb(Integer oldValue, Integer newValue, Integer &base) | |
68 | { return OSAtomicCompareAndSwap32Barrier(oldValue, newValue, &base); } | |
69 | }; | |
70 | ||
71 | #if _HAVE_64BIT_ATOMIC | |
72 | ||
73 | template <> | |
74 | struct AtomicTypes<64> { | |
75 | typedef int64_t Integer; | |
76 | ||
77 | static Integer add(int delta, Integer &base) { return OSAtomicAdd64(delta, &base); } | |
78 | static Integer addb(int delta, Integer &base) { return OSAtomicAdd64Barrier(delta, &base); } | |
79 | ||
80 | static bool cas(Integer oldValue, Integer newValue, Integer &base) | |
81 | { return OSAtomicCompareAndSwap64(oldValue, newValue, &base); } | |
82 | static bool casb(Integer oldValue, Integer newValue, Integer &base) | |
83 | { return OSAtomicCompareAndSwap64Barrier(oldValue, newValue, &base); } | |
84 | }; | |
85 | ||
86 | #endif //_HAVE_64BIT_ATOMIC | |
87 | ||
88 | ||
89 | // | |
90 | // Atomic<Type> is a set of (static) operations that can atomically access memory. | |
91 | // This is not a wrapper object. Think of it as a generator class that produces | |
92 | // the proper atomic memory operations for arbitrary data types. | |
93 | // If the underlying system does not support atomicity for a particular type | |
94 | // (e.g. 64 bits on ppc, or 16 bits anywhere), you will get compilation errors. | |
95 | // | |
96 | template <class Type> | |
97 | class Atomic { | |
98 | typedef AtomicTypes<sizeof(Type) * 8> _Ops; | |
99 | typedef typename _Ops::Integer _Type; | |
100 | ||
101 | public: | |
102 | static Type add(int delta, Type &store) | |
103 | { return Type(_Ops::add(delta, (_Type &)store)); } | |
104 | static Type addb(int delta, Type &store) | |
105 | { return Type(_Ops::addb(delta, (_Type &)store)); } | |
106 | ||
107 | static bool cas(Type oldValue, Type newValue, Type &store) | |
108 | { return _Ops::cas(_Type(oldValue), _Type(newValue), (_Type &)store); } | |
109 | static bool casb(Type oldValue, Type newValue, Type &store) | |
110 | { return _Ops::casb(_Type(oldValue), _Type(newValue), (_Type &)store); } | |
111 | ||
112 | static void barrier() { OSMemoryBarrier(); } | |
113 | static void readBarrier() { OSMemoryBarrier(); } | |
114 | static void writeBarrier() { OSMemoryBarrier(); } | |
115 | ||
116 | // convenience additions (expressed in terms above) | |
117 | ||
118 | static Type increment(Type &store) { return add(1, store); } | |
119 | static Type decrement(Type &store) { return add(-1, store); } | |
120 | ||
121 | static Type load(const Type &store) { readBarrier(); return store; } | |
122 | static Type store(Type value, Type &store) | |
123 | { while (!casb(store, value, store)) {}; return value; } | |
124 | }; | |
125 | ||
126 | ||
127 | } // end namespace Security | |
128 | ||
129 | #endif //_H_THREADING_INTERNAL |