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
1 parent
cf75ac9
commit cd451fb
Showing
1 changed file
with
87 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,90 @@ | ||
# Atomic | ||
|
||
Atomic is an over engineered Generic Wrapper that ensures atomic access to the wrapped value. | ||
Atomic is a thread safe structure for accessing a value atomically. Atomic is mostly just a random project that scratches an itch. | ||
|
||
This is mostly built just to get a weird itch scratched. | ||
## Swift Package | ||
|
||
This is now a Swift Package! | ||
|
||
## `Atomic<T>` | ||
|
||
This is the main Atomic class that allows you to wrap a value for atomic usage. It allows for the most thread safe interactions with the wrapped value. | ||
|
||
### Usage | ||
|
||
```swift | ||
// Create an atomic string | ||
let atomic = Atomic<String>("Atomic String") | ||
|
||
// Gets the current value | ||
let value = atomic.get() | ||
|
||
// Sets the atomic value to the passed in value | ||
atomic.set("New Atomic String") | ||
|
||
/* | ||
Allows extended work to happen with the value and | ||
ensures the value will not be changed while the | ||
modify is running. | ||
The atomic value will be set to the returns of | ||
the closure. | ||
The `holdWhile(_)` function has a similar function | ||
but the value has no modifcations. | ||
*/ | ||
atomic.modify { value in | ||
if value == "New Atomic String" { | ||
return "New " + value | ||
} else { | ||
return "Well New String Again" | ||
} | ||
} | ||
|
||
/* | ||
Allows multiple Atomics to have work done with their | ||
values while ensuring the values do not change. | ||
Must be paired with a `release()` after work is done. | ||
*/ | ||
let value1 = atomic.hold() | ||
let value2 = atomic2.hold() | ||
|
||
// do work with value1 and value 2 | ||
|
||
atomic.release() | ||
atomic2.release() | ||
|
||
``` | ||
|
||
## `SimpleAtomic<T>` | ||
|
||
This is a `propertyWrapper` that is very simplified atomic wrapper. It allows atomic get and set access but does not include all the features of `Atomic<T>` | ||
|
||
### Usage | ||
|
||
```swift | ||
// Create a Simple Atomic | ||
@SimpleAtomic var atomic = "Another Atomic String" | ||
|
||
// Gets the value atomically | ||
print(atomic) | ||
|
||
// Sets the value atomically | ||
atomic = "New Another Atomic String" | ||
|
||
``` | ||
|
||
### Do Not Do | ||
```swift | ||
@SimpleAtomic var atomic = "Another Atomic String" | ||
|
||
// LATER | ||
if atomic == "Another Atomic String" { | ||
atomic = "Modifying " + atomic | ||
} else { | ||
atomic = atomic + ". YAY!" | ||
} | ||
``` | ||
|
||
The result of the assigment is not guarenteed to tbe the same value as the check in the if statement. |