Skip to content

Commit

Permalink
- minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Belozierov committed Apr 4, 2020
1 parent cf9eb93 commit 989a95b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 57 deletions.
10 changes: 0 additions & 10 deletions Sources/SwiftCoroutine/Coroutine/Coroutine/Coroutine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ import Dispatch
///
public struct Coroutine {

@inlinable static func current() throws -> CoroutineProtocol {
if let coroutine = ThreadCoroutineWrapper.current.coroutine { return coroutine }
throw CoroutineError.mustBeCalledInsideCoroutine
}

/// Returns `true` if this property is called inside a coroutine.
@inlinable public static var isInsideCoroutine: Bool {
ThreadCoroutineWrapper.current.coroutine != nil
}

// MARK: - await

/// Suspends a coroutine поки не буде викликаний callback.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// Copyright © 2020 Alex Belozierov. All rights reserved.
//

#if os(Linux)
import Glibc
#else
import Darwin
#endif

@usableFromInline protocol CoroutineProtocol: class {

typealias StackSize = Coroutine.StackSize
Expand All @@ -17,12 +23,46 @@

extension CoroutineProtocol {

@inlinable func performAsCurrent<T>(_ block: () -> T) -> T {
let wrapper = ThreadCoroutineWrapper.current
let caller = wrapper.coroutine
wrapper.coroutine = self
defer { wrapper.coroutine = caller }
return block()
@inlinable internal func performAsCurrent<T>(_ block: () -> T) -> T {
let unmanaged = Unmanaged.passRetained(self)
defer { unmanaged.release() }
if let caller = pthread_getspecific(.coroutine) {
pthread_setspecific(.coroutine, unmanaged.toOpaque())
defer { pthread_setspecific(.coroutine, caller) }
return block()
} else {
pthread_setspecific(.coroutine, unmanaged.toOpaque())
defer { pthread_setspecific(.coroutine, nil) }
return block()
}
}

}

extension Coroutine {

/// Returns `true` if this property is called inside a coroutine.
@inlinable public static var isInsideCoroutine: Bool {
pthread_getspecific(.coroutine) != nil
}

@inlinable static func current() throws -> CoroutineProtocol {
guard let pointer = pthread_getspecific(.coroutine)
else { throw CoroutineError.mustBeCalledInsideCoroutine }
return Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as! CoroutineProtocol
}

}

extension pthread_key_t {

@usableFromInline internal static let coroutine: pthread_key_t = {
let key = UnsafeMutablePointer<pthread_key_t>.allocate(capacity: 1)
pthread_key_create(key, nil)
defer { key.deallocate() }
return key.pointee
}()

}


This file was deleted.

6 changes: 0 additions & 6 deletions SwiftCoroutine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
1A363CBD2413D004000CF996 /* StackfullCoroutine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CBB2413D004000CF996 /* StackfullCoroutine.swift */; };
1A363CBF2413D03B000CF996 /* CoroutineProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */; };
1A363CC02413D03B000CF996 /* CoroutineProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */; };
1A363CC22413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */; };
1A363CC32413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */; };
1A43084523C094EC001A89EA /* DispatchSourceTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A43084423C094EC001A89EA /* DispatchSourceTimer.swift */; };
1A43084623C094EC001A89EA /* DispatchSourceTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A43084423C094EC001A89EA /* DispatchSourceTimer.swift */; };
1A47C25023D73D3F004CC7B1 /* CoroutineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A47C24F23D73D3F004CC7B1 /* CoroutineTests.swift */; };
Expand Down Expand Up @@ -135,7 +133,6 @@
1A3583FE23DD796A0086A6E6 /* CoFuture1+whenComplete.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoFuture1+whenComplete.swift"; sourceTree = "<group>"; };
1A363CBB2413D004000CF996 /* StackfullCoroutine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackfullCoroutine.swift; sourceTree = "<group>"; };
1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoroutineProtocol.swift; sourceTree = "<group>"; };
1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadCoroutineWrapper.swift; sourceTree = "<group>"; };
1A43084423C094EC001A89EA /* DispatchSourceTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DispatchSourceTimer.swift; sourceTree = "<group>"; };
1A47C24F23D73D3F004CC7B1 /* CoroutineTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoroutineTests.swift; sourceTree = "<group>"; };
1A4C6DAC2416AB2C00EF2974 /* FifoQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FifoQueue.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -294,7 +291,6 @@
1A599E51241ACD7B007E744F /* CoroutineProtocol */ = {
isa = PBXGroup;
children = (
1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */,
1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */,
);
path = CoroutineProtocol;
Expand Down Expand Up @@ -694,7 +690,6 @@
1AFFF6ED23C8BDEA0079FF73 /* CoFutureError.swift in Sources */,
1A8129A52417D49900AE061C /* CoroutineDispatcher.swift in Sources */,
F8CD25E92019A01600952299 /* CCoroutine.c in Sources */,
1A363CC22413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */,
1A43084523C094EC001A89EA /* DispatchSourceTimer.swift in Sources */,
1A790E8423E5D7A9007056EC /* Coroutine.swift in Sources */,
1A65BABB239CE05D004C1716 /* CoroutineContext.swift in Sources */,
Expand Down Expand Up @@ -755,7 +750,6 @@
1A65BAC2239CEFED004C1716 /* CoroutineContext.swift in Sources */,
1A8129A62417D49900AE061C /* CoroutineDispatcher.swift in Sources */,
F8CD25ED2019A0F800952299 /* CCoroutine.c in Sources */,
1A363CC32413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */,
1A790E8523E5D7A9007056EC /* Coroutine.swift in Sources */,
1A43084623C094EC001A89EA /* DispatchSourceTimer.swift in Sources */,
1A9C14DF23BB824400E13203 /* CoFuture2+await.swift in Sources */,
Expand Down

0 comments on commit 989a95b

Please sign in to comment.