Skip to content

Commit

Permalink
Cleaned up some stuff formatting, simplified pointer operations, and …
Browse files Browse the repository at this point in the history
…added a concurrent test
  • Loading branch information
bscothern committed Jan 7, 2021
1 parent 9fe4690 commit 22f4206
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 200 deletions.
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017-2019 Braden Scothern.
Copyright (c) 2017-2020 Braden Scothern.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
40 changes: 9 additions & 31 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
// swift-tools-version:4.0
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
// Package.swift
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017-2019 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import PackageDescription

Expand All @@ -35,7 +8,8 @@ let package = Package(
products: [
.library(
name: "Once",
targets: ["OnceC", "Once"]),
targets: ["Once"]
),
],
dependencies: [
],
Expand All @@ -47,12 +21,16 @@ let package = Package(
),
.target(
name: "Once",
dependencies: ["OnceC"],
dependencies: [
.target(name: "OnceC")
],
path: "Sources/Swift"
),
.testTarget(
name: "OnceTests",
dependencies: ["Once"]
dependencies: [
.target(name: "Once")
]
),
]
)
31 changes: 31 additions & 0 deletions Package@swift-4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Once",
products: [
.library(
name: "Once",
targets: ["OnceC", "Once"]),
],
dependencies: [
],
targets: [
.target(
name: "OnceC",
dependencies: [],
path: "Sources/OnceC"
),
.target(
name: "Once",
dependencies: ["OnceC"],
path: "Sources/Swift"
),
.testTarget(
name: "OnceTests",
dependencies: ["Once"]
),
]
)
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# Once

[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/Carthage/Carthage/master/LICENSE.md) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)[![SPM](https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat)](https://swift.org/package-manager)
[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/Carthage/Carthage/master/LICENSE.md) [![SPM](https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat)](https://swift.org/package-manager) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)

A simple thread safe replacement for Dispatch Once and pthread_once for Swift.

## Carthage Usage
## Swift Package Manager
Update your `Package.swift` to include the appropriate dependency below:

Include this line in your `Cartfile`:
### Swift 5.2+
```swift
.package(name: "Once", "https://github.com/bscothern/Once.git", from: "1.4.0")
```
github "bscothern/Once"

### Swift 4.0-5.1
```swift
.package(url: "https://github.com/bscothern/Once.git", from: "1.3.2")
```

## Swift Package Manager
Update your `Package.swift` to include this to your package dependencies:
## Carthage Usage
Include this line in your `Cartfile`:
```
.package(url: "https://github.com/bscothern/Once.git", from: "1.3.2")
github "bscothern/Once"
```

## Usage
```
```swift
import Once

let once = Once()
Expand Down
22 changes: 1 addition & 21 deletions Sources/Once.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017-2019 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Copyright © 2017-2020 Braden Scothern. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
22 changes: 1 addition & 21 deletions Sources/OnceC/OnceC.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017-2019 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Copyright © 2017-2020 Braden Scothern. All rights reserved.
//

#include <stdlib.h>
Expand Down
22 changes: 1 addition & 21 deletions Sources/OnceC/include/OnceC.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017-2019 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Copyright © 2017-2020 Braden Scothern. All rights reserved.
//

#ifndef OnceC_h
Expand Down
45 changes: 15 additions & 30 deletions Sources/Swift/Once.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017-2019 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Copyright © 2017-2020 Braden Scothern. All rights reserved.
//

#if SWIFT_PACKAGE
Expand All @@ -45,30 +25,34 @@ public class Once {
public typealias Block = () -> Void

//MARK:- Properties
//MARK: Private Static
//MARK: Internal Static

/// The helper funciton that is used to bridge the C and Swift functions in order to allow calling of the `Block`.
///
/// This is needed in order to allow the `Block` to have context since C function pointers cannot keep swift context.
private static let runner: OnceBlock = {
UnsafePointer<Block>(OpaquePointer(OnceGetContextPointer()))?.pointee()
@usableFromInline
internal static let runner: OnceBlock = {
OnceGetContextPointer()?.bindMemory(to: Block.self, capacity: 1).pointee()
}

//MARK: Private
//MARK: Internal

// UnsafeMutablePointer must be used here to ensure that memory issues don't occur.
// Follow the thread from this post for more information: https://forums.swift.org/t/atomic-property-wrapper-for-standard-library/30468/15
private var onceC: UnsafeMutablePointer<OnceC>
@usableFromInline
internal var onceC: UnsafeMutablePointer<OnceC>

//MARK:- Init
//MARK: Public

/// Create a `Once` that ensurse a block only executes once.
@inlinable
public init() {
onceC = .allocate(capacity: 1)
onceC.initialize(to: OnceCCreate())
onceC.initialize(to: OnceCCreate())
}


@inlinable
deinit {
onceC.deinitialize(count: 1)
onceC.deallocate()
Expand All @@ -80,6 +64,7 @@ public class Once {
/// The funciton that runs the given `Block` if the `Once` hasn't already executed.
///
/// - Parameter block: The `Block` that should be executed once.
@inlinable
public func runOnce(_ block: Block) {
withoutActuallyEscaping(block) { block in
var block = block
Expand Down
22 changes: 1 addition & 21 deletions Sources/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Copyright © 2017-2020 Braden Scothern. All rights reserved.
//

framework module Once {
Expand Down
22 changes: 1 addition & 21 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
// Once
//
// Created by Braden Scothern on 10/11/17.
// Copyright © 2017-2019 Braden Scothern. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Copyright © 2017-2020 Braden Scothern. All rights reserved.
//

import XCTest
Expand Down
Loading

0 comments on commit 22f4206

Please sign in to comment.