|
1 | 1 | # ThreadSafeSwift
|
2 | 2 |
|
3 |
| -A description of this package. |
| 3 | +<p> |
| 4 | + <img src="https://img.shields.io/badge/Swift-5.1%2B-yellowgreen.svg?style=flat" /> |
| 5 | + <img src="https://img.shields.io/badge/License-MIT-blue.svg" /> |
| 6 | + <a href="https://github.com/apple/swift-package-manager"> |
| 7 | + <img src="https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat" /> |
| 8 | + </a> |
| 9 | +</p> |
| 10 | + |
| 11 | +Collection of Property Wrappers and other Types explicitly designed to provide quick, simple, and efficient Thread-Safety in your Swift projects. |
| 12 | + |
| 13 | +## Installation |
| 14 | +### Xcode Projects |
| 15 | +Select `File` -> `Swift Packages` -> `Add Package Dependency` and enter `https://github.com/Flowduino/ThreadSafeSwift.git` |
| 16 | + |
| 17 | +### Swift Package Manager Projects |
| 18 | +You can use `ThreadSafeSwift` as a Package Dependency in your own Packages' `Package.swift` file: |
| 19 | +```swift |
| 20 | +let package = Package( |
| 21 | + //... |
| 22 | + dependencies: [ |
| 23 | + .package( |
| 24 | + url: "https://github.com/Flowduino/ThreadSafeSwift.git", |
| 25 | + .upToNextMajor(from: "1.0.0") |
| 26 | + ), |
| 27 | + ], |
| 28 | + //... |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +From there, refer to `ThreadSafeSwift` as a "target dependency" in any of _your_ package's targets that need it. |
| 33 | + |
| 34 | +```swift |
| 35 | +targets: [ |
| 36 | + .target( |
| 37 | + name: "YourLibrary", |
| 38 | + dependencies: [ |
| 39 | + "ThreadSafeSwift", |
| 40 | + ], |
| 41 | + //... |
| 42 | + ), |
| 43 | + //... |
| 44 | +] |
| 45 | +``` |
| 46 | +You can then do `import ThreadSafeSwift` in any code that requires it. |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +Here are some quick and easy usage examples for the features provided by `ThreadSafeSwift`: |
| 51 | + |
| 52 | +### ThreadSafeSemaphore - Property Wrapper |
| 53 | +You can use the `ThreadSafeSemaphore` Property Wrapper to encapsulate any Value Type behind a Thread-Safe `DispatchSemaphore`. |
| 54 | +This is extremely easy for most types: |
| 55 | +```swift |
| 56 | +@ThreadSafeSemaphore var myInt: Int |
| 57 | +``` |
| 58 | + |
| 59 | +Further, you can access the underlying `DispatchSemaphore` directly, which is useful where you need to acquire the Lock for multiple operations that must performed *Atomically*: |
| 60 | +```swift |
| 61 | +@ThreadSafeSemaphore var myInts: [Int] |
| 62 | + |
| 63 | +//... |
| 64 | + |
| 65 | +func incrementEveryIntegerByOne() { |
| 66 | + _myInts.lock.wait() |
| 67 | + for (index,val) in myInts.enumerated() { |
| 68 | + myInts[index] = val + 1 |
| 69 | + } |
| 70 | + _myInts.lock.signal() |
| 71 | +} |
| 72 | +``` |
| 73 | +Of course, for Arrays, you really should try to minimize the number of get/set operations required, and the duration throughout which the `DispatchSemaphore` is locked: |
| 74 | +```swift |
| 75 | +@ThreadSafeSemaphore var myInts: [Int] |
| 76 | + |
| 77 | +//... |
| 78 | + |
| 79 | +func incrementEveryIntegerByOne() { |
| 80 | + var values = myInts // This would marshal the `DispatchSemaphore` and return a copy of the Array, then release the `DispatchSemaphore` |
| 81 | + for (index,val) in values.enumerated() { |
| 82 | + myInts[index] = val + 1 |
| 83 | + } |
| 84 | + myInts = values // This would marshal the `DispatchSempahore` and replace the entire Array with our modified one, then release the `DispatchSemaphore` |
| 85 | +} |
| 86 | +``` |
| 87 | +## License |
| 88 | + |
| 89 | +`ThreadSafeSwift` is available under the MIT license. See the [LICENSE file](./LICENSE) for more info. |
0 commit comments