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
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import (
autogenerated_warning, integerOperations, lowerFirst, argLabel)
}%
${autogenerated_warning()}
% for construct in ["UnsafeAtomic", "ManagedAtomic"]:
extension ${construct} where Value: AtomicInteger {
% for (name, _, op, label, doc) in integerOperations:
/// Perform an atomic ${doc} operation and return the original value, applying
/// the specified memory ordering.
///
% if "Wrapping" in name:
/// Note: This operation silently wraps around on overflow, like the
/// `${op}` operator does on `Int` values.
///
% end
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThen${name}(
${label} operand: Value${" = 1" if "crement" in name else ""},
ordering: AtomicUpdateOrdering
) -> Value {
_Storage.atomicLoadThen${name}(
${argLabel(label)}operand,
at: _ptr,
ordering: ordering)
}
% end
% for (name, _, op, label, doc) in integerOperations:
/// Perform an atomic ${doc} operation and return the new value, applying
/// the specified memory ordering.
///
% if "Wrapping" in name:
/// Note: This operation silently wraps around on overflow, like the
/// `${op}` operator does on `Int` values.
///
% end
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func ${lowerFirst(name)}ThenLoad(
${label} operand: Value${" = 1" if "crement" in name else ""},
ordering: AtomicUpdateOrdering
) -> Value {
let original = _Storage.atomicLoadThen${name}(
${argLabel(label)}operand,
at: _ptr,
ordering: ordering)
return original ${op} operand
}
% end
/// Perform an atomic wrapping increment operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+=` operator does on `Int` values.
///
/// - Parameter operand: The value to add to the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingIncrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = _Storage.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping decrement operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-=` operator does on `Int` values.
///
/// - Parameter operand: The value to subtract from the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingDecrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = _Storage.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
}
}
|