Platform.Darwin.swift 951 B

1234567891011121314151617181920212223242526272829303132333435
  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 Foundation
  11. extension Thread {
  12. static func setThreadLocalStorageValue<T: AnyObject>(_ value: T?, forKey key: NSCopying) {
  13. let currentThread = Thread.current
  14. let threadDictionary = currentThread.threadDictionary
  15. if let newValue = value {
  16. threadDictionary[key] = newValue
  17. }
  18. else {
  19. threadDictionary[key] = nil
  20. }
  21. }
  22. static func getThreadLocalStorageValueForKey<T>(_ key: NSCopying) -> T? {
  23. let currentThread = Thread.current
  24. let threadDictionary = currentThread.threadDictionary
  25. return threadDictionary[key] as? T
  26. }
  27. }
  28. #endif