-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathProcess.swift
More file actions
40 lines (34 loc) · 976 Bytes
/
Process.swift
File metadata and controls
40 lines (34 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// Process
// Swifter
//
// Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
//
import Foundation
public class Process {
public static var pid: Int {
return Int(getpid())
}
public static var tid: UInt64 {
#if os(Linux)
return UInt64(pthread_self())
#else
var tid: __uint64_t = 0
pthread_threadid_np(nil, &tid)
return UInt64(tid)
#endif
}
private static var signalsWatchers = [(Int32) -> Void]()
private static var signalsObserved = false
public static func watchSignals(_ callback: @escaping (Int32) -> Void) {
if !signalsObserved {
[SIGTERM, SIGHUP, SIGSTOP, SIGINT].forEach { item in
signal(item) { signum in
Process.signalsWatchers.forEach { $0(signum) }
}
}
signalsObserved = true
}
signalsWatchers.append(callback)
}
}