Skip to content

Commit 2c332d0

Browse files
committed
Initial Package Commit
Added the `ThreadSafeSemaphore` Property Wrapper
1 parent 6602f29 commit 2c332d0

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ fastlane/test_output
8888
# https://github.com/johnno1962/injectionforxcode
8989

9090
iOSInjectionProject/
91+
.DS_Store

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version: 5.6
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "ThreadSafeSwift",
8+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "ThreadSafeSwift",
12+
targets: ["ThreadSafeSwift"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "ThreadSafeSwift",
23+
dependencies: []),
24+
.testTarget(
25+
name: "ThreadSafeSwiftTests",
26+
dependencies: ["ThreadSafeSwift"]),
27+
]
28+
)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ThreadSafeSwift
2+
3+
A description of this package.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// ThreadSafeSemaphore.swift
3+
// Copyright (c) 2022, Flowduino
4+
// Authored by Simon J. Stuart on 8th July 2022
5+
//
6+
// Subject to terms, restrictions, and liability waiver of the MIT License
7+
//
8+
import Foundation
9+
10+
/**
11+
Enforces a `DispatchSemaphore` Lock against the given Value Type to ensure single-threaded access.
12+
- Author: Simon J. Stuart
13+
- Version: 1.0
14+
*/
15+
@propertyWrapper
16+
public struct ThreadSafeSemaphore<T> {
17+
public var lock = DispatchSemaphore(value: 1)
18+
private var value: T
19+
20+
public var wrappedValue: T {
21+
get {
22+
lock.wait()
23+
let result = value
24+
lock.signal()
25+
return result
26+
}
27+
set {
28+
lock.wait()
29+
value = newValue
30+
lock.signal()
31+
}
32+
}
33+
34+
public init(wrappedValue: T) {
35+
lock.wait()
36+
value = wrappedValue
37+
lock.signal()
38+
}
39+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import XCTest
2+
@testable import ThreadSafeSwift
3+
4+
final class ThreadSafeSwiftTests: XCTestCase {
5+
func testExample() throws {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
// XCTAssertEqual(ThreadSafeSwift().text, "Hello, World!")
10+
}
11+
}

0 commit comments

Comments
 (0)