├── .gitignore ├── .swift-version ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── Safe.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ └── Safe.xcscheme ├── Safe ├── Info.plist ├── Safe.h └── Safe.swift ├── SafeTests ├── Info.plist └── SafeTests.swift └── SwiftSafe.podspec /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | # Swift Package Manager 31 | # 32 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 33 | # Packages/ 34 | .build/ 35 | 36 | # CocoaPods 37 | # 38 | # We recommend against adding the Pods directory to your .gitignore. However 39 | # you should judge for yourself, the pros and cons are mentioned at: 40 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 41 | # 42 | # Pods/ 43 | 44 | # Carthage 45 | # 46 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 47 | # Carthage/Checkouts 48 | 49 | Carthage/Build 50 | 51 | # fastlane 52 | # 53 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 54 | # screenshots whenever they are needed. 55 | # For more information about the recommended setup visit: 56 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 57 | 58 | fastlane/report.xml 59 | fastlane/screenshots 60 | -------------------------------------------------------------------------------- /.swift-version: -------------------------------------------------------------------------------- 1 | 3.0 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at czechboy0@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Honza Dvorsky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftSafe 2 | Thread synchronization made easy. 3 | 4 | :question: Why? 5 | ---- 6 | Performance-sensitive classes need internal state synchronization, so that external accessors don't break the internal invariants and cause race conditions. Using GCD (Grand Central Dispatch) directly can confuse newcomers, because the API doesn't necessarily reflect the actual reason why it's being used. That's why wrapping it in a straighforward Swifty API can introduce a bit more clarity to your code. See examples below. 7 | 8 | :watch: Concurrency modes supported 9 | --------------------------- 10 | - EREW - **Exclusive Read, Exclusive Write**: The most common synchronization method, where only one thread can read or write your protected resource at one time. One big disadvantage is that it's prone to deadlocks. 11 | - CREW - **Concurrent Read, Exclusive Write**: Less common, but IMO more powerful method, where multiple threads can read, but only one thread can write at one time. Reading and writing is automatically made exclusive, i.e. all reads enqueued before a write are executed first, then the single write is executed, then more reads can be executed (those enqueued after the write). 12 | 13 | ## 📝 Requirements 14 | 15 | * iOS 8.0+ 16 | * Swift 3.0+ 17 | 18 | ## 📦 Installation 19 | 20 | ### Carthage 21 | ~~~ 22 | github "nodes-ios/SwiftSafe" ~> 2.0 23 | 24 | ~~~ 25 | 26 | > For Swift 2.2 release: 27 | > `github "nodes-ios/SwiftSafe" ~> 1.0` 28 | 29 | 30 | ### CocoaPods 31 | ~~~ 32 | pod 'SwiftSafe' 33 | ~~~ 34 | 35 | :mortar_board: Usage 36 | ----- 37 | Let's say you're writing a thread-safe `NSData` cache. You'd like it to use CREW access to maximize performance. 38 | 39 | ```swift 40 | class MyCache { 41 | private let storage: NSMutableDictionary = NSMutableDictionary() 42 | public func hit(key: String) -> NSData? { 43 | let found = self.storage[key] 44 | if let found = found { 45 | print("Hit for key \(key) -> \(found)") 46 | } else { 47 | print("Miss for key \(key)") 48 | } 49 | return found 50 | } 51 | public func update(key: String, value: NSData) { 52 | print("Updating \(key) -> \(value)") 53 | self.storage[key] = value 54 | } 55 | } 56 | ``` 57 | 58 | This is the first implementation. It works, but when you start calling it from multiple threads at the same time, you'll encounter inconsistencies and race conditions - meaning you'll be getting different results with the same sequence of actions. In more complicated cases, you might even cause a runtime crash. 59 | 60 | The way you fix it is obviously by protecting the internal `storage` (note: `NSDictionary` is not thread-safe). Again, to be most efficient, we want to allow any number of threads reading from the cache, but only one to update it (and ensure that nobody is reading from it at that time). You can achieve these guarantees with GCD, which is what `SwiftSafe` does. What this API brings to the table, however, is the simple and obvious naming. 61 | 62 | Let's see how we can make our cache thread-safe in a couple lines of code. 63 | 64 | ```swift 65 | import SwiftSafe 66 | 67 | class MyCache { 68 | private let storage: NSMutableDictionary = NSMutableDictionary() 69 | private let safe: Safe = CREW() 70 | public func hit(key: String) -> NSData? { 71 | var found: NSData? 72 | safe.read { 73 | found = self.storage[key] 74 | if let found = found { 75 | print("Hit for key \(key) -> \(found)") 76 | } else { 77 | print("Miss for key \(key)") 78 | } 79 | } 80 | return found 81 | } 82 | public func update(key: String, value: NSData) { 83 | safe.write { 84 | print("Updating \(key) -> \(value)") 85 | self.storage[key] = value 86 | } 87 | } 88 | } 89 | ``` 90 | 91 | That's it! Just import the library, create a `Safe` object which follows the concurrency mode you're trying to achieve (CREW in this case) and wrap your accesses to the shared resource in `read` and `write` calls. Note that `read` closures always **block** the caller thread, whereas `write` don't. If you ever have a call which both updates your resource and reads from it, make sure to split that functionality into the *writing* and *reading* part. This will make it much easier to reason about and parallelize. 92 | 93 | :point_right: Bottom line 94 | ------- 95 | Even though it might sound unnecessary to some, understandable and correct methods of synchronization will save you days of debugging and many headaches. :) 96 | 97 | :blue_heart: Code of Conduct 98 | ------------ 99 | Please note that this project is released with a [Contributor Code of Conduct](./CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms. 100 | 101 | :v: License 102 | ------- 103 | MIT 104 | 105 | :alien: Author 106 | ------ 107 | Honza Dvorsky - http://honzadvorsky.com, [@czechboy0](http://twitter.com/czechboy0) -------------------------------------------------------------------------------- /Safe.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 3AE6C5B21C4E4E7D00616C19 /* Safe.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AE6C5B11C4E4E7D00616C19 /* Safe.h */; settings = {ATTRIBUTES = (Public, ); }; }; 11 | 3AE6C5B91C4E4E7D00616C19 /* Safe.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AE6C5AE1C4E4E7D00616C19 /* Safe.framework */; }; 12 | 3AE6C5BE1C4E4E7D00616C19 /* SafeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE6C5BD1C4E4E7D00616C19 /* SafeTests.swift */; }; 13 | 3AE6C5C91C4E4E8B00616C19 /* Safe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE6C5C81C4E4E8B00616C19 /* Safe.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXContainerItemProxy section */ 17 | 3AE6C5BA1C4E4E7D00616C19 /* PBXContainerItemProxy */ = { 18 | isa = PBXContainerItemProxy; 19 | containerPortal = 3AE6C5A51C4E4E7D00616C19 /* Project object */; 20 | proxyType = 1; 21 | remoteGlobalIDString = 3AE6C5AD1C4E4E7D00616C19; 22 | remoteInfo = Safe; 23 | }; 24 | /* End PBXContainerItemProxy section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | 3AE6C5AE1C4E4E7D00616C19 /* Safe.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Safe.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | 3AE6C5B11C4E4E7D00616C19 /* Safe.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Safe.h; sourceTree = ""; }; 29 | 3AE6C5B31C4E4E7D00616C19 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 3AE6C5B81C4E4E7D00616C19 /* SafeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SafeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 3AE6C5BD1C4E4E7D00616C19 /* SafeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafeTests.swift; sourceTree = ""; }; 32 | 3AE6C5BF1C4E4E7D00616C19 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 33 | 3AE6C5C81C4E4E8B00616C19 /* Safe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Safe.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | 3AE6C5AA1C4E4E7D00616C19 /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | 3AE6C5B51C4E4E7D00616C19 /* Frameworks */ = { 45 | isa = PBXFrameworksBuildPhase; 46 | buildActionMask = 2147483647; 47 | files = ( 48 | 3AE6C5B91C4E4E7D00616C19 /* Safe.framework in Frameworks */, 49 | ); 50 | runOnlyForDeploymentPostprocessing = 0; 51 | }; 52 | /* End PBXFrameworksBuildPhase section */ 53 | 54 | /* Begin PBXGroup section */ 55 | 3AE6C5A41C4E4E7D00616C19 = { 56 | isa = PBXGroup; 57 | children = ( 58 | 3AE6C5B01C4E4E7D00616C19 /* Safe */, 59 | 3AE6C5BC1C4E4E7D00616C19 /* SafeTests */, 60 | 3AE6C5AF1C4E4E7D00616C19 /* Products */, 61 | ); 62 | sourceTree = ""; 63 | }; 64 | 3AE6C5AF1C4E4E7D00616C19 /* Products */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | 3AE6C5AE1C4E4E7D00616C19 /* Safe.framework */, 68 | 3AE6C5B81C4E4E7D00616C19 /* SafeTests.xctest */, 69 | ); 70 | name = Products; 71 | sourceTree = ""; 72 | }; 73 | 3AE6C5B01C4E4E7D00616C19 /* Safe */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 3AE6C5B11C4E4E7D00616C19 /* Safe.h */, 77 | 3AE6C5B31C4E4E7D00616C19 /* Info.plist */, 78 | 3AE6C5C81C4E4E8B00616C19 /* Safe.swift */, 79 | ); 80 | path = Safe; 81 | sourceTree = ""; 82 | }; 83 | 3AE6C5BC1C4E4E7D00616C19 /* SafeTests */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | 3AE6C5BD1C4E4E7D00616C19 /* SafeTests.swift */, 87 | 3AE6C5BF1C4E4E7D00616C19 /* Info.plist */, 88 | ); 89 | path = SafeTests; 90 | sourceTree = ""; 91 | }; 92 | /* End PBXGroup section */ 93 | 94 | /* Begin PBXHeadersBuildPhase section */ 95 | 3AE6C5AB1C4E4E7D00616C19 /* Headers */ = { 96 | isa = PBXHeadersBuildPhase; 97 | buildActionMask = 2147483647; 98 | files = ( 99 | 3AE6C5B21C4E4E7D00616C19 /* Safe.h in Headers */, 100 | ); 101 | runOnlyForDeploymentPostprocessing = 0; 102 | }; 103 | /* End PBXHeadersBuildPhase section */ 104 | 105 | /* Begin PBXNativeTarget section */ 106 | 3AE6C5AD1C4E4E7D00616C19 /* Safe */ = { 107 | isa = PBXNativeTarget; 108 | buildConfigurationList = 3AE6C5C21C4E4E7D00616C19 /* Build configuration list for PBXNativeTarget "Safe" */; 109 | buildPhases = ( 110 | 3AE6C5A91C4E4E7D00616C19 /* Sources */, 111 | 3AE6C5AA1C4E4E7D00616C19 /* Frameworks */, 112 | 3AE6C5AB1C4E4E7D00616C19 /* Headers */, 113 | 3AE6C5AC1C4E4E7D00616C19 /* Resources */, 114 | ); 115 | buildRules = ( 116 | ); 117 | dependencies = ( 118 | ); 119 | name = Safe; 120 | productName = Safe; 121 | productReference = 3AE6C5AE1C4E4E7D00616C19 /* Safe.framework */; 122 | productType = "com.apple.product-type.framework"; 123 | }; 124 | 3AE6C5B71C4E4E7D00616C19 /* SafeTests */ = { 125 | isa = PBXNativeTarget; 126 | buildConfigurationList = 3AE6C5C51C4E4E7D00616C19 /* Build configuration list for PBXNativeTarget "SafeTests" */; 127 | buildPhases = ( 128 | 3AE6C5B41C4E4E7D00616C19 /* Sources */, 129 | 3AE6C5B51C4E4E7D00616C19 /* Frameworks */, 130 | 3AE6C5B61C4E4E7D00616C19 /* Resources */, 131 | ); 132 | buildRules = ( 133 | ); 134 | dependencies = ( 135 | 3AE6C5BB1C4E4E7D00616C19 /* PBXTargetDependency */, 136 | ); 137 | name = SafeTests; 138 | productName = SafeTests; 139 | productReference = 3AE6C5B81C4E4E7D00616C19 /* SafeTests.xctest */; 140 | productType = "com.apple.product-type.bundle.unit-test"; 141 | }; 142 | /* End PBXNativeTarget section */ 143 | 144 | /* Begin PBXProject section */ 145 | 3AE6C5A51C4E4E7D00616C19 /* Project object */ = { 146 | isa = PBXProject; 147 | attributes = { 148 | LastSwiftUpdateCheck = 0730; 149 | LastUpgradeCheck = 0800; 150 | ORGANIZATIONNAME = "Honza Dvorsky"; 151 | TargetAttributes = { 152 | 3AE6C5AD1C4E4E7D00616C19 = { 153 | CreatedOnToolsVersion = 7.3; 154 | LastSwiftMigration = 0800; 155 | }; 156 | 3AE6C5B71C4E4E7D00616C19 = { 157 | CreatedOnToolsVersion = 7.3; 158 | LastSwiftMigration = 0800; 159 | }; 160 | }; 161 | }; 162 | buildConfigurationList = 3AE6C5A81C4E4E7D00616C19 /* Build configuration list for PBXProject "Safe" */; 163 | compatibilityVersion = "Xcode 3.2"; 164 | developmentRegion = English; 165 | hasScannedForEncodings = 0; 166 | knownRegions = ( 167 | en, 168 | ); 169 | mainGroup = 3AE6C5A41C4E4E7D00616C19; 170 | productRefGroup = 3AE6C5AF1C4E4E7D00616C19 /* Products */; 171 | projectDirPath = ""; 172 | projectRoot = ""; 173 | targets = ( 174 | 3AE6C5AD1C4E4E7D00616C19 /* Safe */, 175 | 3AE6C5B71C4E4E7D00616C19 /* SafeTests */, 176 | ); 177 | }; 178 | /* End PBXProject section */ 179 | 180 | /* Begin PBXResourcesBuildPhase section */ 181 | 3AE6C5AC1C4E4E7D00616C19 /* Resources */ = { 182 | isa = PBXResourcesBuildPhase; 183 | buildActionMask = 2147483647; 184 | files = ( 185 | ); 186 | runOnlyForDeploymentPostprocessing = 0; 187 | }; 188 | 3AE6C5B61C4E4E7D00616C19 /* Resources */ = { 189 | isa = PBXResourcesBuildPhase; 190 | buildActionMask = 2147483647; 191 | files = ( 192 | ); 193 | runOnlyForDeploymentPostprocessing = 0; 194 | }; 195 | /* End PBXResourcesBuildPhase section */ 196 | 197 | /* Begin PBXSourcesBuildPhase section */ 198 | 3AE6C5A91C4E4E7D00616C19 /* Sources */ = { 199 | isa = PBXSourcesBuildPhase; 200 | buildActionMask = 2147483647; 201 | files = ( 202 | 3AE6C5C91C4E4E8B00616C19 /* Safe.swift in Sources */, 203 | ); 204 | runOnlyForDeploymentPostprocessing = 0; 205 | }; 206 | 3AE6C5B41C4E4E7D00616C19 /* Sources */ = { 207 | isa = PBXSourcesBuildPhase; 208 | buildActionMask = 2147483647; 209 | files = ( 210 | 3AE6C5BE1C4E4E7D00616C19 /* SafeTests.swift in Sources */, 211 | ); 212 | runOnlyForDeploymentPostprocessing = 0; 213 | }; 214 | /* End PBXSourcesBuildPhase section */ 215 | 216 | /* Begin PBXTargetDependency section */ 217 | 3AE6C5BB1C4E4E7D00616C19 /* PBXTargetDependency */ = { 218 | isa = PBXTargetDependency; 219 | target = 3AE6C5AD1C4E4E7D00616C19 /* Safe */; 220 | targetProxy = 3AE6C5BA1C4E4E7D00616C19 /* PBXContainerItemProxy */; 221 | }; 222 | /* End PBXTargetDependency section */ 223 | 224 | /* Begin XCBuildConfiguration section */ 225 | 3AE6C5C01C4E4E7D00616C19 /* Debug */ = { 226 | isa = XCBuildConfiguration; 227 | buildSettings = { 228 | ALWAYS_SEARCH_USER_PATHS = NO; 229 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 230 | CLANG_CXX_LIBRARY = "libc++"; 231 | CLANG_ENABLE_MODULES = YES; 232 | CLANG_ENABLE_OBJC_ARC = YES; 233 | CLANG_WARN_BOOL_CONVERSION = YES; 234 | CLANG_WARN_CONSTANT_CONVERSION = YES; 235 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 236 | CLANG_WARN_EMPTY_BODY = YES; 237 | CLANG_WARN_ENUM_CONVERSION = YES; 238 | CLANG_WARN_INFINITE_RECURSION = YES; 239 | CLANG_WARN_INT_CONVERSION = YES; 240 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 241 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 242 | CLANG_WARN_UNREACHABLE_CODE = YES; 243 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 244 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 245 | COPY_PHASE_STRIP = NO; 246 | CURRENT_PROJECT_VERSION = 1; 247 | DEBUG_INFORMATION_FORMAT = dwarf; 248 | ENABLE_STRICT_OBJC_MSGSEND = YES; 249 | ENABLE_TESTABILITY = YES; 250 | GCC_C_LANGUAGE_STANDARD = gnu99; 251 | GCC_DYNAMIC_NO_PIC = NO; 252 | GCC_NO_COMMON_BLOCKS = YES; 253 | GCC_OPTIMIZATION_LEVEL = 0; 254 | GCC_PREPROCESSOR_DEFINITIONS = ( 255 | "DEBUG=1", 256 | "$(inherited)", 257 | ); 258 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 259 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 260 | GCC_WARN_UNDECLARED_SELECTOR = YES; 261 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 262 | GCC_WARN_UNUSED_FUNCTION = YES; 263 | GCC_WARN_UNUSED_VARIABLE = YES; 264 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 265 | MTL_ENABLE_DEBUG_INFO = YES; 266 | ONLY_ACTIVE_ARCH = YES; 267 | SDKROOT = iphoneos; 268 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 269 | TARGETED_DEVICE_FAMILY = "1,2"; 270 | VERSIONING_SYSTEM = "apple-generic"; 271 | VERSION_INFO_PREFIX = ""; 272 | }; 273 | name = Debug; 274 | }; 275 | 3AE6C5C11C4E4E7D00616C19 /* Release */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ALWAYS_SEARCH_USER_PATHS = NO; 279 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 280 | CLANG_CXX_LIBRARY = "libc++"; 281 | CLANG_ENABLE_MODULES = YES; 282 | CLANG_ENABLE_OBJC_ARC = YES; 283 | CLANG_WARN_BOOL_CONVERSION = YES; 284 | CLANG_WARN_CONSTANT_CONVERSION = YES; 285 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 286 | CLANG_WARN_EMPTY_BODY = YES; 287 | CLANG_WARN_ENUM_CONVERSION = YES; 288 | CLANG_WARN_INFINITE_RECURSION = YES; 289 | CLANG_WARN_INT_CONVERSION = YES; 290 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 291 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 292 | CLANG_WARN_UNREACHABLE_CODE = YES; 293 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 294 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 295 | COPY_PHASE_STRIP = NO; 296 | CURRENT_PROJECT_VERSION = 1; 297 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 298 | ENABLE_NS_ASSERTIONS = NO; 299 | ENABLE_STRICT_OBJC_MSGSEND = YES; 300 | GCC_C_LANGUAGE_STANDARD = gnu99; 301 | GCC_NO_COMMON_BLOCKS = YES; 302 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 303 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 304 | GCC_WARN_UNDECLARED_SELECTOR = YES; 305 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 306 | GCC_WARN_UNUSED_FUNCTION = YES; 307 | GCC_WARN_UNUSED_VARIABLE = YES; 308 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 309 | MTL_ENABLE_DEBUG_INFO = NO; 310 | SDKROOT = iphoneos; 311 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 312 | TARGETED_DEVICE_FAMILY = "1,2"; 313 | VALIDATE_PRODUCT = YES; 314 | VERSIONING_SYSTEM = "apple-generic"; 315 | VERSION_INFO_PREFIX = ""; 316 | }; 317 | name = Release; 318 | }; 319 | 3AE6C5C31C4E4E7D00616C19 /* Debug */ = { 320 | isa = XCBuildConfiguration; 321 | buildSettings = { 322 | CLANG_ENABLE_MODULES = YES; 323 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; 324 | DEFINES_MODULE = YES; 325 | DYLIB_COMPATIBILITY_VERSION = 1; 326 | DYLIB_CURRENT_VERSION = 1; 327 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 328 | INFOPLIST_FILE = Safe/Info.plist; 329 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 330 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 331 | PRODUCT_BUNDLE_IDENTIFIER = com.honzadvorsky.Safe; 332 | PRODUCT_NAME = "$(TARGET_NAME)"; 333 | SKIP_INSTALL = YES; 334 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 335 | SWIFT_VERSION = 3.0; 336 | }; 337 | name = Debug; 338 | }; 339 | 3AE6C5C41C4E4E7D00616C19 /* Release */ = { 340 | isa = XCBuildConfiguration; 341 | buildSettings = { 342 | CLANG_ENABLE_MODULES = YES; 343 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; 344 | DEFINES_MODULE = YES; 345 | DYLIB_COMPATIBILITY_VERSION = 1; 346 | DYLIB_CURRENT_VERSION = 1; 347 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 348 | INFOPLIST_FILE = Safe/Info.plist; 349 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 350 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 351 | PRODUCT_BUNDLE_IDENTIFIER = com.honzadvorsky.Safe; 352 | PRODUCT_NAME = "$(TARGET_NAME)"; 353 | SKIP_INSTALL = YES; 354 | SWIFT_VERSION = 3.0; 355 | }; 356 | name = Release; 357 | }; 358 | 3AE6C5C61C4E4E7D00616C19 /* Debug */ = { 359 | isa = XCBuildConfiguration; 360 | buildSettings = { 361 | INFOPLIST_FILE = SafeTests/Info.plist; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 363 | PRODUCT_BUNDLE_IDENTIFIER = com.honzadvorsky.SafeTests; 364 | PRODUCT_NAME = "$(TARGET_NAME)"; 365 | SWIFT_VERSION = 3.0; 366 | }; 367 | name = Debug; 368 | }; 369 | 3AE6C5C71C4E4E7D00616C19 /* Release */ = { 370 | isa = XCBuildConfiguration; 371 | buildSettings = { 372 | INFOPLIST_FILE = SafeTests/Info.plist; 373 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 374 | PRODUCT_BUNDLE_IDENTIFIER = com.honzadvorsky.SafeTests; 375 | PRODUCT_NAME = "$(TARGET_NAME)"; 376 | SWIFT_VERSION = 3.0; 377 | }; 378 | name = Release; 379 | }; 380 | /* End XCBuildConfiguration section */ 381 | 382 | /* Begin XCConfigurationList section */ 383 | 3AE6C5A81C4E4E7D00616C19 /* Build configuration list for PBXProject "Safe" */ = { 384 | isa = XCConfigurationList; 385 | buildConfigurations = ( 386 | 3AE6C5C01C4E4E7D00616C19 /* Debug */, 387 | 3AE6C5C11C4E4E7D00616C19 /* Release */, 388 | ); 389 | defaultConfigurationIsVisible = 0; 390 | defaultConfigurationName = Release; 391 | }; 392 | 3AE6C5C21C4E4E7D00616C19 /* Build configuration list for PBXNativeTarget "Safe" */ = { 393 | isa = XCConfigurationList; 394 | buildConfigurations = ( 395 | 3AE6C5C31C4E4E7D00616C19 /* Debug */, 396 | 3AE6C5C41C4E4E7D00616C19 /* Release */, 397 | ); 398 | defaultConfigurationIsVisible = 0; 399 | defaultConfigurationName = Release; 400 | }; 401 | 3AE6C5C51C4E4E7D00616C19 /* Build configuration list for PBXNativeTarget "SafeTests" */ = { 402 | isa = XCConfigurationList; 403 | buildConfigurations = ( 404 | 3AE6C5C61C4E4E7D00616C19 /* Debug */, 405 | 3AE6C5C71C4E4E7D00616C19 /* Release */, 406 | ); 407 | defaultConfigurationIsVisible = 0; 408 | defaultConfigurationName = Release; 409 | }; 410 | /* End XCConfigurationList section */ 411 | }; 412 | rootObject = 3AE6C5A51C4E4E7D00616C19 /* Project object */; 413 | } 414 | -------------------------------------------------------------------------------- /Safe.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Safe.xcodeproj/xcshareddata/xcschemes/Safe.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 65 | 71 | 72 | 73 | 74 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 94 | 95 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /Safe/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 0.1 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Safe/Safe.h: -------------------------------------------------------------------------------- 1 | // 2 | // Safe.h 3 | // Safe 4 | // 5 | // Created by Honza Dvorsky on 1/19/16. 6 | // Copyright © 2016 Honza Dvorsky. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Safe. 12 | FOUNDATION_EXPORT double SafeVersionNumber; 13 | 14 | //! Project version string for Safe. 15 | FOUNDATION_EXPORT const unsigned char SafeVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /Safe/Safe.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Safe.swift 3 | // Safe 4 | // 5 | // Created by Honza Dvorsky on 1/19/16. 6 | // Copyright © 2016 Honza Dvorsky. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public typealias SafeAccess = () -> () 12 | 13 | /** 14 | * Represents the safe-access object. Own it by your thread-safe 15 | * class and proxy all synchronizable calls through it. 16 | */ 17 | public protocol Safe { 18 | 19 | /** 20 | * Blocks calling thread until `access` can safely execute. 21 | */ 22 | func read(_ access: SafeAccess) 23 | 24 | /** 25 | * Returns immediately, `access` executes as soon as is safely possible. 26 | */ 27 | func write(_ access: @escaping SafeAccess) 28 | } 29 | 30 | let QueueName = "com.honzadvorsky.safe.queue" 31 | 32 | /** 33 | * Exclusive read, exclusive write. Only one thread can read 34 | * or write at one time. 35 | */ 36 | open class EREW: Safe { 37 | 38 | let queue: DispatchQueue 39 | 40 | public init(queue: DispatchQueue = DispatchQueue(label: QueueName, attributes: [])) { 41 | self.queue = queue 42 | } 43 | 44 | open func read(_ access: SafeAccess) { 45 | self.queue.sync(execute: access) 46 | } 47 | 48 | open func write(_ access: @escaping SafeAccess) { 49 | self.queue.async(execute: access) 50 | } 51 | } 52 | 53 | /** 54 | * Concurrent read, exclusive write. Only one thread can write or 55 | * multiple threads can read. Write waits for all previously-enqueued 56 | * reads to finish, executes, then reads can start again. Barrier-based. 57 | */ 58 | open class CREW: Safe { 59 | 60 | let queue: DispatchQueue 61 | 62 | public init(queue: DispatchQueue = DispatchQueue(label: QueueName, attributes: DispatchQueue.Attributes.concurrent)) { 63 | self.queue = queue 64 | } 65 | 66 | open func read(_ access: SafeAccess) { 67 | self.queue.sync(execute: access) 68 | } 69 | 70 | open func write(_ access: @escaping SafeAccess) { 71 | self.queue.async(flags: .barrier, execute: access) 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /SafeTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /SafeTests/SafeTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SafeTests.swift 3 | // SafeTests 4 | // 5 | // Created by Honza Dvorsky on 1/19/16. 6 | // Copyright © 2016 Honza Dvorsky. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import Safe 11 | 12 | func slp(_ ti: TimeInterval) { 13 | Thread.sleep(forTimeInterval: ti) 14 | } 15 | 16 | class SafeTests: XCTestCase { 17 | 18 | func testEREW() { 19 | 20 | var acc: [Int] = [] 21 | let read1 = { 22 | acc.append(1) 23 | slp(0.1) 24 | acc.append(2) 25 | } 26 | 27 | let read2 = { 28 | acc.append(3) 29 | slp(0.1) 30 | acc.append(4) 31 | } 32 | 33 | let write1 = { 34 | slp(0.001) 35 | acc.append(5) 36 | slp(0.1) 37 | acc.append(6) 38 | } 39 | 40 | let read3 = { 41 | slp(0.002) 42 | acc.append(7) 43 | slp(0.1) 44 | acc.append(8) 45 | } 46 | 47 | let exp = self.expectation(description: "end") 48 | var safe: Safe! = EREW() 49 | let helperQ = DispatchQueue(label: "test", attributes: DispatchQueue.Attributes.concurrent) 50 | helperQ.async { safe.read(read1) } 51 | helperQ.async { safe.read(read2) } 52 | helperQ.async { safe.write(write1) } 53 | helperQ.async { safe.read(read3) } 54 | 55 | helperQ.async(flags: .barrier, execute: { 56 | 57 | safe.write { 58 | slp(0.1) 59 | safe = nil 60 | let expAcc = [1, 2, 3, 4, 5, 6, 7, 8] 61 | XCTAssertEqual(expAcc, acc) 62 | exp.fulfill() 63 | } 64 | }) 65 | self.waitForExpectations(timeout: 10, handler: nil) 66 | } 67 | 68 | func testCREW() { 69 | 70 | var acc: [Int] = [] 71 | let read1 = { 72 | acc.append(1) 73 | slp(0.1) 74 | acc.append(2) 75 | } 76 | 77 | let read2 = { 78 | slp(0.001) 79 | acc.append(3) 80 | slp(0.1) 81 | acc.append(4) 82 | } 83 | 84 | let write1 = { 85 | slp(0.002) 86 | acc.append(5) 87 | slp(0.1) 88 | acc.append(6) 89 | } 90 | 91 | let read3 = { 92 | slp(0.003) 93 | acc.append(7) 94 | slp(0.1) 95 | acc.append(8) 96 | } 97 | 98 | let exp = self.expectation(description: "end") 99 | var safe: Safe! = CREW() 100 | let helperQ = DispatchQueue(label: "test", attributes: DispatchQueue.Attributes.concurrent) 101 | helperQ.async { safe.read(read1) } 102 | helperQ.async { safe.read(read2) } 103 | helperQ.async { safe.write(write1) } 104 | helperQ.async { safe.read(read3) } 105 | 106 | helperQ.async(flags: .barrier, execute: { 107 | 108 | safe.write { 109 | slp(0.1) 110 | safe = nil 111 | 112 | print(acc) 113 | 114 | //the first two reads should be intertwined 115 | let expReadFirsts: Set = [1, 3] 116 | let readFirsts = Set(acc.prefix(2)) 117 | XCTAssertEqual(expReadFirsts, readFirsts) 118 | 119 | let expReadSeconds: Set = [2, 4] 120 | let prefixed = acc.prefix(4) 121 | let readSeconds = Set(prefixed.suffix(2)) 122 | XCTAssertEqual(expReadSeconds, readSeconds) 123 | 124 | let expAccTail = [5, 6, 7, 8] 125 | let accTail = Array(acc.dropFirst(4)) 126 | XCTAssertEqual(expAccTail, accTail) 127 | 128 | exp.fulfill() 129 | } 130 | }) 131 | self.waitForExpectations(timeout: 50, handler: nil) 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /SwiftSafe.podspec: -------------------------------------------------------------------------------- 1 | 2 | Pod::Spec.new do |s| 3 | 4 | s.name = "SwiftSafe" 5 | s.version = "2.0.0" 6 | s.summary = "Thread synchronization made easy." 7 | 8 | s.homepage = "https://github.com/nodes-ios/SwiftSafe" 9 | s.license = { :type => "MIT", :file => "LICENSE" } 10 | 11 | s.author = { "Honza Dvorsky" => "https://honzadvorsky.com" } 12 | # s.social_media_url = "https://twitter.com/czechboy0" 13 | 14 | s.ios.deployment_target = "8.0" 15 | s.osx.deployment_target = "10.10" 16 | s.watchos.deployment_target = "2.0" 17 | s.tvos.deployment_target = "9.0" 18 | 19 | s.source = { :git => "https://github.com/nodes-ios/SwiftSafe.git", :tag => "#{s.version}" } 20 | 21 | s.source_files = "Safe/*.swift" 22 | 23 | end 24 | --------------------------------------------------------------------------------