-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from noppoMan/handle-signals
Handle signals
- Loading branch information
Showing
6 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// EventEmitter.swift | ||
// HexavillePackageDescription | ||
// | ||
// Created by Yuki Takei on 2017/10/17. | ||
// | ||
|
||
import Foundation | ||
import Prorsum | ||
|
||
/** | ||
Thread safety EventEmitter | ||
*/ | ||
public class EventEmitter<T> { | ||
|
||
private var mutex = Mutex() | ||
|
||
public var onceListenersCount: Int { | ||
return onceListeners.count | ||
} | ||
|
||
public var onListenersCount: Int { | ||
return onListeners.count | ||
} | ||
|
||
private var onceListeners: [(T) -> Void] = [] | ||
|
||
private var onListeners: [(T) -> Void] = [] | ||
|
||
public init() {} | ||
|
||
public func emit(with value: T) { | ||
defer { | ||
mutex.lock() | ||
onceListeners.removeAll() | ||
mutex.unlock() | ||
} | ||
|
||
for handle in onceListeners { | ||
handle(value) | ||
} | ||
|
||
for handle in onListeners { | ||
handle(value) | ||
} | ||
} | ||
|
||
public func once(handler: @escaping (T) -> Void) { | ||
defer { | ||
mutex.unlock() | ||
} | ||
mutex.lock() | ||
onceListeners.append(handler) | ||
} | ||
|
||
public func on(handler: @escaping (T) -> Void) { | ||
defer { | ||
mutex.unlock() | ||
} | ||
mutex.lock() | ||
onListeners.append(handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// SignalHandler.swift | ||
// HexavillePackageDescription | ||
// | ||
// Created by Yuki Takei on 2017/10/17. | ||
// | ||
#if os(OSX) | ||
import Darwin | ||
#else | ||
import Glibc | ||
#endif | ||
|
||
import Foundation | ||
import Dispatch | ||
|
||
public class SignalEventEmitter: EventEmitter<SignalHandler.Signal> { | ||
public static let shared = SignalEventEmitter() | ||
} | ||
|
||
public class SignalHandler { | ||
|
||
public enum Signal: Int32 { | ||
case hup = 1 | ||
case int = 2 | ||
case quit = 3 | ||
case abrt = 6 | ||
case kill = 9 | ||
case alrm = 14 | ||
case term = 15 | ||
} | ||
|
||
public static let shared = SignalHandler() | ||
|
||
private var handlers: [() -> Void] = [] | ||
|
||
private init() {} | ||
|
||
public func trap(with signal: Signal, handler: @escaping () -> Void) { | ||
handlers.append(handler) | ||
|
||
let action: @convention(c) (Int32) -> () = { sig in | ||
for handle in SignalHandler.shared.handlers { | ||
handle() | ||
} | ||
} | ||
|
||
#if os(macOS) | ||
var signalAction = sigaction( | ||
__sigaction_u: unsafeBitCast(action, to: __sigaction_u.self), | ||
sa_mask: 0, | ||
sa_flags: 0 | ||
) | ||
|
||
_ = withUnsafePointer(to: &signalAction) { actionPointer in | ||
sigaction(signal.rawValue, actionPointer, nil) | ||
} | ||
|
||
#elseif os(Linux) | ||
var sigAction = sigaction() | ||
sigAction.__sigaction_handler = unsafeBitCast( | ||
action, | ||
to: sigaction.__Unnamed_union___sigaction_handler.self | ||
) | ||
_ = sigaction(signal.rawValue, &sigAction, nil) | ||
#endif | ||
} | ||
|
||
public func raise(_ signal: Signal) { | ||
#if os(macOS) | ||
_ = Darwin.raise(signal.rawValue) | ||
#elseif os(Linux) | ||
_ = Glibc.raise(signal.rawValue) | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// EventEmitterTests.swift | ||
// HexavillePackageDescription | ||
// | ||
// Created by Yuki Takei on 2017/10/17. | ||
// | ||
|
||
import Foundation | ||
|
||
import XCTest | ||
@testable import HexavilleCore | ||
|
||
class EventEmitterTests: XCTestCase { | ||
|
||
func testOn() { | ||
let exp = expectation(description: "testOnce") | ||
|
||
let evm = EventEmitter<Int>() | ||
|
||
evm.on { | ||
XCTAssertEqual($0 + 1, 2) | ||
exp.fulfill() | ||
} | ||
|
||
evm.emit(with: 1) | ||
|
||
waitForExpectations(timeout: 1, handler: nil) | ||
|
||
XCTAssertEqual(evm.onListenersCount, 1) | ||
} | ||
|
||
func testOnce() { | ||
let exp = expectation(description: "testOnce") | ||
|
||
let evm = EventEmitter<Int>() | ||
|
||
evm.once { | ||
XCTAssertEqual($0 + 1, 2) | ||
exp.fulfill() | ||
} | ||
|
||
evm.emit(with: 1) | ||
|
||
waitForExpectations(timeout: 1, handler: nil) | ||
|
||
XCTAssertEqual(evm.onceListenersCount, 0) | ||
} | ||
|
||
static var allTests = [ | ||
("testOn", testOn), | ||
("testOnce", testOnce), | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters