0001 // Threading.swift 0002 // 0003 // Copyright (c) 2015 Jens Ravens (http://jensravens.de) 0004 // 0005 // Permission is hereby granted, free of charge, to any person obtaining a copy 0006 // of this software and associated documentation files (the "Software"), to deal 0007 // in the Software without restriction, including without limitation the rights 0008 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 // copies of the Software, and to permit persons to whom the Software is 0010 // furnished to do so, subject to the following conditions: 0011 // 0012 // The above copyright notice and this permission notice shall be included in 0013 // all copies or substantial portions of the Software. 0014 // 0015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0021 // THE SOFTWARE. 0022 0023 import Foundation 0024 0025 /** 0026 Several functions that should make multithreading simpler. 0027 Use this functions together with Signal.ensure: 0028 Signal.ensure(Thread.main) // will create a new Signal on the main queue 0029 */ 0030 public final class Thread { 0031 #if os(Linux) 0032 #else 0033 /// Transform a signal to the main queue 0034 public static func main<T>(a: Result<T>, completion: Result<T>->Void) { 0035 queue(dispatch_get_main_queue())(a, completion) 0036 } 0037 #endif 0038 0039 #if os(Linux) 0040 #else 0041 /// Transform the signal to a specified queue 0042 public static func queue<T>(queue: dispatch_queue_t) -> (Result<T>, Result<T>->Void) -> Void { 0043 return { a, completion in 0044 dispatch_async(queue){ 0045 completion(a) 0046 } 0047 } 0048 } 0049 #endif 0050 0051 #if os(Linux) 0052 #else 0053 /// Transform the signal to a global background queue with priority default 0054 public static func background<T>(a: Result<T>, completion: Result<T>->Void) { 0055 let q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 0056 dispatch_async(q) { 0057 completion(a) 0058 } 0059 } 0060 #endif 0061 }
Threading.swift:35 queue(dispatch_get_main_queue())(a, completion)