forked from apple/swift-atomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtomicInteger.swift
127 lines (120 loc) · 5.45 KB
/
AtomicInteger.swift
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
116
117
118
119
120
121
122
123
124
125
126
127
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if ATOMICS_NATIVE_BUILTINS
import Swift
#endif
/// A type that supports atomic integer operations through a separate
/// atomic storage representation.
///
/// Atomic integer types provide a number of additional atomic
/// operations beyond the ones supported by `AtomicValue`, such as
/// atomic increments/decrements as well as atomic bitwise operations.
/// These may be mapped to dedicated instructions that can be more
/// efficient than implementations based on the general compare and
/// exchange operation; however, this depends on the capabilities of
/// the compiler and the underlying hardware.
public protocol AtomicInteger: AtomicValue, FixedWidthInteger
where
AtomicRepresentation: AtomicIntegerStorage,
AtomicRepresentation.Value == Self
{}
/// The storage representation for an atomic integer value, providing
/// pointer-based atomic operations.
///
/// This is a low-level implementation detail of atomic types; instead
/// of directly handling conforming types, it is usually better to use
/// the `UnsafeAtomic` or `ManagedAtomic` generics -- these provide a
/// more reliable interface while ensuring that the storage is
/// correctly constructed and destroyed.
public protocol AtomicIntegerStorage: AtomicStorage {
/// Perform an atomic wrapping increment operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+=` operator does on integer values.
///
/// - Parameter operand: The value to add to the current value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenWrappingIncrement(
by operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic wrapping decrement operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-=` operator does on integer values.
///
/// - Parameter operand: The value to subtract from the current value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenWrappingDecrement(
by operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic bitwise AND operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenBitwiseAnd(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic bitwise OR operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenBitwiseOr(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic bitwise XOR operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenBitwiseXor(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
}