You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1005 B

5 years ago
  1. //
  2. // Platform.Darwin.swift
  3. // Platform
  4. //
  5. // Created by Krunoslav Zaher on 12/29/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
  9. import Darwin
  10. import class Foundation.Thread
  11. import protocol Foundation.NSCopying
  12. extension Thread {
  13. static func setThreadLocalStorageValue<T: AnyObject>(_ value: T?, forKey key: NSCopying) {
  14. let currentThread = Thread.current
  15. let threadDictionary = currentThread.threadDictionary
  16. if let newValue = value {
  17. threadDictionary[key] = newValue
  18. }
  19. else {
  20. threadDictionary[key] = nil
  21. }
  22. }
  23. static func getThreadLocalStorageValueForKey<T>(_ key: NSCopying) -> T? {
  24. let currentThread = Thread.current
  25. let threadDictionary = currentThread.threadDictionary
  26. return threadDictionary[key] as? T
  27. }
  28. }
  29. #endif