-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Belozierov
committed
Apr 1, 2020
1 parent
2bc9a0f
commit 42a0257
Showing
10 changed files
with
177 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// AtomicInt.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 01.04.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
#if SWIFT_PACKAGE | ||
import CCoroutine | ||
#endif | ||
|
||
internal struct AtomicInt { | ||
|
||
private var _value: Int | ||
|
||
@inlinable init(value: Int) { | ||
_value = value | ||
} | ||
|
||
@inlinable var value: Int { | ||
get { _value } | ||
set { | ||
withUnsafeMutablePointer(to: &_value) { | ||
__atomicStore(OpaquePointer($0), newValue) | ||
} | ||
} | ||
} | ||
|
||
@inlinable mutating func add(_ value: Int) { | ||
withUnsafeMutablePointer(to: &_value) { | ||
__atomicFetchAdd(OpaquePointer($0), value) | ||
} | ||
} | ||
|
||
@inlinable mutating func increase() { add(1) } | ||
@inlinable mutating func decrease() { add(-1) } | ||
|
||
@discardableResult @inlinable | ||
mutating func update(_ transform: (Int) -> Int) -> (old: Int, new: Int) { | ||
withUnsafeMutablePointer(to: &_value) { | ||
var oldValue = $0.pointee, newValue: Int | ||
repeat { newValue = transform(oldValue) } | ||
while __atomicCompareExchange(OpaquePointer($0), &oldValue, newValue) == 0 | ||
return (oldValue, newValue) | ||
} | ||
} | ||
|
||
@inlinable mutating func update(_ newValue: Int) -> Int { | ||
withUnsafeMutablePointer(to: &_value) { | ||
__atomicExchange(OpaquePointer($0), newValue) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters