Process.swift 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // Process
  3. // Swifter
  4. //
  5. // Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
  6. //
  7. #if os(Linux)
  8. import Glibc
  9. #else
  10. import Foundation
  11. #endif
  12. public class Process {
  13. public static var PID: Int {
  14. return Int(getpid())
  15. }
  16. public static var TID: UInt64 {
  17. #if os(Linux)
  18. return UInt64(pthread_self())
  19. #else
  20. var tid: __uint64_t = 0
  21. pthread_threadid_np(nil, &tid);
  22. return UInt64(tid)
  23. #endif
  24. }
  25. private static var signalsWatchers = Array<(Int32) -> Void>()
  26. private static var signalsObserved = false
  27. public static func watchSignals(callback: (Int32) -> Void) {
  28. if !signalsObserved {
  29. [SIGTERM, SIGHUP, SIGSTOP, SIGINT].forEach { item in
  30. signal(item) {
  31. signum in Process.signalsWatchers.forEach { $0(signum) }
  32. }
  33. }
  34. signalsObserved = true
  35. }
  36. signalsWatchers.append(callback)
  37. }
  38. }