mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
Add support to obtain the current EventLoop (#120)
Motivation: In certain cases it would be useful to be able to access the current EventLoop (which is tied to a thread). Modifications: - Add MultiThreadEventLoopGroup.currentEventLoop - Add tests Result: Fixes [#59]
This commit is contained in:
1 parent
73e89833fc
commit
c40c6e1222
3 files changed
+61
No files matched your search
@@ -641,6 +641,8 @@ typealias ThreadInitializer = (Thread) -> Void
|
||||
/// An `EventLoopGroup` which will create multiple `EventLoop`s, each tied to its own `Thread`.
|
||||
final public class MultiThreadedEventLoopGroup: EventLoopGroup {
|
||||
|
||||
private static let threadSpecificEventLoop = ThreadSpecificVariable<SelectableEventLoop>()
|
||||
|
||||
private let index = Atomic<Int>(value: 0)
|
||||
private let eventLoops: [SelectableEventLoop]
|
||||
|
||||
@@ -660,6 +662,10 @@ final public class MultiThreadedEventLoopGroup: EventLoopGroup {
|
||||
do {
|
||||
/* we try! this as this must work (just setting up kqueue/epoll) or else there's not much we can do here */
|
||||
let l = try! SelectableEventLoop(thread: t)
|
||||
threadSpecificEventLoop.currentValue = l
|
||||
defer {
|
||||
threadSpecificEventLoop.currentValue = nil
|
||||
}
|
||||
lock.withLock {
|
||||
_loop = l
|
||||
}
|
||||
@@ -699,6 +705,13 @@ final public class MultiThreadedEventLoopGroup: EventLoopGroup {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `EventLoop` for the calling thread.
|
||||
///
|
||||
/// - returns: The current `EventLoop` for the calling thread or `nil` if none is assigned to the thread.
|
||||
public static var currentEventLoop: EventLoop? {
|
||||
return threadSpecificEventLoop.currentValue
|
||||
}
|
||||
|
||||
public func next() -> EventLoop {
|
||||
return eventLoops[abs(index.add(1) % eventLoops.count)]
|
||||
}
|
||||
@@ -729,6 +742,9 @@ final public class MultiThreadedEventLoopGroup: EventLoopGroup {
|
||||
|
||||
g.notify(queue: q) {
|
||||
let failure = self.eventLoops.map { try? $0.close0() }.filter { $0 == nil }.count > 0
|
||||
|
||||
// TODO: In the next major release we should join in the Thread used by the EventLoop before invoking the callback to ensure
|
||||
// it is really gone.
|
||||
if failure {
|
||||
error = EventLoopError.shutdownFailed
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ extension EventLoopTest {
|
||||
("testEventLoopThreads", testEventLoopThreads),
|
||||
("testEventLoopPinned", testEventLoopPinned),
|
||||
("testEventLoopPinnedCPUIdsConstructor", testEventLoopPinnedCPUIdsConstructor),
|
||||
("testCurrentEventLoop", testCurrentEventLoop),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,4 +228,48 @@ public class EventLoopTest : XCTestCase {
|
||||
XCTAssertNoThrow(try group.syncShutdownGracefully())
|
||||
#endif
|
||||
}
|
||||
|
||||
public func testCurrentEventLoop() throws {
|
||||
class EventLoopHolder {
|
||||
weak var loop: EventLoop?
|
||||
init(_ loop: EventLoop) {
|
||||
self.loop = loop
|
||||
}
|
||||
}
|
||||
|
||||
func assertCurrentEventLoop0() throws -> EventLoopHolder {
|
||||
let group = MultiThreadedEventLoopGroup(numThreads: 2)
|
||||
|
||||
let loop1 = group.next()
|
||||
let currentLoop1 = try loop1.submit {
|
||||
MultiThreadedEventLoopGroup.currentEventLoop
|
||||
}.wait()
|
||||
XCTAssertTrue(loop1 === currentLoop1)
|
||||
|
||||
let loop2 = group.next()
|
||||
let currentLoop2 = try loop2.submit {
|
||||
MultiThreadedEventLoopGroup.currentEventLoop
|
||||
}.wait()
|
||||
XCTAssertTrue(loop2 === currentLoop2)
|
||||
XCTAssertFalse(loop1 === loop2)
|
||||
|
||||
let holder = EventLoopHolder(loop2)
|
||||
XCTAssertNotNil(holder.loop)
|
||||
XCTAssertNil(MultiThreadedEventLoopGroup.currentEventLoop)
|
||||
XCTAssertNoThrow(try group.syncShutdownGracefully())
|
||||
return holder
|
||||
}
|
||||
|
||||
let holder = try assertCurrentEventLoop0()
|
||||
|
||||
// We loop as the Thread used by SelectableEventLoop may not be gone yet.
|
||||
// In the next major version we should ensure to join all threads and so be sure all are gone when
|
||||
// syncShutdownGracefully returned.
|
||||
var tries = 0
|
||||
while holder.loop != nil {
|
||||
XCTAssertTrue(tries < 5, "Reference to EventLoop still alive after 5 seconds")
|
||||
sleep(1)
|
||||
tries += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user