Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 989a95b

Browse files
author
Alex Belozierov
committed
- minor improvements
1 parent cf9eb93 commit 989a95b

File tree

4 files changed

+46
-57
lines changed

4 files changed

+46
-57
lines changed

Sources/SwiftCoroutine/Coroutine/Coroutine/Coroutine.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ import Dispatch
1515
///
1616
public struct Coroutine {
1717

18-
@inlinable static func current() throws -> CoroutineProtocol {
19-
if let coroutine = ThreadCoroutineWrapper.current.coroutine { return coroutine }
20-
throw CoroutineError.mustBeCalledInsideCoroutine
21-
}
22-
23-
/// Returns `true` if this property is called inside a coroutine.
24-
@inlinable public static var isInsideCoroutine: Bool {
25-
ThreadCoroutineWrapper.current.coroutine != nil
26-
}
27-
2818
// MARK: - await
2919

3020
/// Suspends a coroutine поки не буде викликаний callback.

Sources/SwiftCoroutine/Coroutine/CoroutineProtocol/CoroutineProtocol.swift

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
// Copyright © 2020 Alex Belozierov. All rights reserved.
77
//
88

9+
#if os(Linux)
10+
import Glibc
11+
#else
12+
import Darwin
13+
#endif
14+
915
@usableFromInline protocol CoroutineProtocol: class {
1016

1117
typealias StackSize = Coroutine.StackSize
@@ -17,12 +23,46 @@
1723

1824
extension CoroutineProtocol {
1925

20-
@inlinable func performAsCurrent<T>(_ block: () -> T) -> T {
21-
let wrapper = ThreadCoroutineWrapper.current
22-
let caller = wrapper.coroutine
23-
wrapper.coroutine = self
24-
defer { wrapper.coroutine = caller }
25-
return block()
26+
@inlinable internal func performAsCurrent<T>(_ block: () -> T) -> T {
27+
let unmanaged = Unmanaged.passRetained(self)
28+
defer { unmanaged.release() }
29+
if let caller = pthread_getspecific(.coroutine) {
30+
pthread_setspecific(.coroutine, unmanaged.toOpaque())
31+
defer { pthread_setspecific(.coroutine, caller) }
32+
return block()
33+
} else {
34+
pthread_setspecific(.coroutine, unmanaged.toOpaque())
35+
defer { pthread_setspecific(.coroutine, nil) }
36+
return block()
37+
}
38+
}
39+
40+
}
41+
42+
extension Coroutine {
43+
44+
/// Returns `true` if this property is called inside a coroutine.
45+
@inlinable public static var isInsideCoroutine: Bool {
46+
pthread_getspecific(.coroutine) != nil
47+
}
48+
49+
@inlinable static func current() throws -> CoroutineProtocol {
50+
guard let pointer = pthread_getspecific(.coroutine)
51+
else { throw CoroutineError.mustBeCalledInsideCoroutine }
52+
return Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as! CoroutineProtocol
2653
}
2754

2855
}
56+
57+
extension pthread_key_t {
58+
59+
@usableFromInline internal static let coroutine: pthread_key_t = {
60+
let key = UnsafeMutablePointer<pthread_key_t>.allocate(capacity: 1)
61+
pthread_key_create(key, nil)
62+
defer { key.deallocate() }
63+
return key.pointee
64+
}()
65+
66+
}
67+
68+

Sources/SwiftCoroutine/Coroutine/CoroutineProtocol/ThreadCoroutineWrapper.swift

Lines changed: 0 additions & 35 deletions
This file was deleted.

SwiftCoroutine.xcodeproj/project.pbxproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
1A363CBD2413D004000CF996 /* StackfullCoroutine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CBB2413D004000CF996 /* StackfullCoroutine.swift */; };
2828
1A363CBF2413D03B000CF996 /* CoroutineProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */; };
2929
1A363CC02413D03B000CF996 /* CoroutineProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */; };
30-
1A363CC22413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */; };
31-
1A363CC32413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */; };
3230
1A43084523C094EC001A89EA /* DispatchSourceTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A43084423C094EC001A89EA /* DispatchSourceTimer.swift */; };
3331
1A43084623C094EC001A89EA /* DispatchSourceTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A43084423C094EC001A89EA /* DispatchSourceTimer.swift */; };
3432
1A47C25023D73D3F004CC7B1 /* CoroutineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A47C24F23D73D3F004CC7B1 /* CoroutineTests.swift */; };
@@ -135,7 +133,6 @@
135133
1A3583FE23DD796A0086A6E6 /* CoFuture1+whenComplete.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoFuture1+whenComplete.swift"; sourceTree = "<group>"; };
136134
1A363CBB2413D004000CF996 /* StackfullCoroutine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackfullCoroutine.swift; sourceTree = "<group>"; };
137135
1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoroutineProtocol.swift; sourceTree = "<group>"; };
138-
1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadCoroutineWrapper.swift; sourceTree = "<group>"; };
139136
1A43084423C094EC001A89EA /* DispatchSourceTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DispatchSourceTimer.swift; sourceTree = "<group>"; };
140137
1A47C24F23D73D3F004CC7B1 /* CoroutineTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoroutineTests.swift; sourceTree = "<group>"; };
141138
1A4C6DAC2416AB2C00EF2974 /* FifoQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FifoQueue.swift; sourceTree = "<group>"; };
@@ -294,7 +291,6 @@
294291
1A599E51241ACD7B007E744F /* CoroutineProtocol */ = {
295292
isa = PBXGroup;
296293
children = (
297-
1A363CC12413D077000CF996 /* ThreadCoroutineWrapper.swift */,
298294
1A363CBE2413D03B000CF996 /* CoroutineProtocol.swift */,
299295
);
300296
path = CoroutineProtocol;
@@ -694,7 +690,6 @@
694690
1AFFF6ED23C8BDEA0079FF73 /* CoFutureError.swift in Sources */,
695691
1A8129A52417D49900AE061C /* CoroutineDispatcher.swift in Sources */,
696692
F8CD25E92019A01600952299 /* CCoroutine.c in Sources */,
697-
1A363CC22413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */,
698693
1A43084523C094EC001A89EA /* DispatchSourceTimer.swift in Sources */,
699694
1A790E8423E5D7A9007056EC /* Coroutine.swift in Sources */,
700695
1A65BABB239CE05D004C1716 /* CoroutineContext.swift in Sources */,
@@ -755,7 +750,6 @@
755750
1A65BAC2239CEFED004C1716 /* CoroutineContext.swift in Sources */,
756751
1A8129A62417D49900AE061C /* CoroutineDispatcher.swift in Sources */,
757752
F8CD25ED2019A0F800952299 /* CCoroutine.c in Sources */,
758-
1A363CC32413D077000CF996 /* ThreadCoroutineWrapper.swift in Sources */,
759753
1A790E8523E5D7A9007056EC /* Coroutine.swift in Sources */,
760754
1A43084623C094EC001A89EA /* DispatchSourceTimer.swift in Sources */,
761755
1A9C14DF23BB824400E13203 /* CoFuture2+await.swift in Sources */,

0 commit comments

Comments
 (0)