-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadsafeOperators.swift
More file actions
115 lines (88 loc) · 2.57 KB
/
ThreadsafeOperators.swift
File metadata and controls
115 lines (88 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import Foundation
// 32 and 64 bit signed types
infix operator +=! { associativity right precedence 90 }
infix operator -=! { associativity right precedence 90 }
postfix operator ++! {}
postfix operator --! {}
// unsigned 32 bit types for win32 and GPU inter-op, mainly.
infix operator |=! { associativity right precedence 90 }
infix operator &=! { associativity right precedence 90 }
infix operator ^=! { associativity right precedence 90 }
// where rhs is dependent on lhs
infix operator =! { associativity right precedence 90 }
// MARK: -
func +=!(inout lhs: Int64, rhs: Int64) -> Int64 {
OSAtomicAdd64(rhs, &lhs)
}
func +=!(inout lhs: Int32, rhs: Int32) -> Int32 {
OSAtomicAdd32(rhs, &lhs)
}
func -=!(inout lhs: Int64, rhs: Int64) -> Int64 {
lhs +=! -rhs
}
func -=!(inout lhs: Int32, rhs: Int32) -> Int32 {
lhs +=! -rhs
}
postfix func ++!(inout value: Int64) -> Int64 {
OSAtomicIncrement64(&value)
}
postfix func ++!(inout value: Int32) -> Int32 {
OSAtomicIncrement32(&value)
}
postfix func --!(inout value: Int64) -> Int64 {
OSAtomicDecrement64(&value)
}
postfix func --!(inout value: Int32) -> Int32 {
OSAtomicDecrement32(&value)
}
func |=!(inout lhs: UInt32, rhs: UInt32) -> UInt32 {
UInt32(OSAtomicOr32(rhs, &lhs))
}
func &=!(inout lhs: UInt32, rhs: UInt32) -> UInt32 {
UInt32(OSAtomicAnd32(rhs, &lhs))
}
func ^=!(inout lhs: UInt32, rhs: UInt32) -> UInt32 {
UInt32(OSAtomicXor32(rhs, &lhs))
}
func =!(inout lhs: Int32, rhs: @autoclosure () -> Int32) -> Int32 {
var success: Bool
var newValue: Int32!
do {
let original = lhs
newValue = rhs()
success = OSAtomicCompareAndSwap32(original, newValue, &lhs)
} while(!success)
return newValue
}
func =!(inout lhs: Int64, rhs: @autoclosure () -> Int64) -> Int64 {
var success: Bool
var newValue: Int64!
do {
let original = lhs
newValue = rhs()
success = OSAtomicCompareAndSwap64(original, newValue, &lhs)
} while(!success)
return newValue
}
func =!(inout lhs: Int, rhs: @autoclosure () -> Int) -> Int {
var success: Bool
var newValue: Int!
do {
let original = lhs
newValue = rhs()
success = OSAtomicCompareAndSwapLong(original, newValue, &lhs)
} while(!success)
return newValue
}
func =!(inout lhs: UnsafeMutablePointer<Void>,
rhs: @autoclosure () -> UnsafeMutablePointer<Void>
) -> UnsafeMutablePointer<Void> {
var success: Bool
var newValue: UnsafeMutablePointer<Void>!
do {
let original = lhs
newValue = rhs()
success = OSAtomicCompareAndSwapPtr(original, newValue, &lhs)
} while(!success)
return newValue
}