]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/threading_internal.h
Security-54.1.3.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / threading_internal.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // threading_internal - internal support classes and functions for threading implementation
21 //
22 #ifndef _H_THREADING_INTERNAL
23 #define _H_THREADING_INTERNAL
24
25 #include <Security/utilities.h>
26
27
28 namespace Security {
29
30
31 //
32 // Architecture-specific atomic operation primitives.
33 // AtomicWord is an integer type that works with these;
34 // we'll assume that a pointer fits into it (using reinterpret_cast).
35 //
36 #if TARGET_CPU_PPC
37
38 #define _HAVE_ATOMIC_OPERATIONS
39
40 typedef unsigned int AtomicWord;
41
42 inline AtomicWord atomicLoad(AtomicWord &atom)
43 {
44 AtomicWord result;
45 asm volatile (
46 "0: lwarx %0,0,%1 \n"
47 " stwcx. %0,0,%1 \n"
48 " bne- 0b"
49 : "=&r"(result)
50 : "b"(&atom)
51 : "cc"
52 );
53 return result;
54 }
55
56 inline AtomicWord atomicStore(AtomicWord &atom, AtomicWord newValue, AtomicWord oldValue)
57 {
58 register bool result;
59 asm volatile (
60 "0: lwarx %0,0,%1 \n" // load and reserve -> %0
61 " cmpw %0,%3 \n" // compare to old
62 " bne 1f \n" // fail if not equal
63 " stwcx. %2,0,%1 \n" // store and check
64 " bne 0b \n" // retry if contended
65 "1: "
66 : "=&r"(result)
67 : "b"(&atom), "r"(newValue), "r"(oldValue)
68 : "cc"
69 );
70 return result;
71 }
72
73 inline AtomicWord atomicOffset(AtomicWord &atom, int offset)
74 {
75 AtomicWord result;
76 asm volatile (
77 "0: lwarx %0,0,%1 \n"
78 " add %0,%0,%2 \n"
79 " stwcx. %0,0,%1 \n"
80 " bne- 0b"
81 : "=&r"(result)
82 : "b"(&atom), "r"(offset)
83 : "cc"
84 );
85 return result;
86 }
87
88 inline AtomicWord atomicIncrement(AtomicWord &atom)
89 { return atomicOffset(atom, +1); }
90
91 inline AtomicWord atomicDecrement(AtomicWord &atom)
92 { return atomicOffset(atom, -1); }
93
94 #endif //TARGET_CPU_PPC
95
96 } // end namespace Security
97
98 #endif //_H_THREADING_INTERNAL