|
1 | 1 | //
|
2 |
| -// Dispatcher.swift |
3 |
| -// SQLite |
| 2 | +// SQLite.swift |
| 3 | +// https://github.com/stephencelis/SQLite.swift |
| 4 | +// Copyright © 2014-2015 Stephen Celis. |
4 | 5 | //
|
5 |
| -// Created by Kevin Wooten on 11/28/15. |
6 |
| -// Copyright © 2015 stephencelis. All rights reserved. |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
7 | 23 | //
|
8 | 24 |
|
9 | 25 | import Foundation
|
10 | 26 |
|
11 | 27 |
|
12 | 28 | /// Block dispatch method
|
13 | 29 | public protocol Dispatcher {
|
14 |
| - |
15 |
| - /// Dispatches the provided block |
16 |
| - func dispatch(block: dispatch_block_t) |
17 |
| - |
| 30 | + |
| 31 | + /// Dispatches the provided block |
| 32 | + func dispatch(block: dispatch_block_t) |
| 33 | + |
18 | 34 | }
|
19 | 35 |
|
20 | 36 |
|
21 | 37 | /// Dispatches block immediately on current thread
|
22 | 38 | public final class ImmediateDispatcher : Dispatcher {
|
23 |
| - |
24 |
| - public func dispatch(block: dispatch_block_t) { |
25 |
| - block() |
26 |
| - } |
27 |
| - |
| 39 | + |
| 40 | + public func dispatch(block: dispatch_block_t) { |
| 41 | + block() |
| 42 | + } |
| 43 | + |
28 | 44 | }
|
29 | 45 |
|
30 | 46 |
|
31 | 47 | /// Synchronously dispatches block on a serial
|
32 | 48 | /// queue. Specifically allows reentrant calls
|
33 | 49 | public final class ReentrantDispatcher : Dispatcher {
|
34 |
| - |
35 |
| - static let queueKey = unsafeBitCast(ReentrantDispatcher.self, UnsafePointer<Void>.self) |
36 |
| - |
37 |
| - let queue : dispatch_queue_t |
38 |
| - |
39 |
| - let queueContext : UnsafeMutablePointer<Void>! |
40 |
| - |
41 |
| - public init(_ name: String) { |
42 |
| - queue = dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL) |
43 |
| - queueContext = unsafeBitCast(queue, UnsafeMutablePointer<Void>.self) |
44 |
| - dispatch_queue_set_specific(queue, ReentrantDispatcher.queueKey, queueContext, nil) |
45 |
| - } |
46 |
| - |
47 |
| - public func dispatch(block: dispatch_block_t) { |
48 |
| - if dispatch_get_specific(ReentrantDispatcher.queueKey) == self.queueContext { |
49 |
| - block() |
50 |
| - } else { |
51 |
| - dispatch_sync(self.queue, block) // FIXME: rdar://problem/21389236 |
| 50 | + |
| 51 | + static let queueKey = unsafeBitCast(ReentrantDispatcher.self, UnsafePointer<Void>.self) |
| 52 | + |
| 53 | + let queue : dispatch_queue_t |
| 54 | + |
| 55 | + let queueContext : UnsafeMutablePointer<Void> |
| 56 | + |
| 57 | + public init(_ name: String) { |
| 58 | + queue = dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL) |
| 59 | + queueContext = unsafeBitCast(queue, UnsafeMutablePointer<Void>.self) |
| 60 | + dispatch_queue_set_specific(queue, ReentrantDispatcher.queueKey, queueContext, nil) |
| 61 | + } |
| 62 | + |
| 63 | + public func dispatch(block: dispatch_block_t) { |
| 64 | + if dispatch_get_specific(ReentrantDispatcher.queueKey) == self.queueContext { |
| 65 | + block() |
| 66 | + } else { |
| 67 | + dispatch_sync(self.queue, block) // FIXME: rdar://problem/21389236 |
| 68 | + } |
52 | 69 | }
|
53 |
| - } |
54 |
| - |
| 70 | + |
55 | 71 | }
|
0 commit comments