-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputation.swift
117 lines (85 loc) · 2.69 KB
/
Computation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Computation.swift
// DFSwift
//
// Created by Sinha, Gyanendra on 2/10/15.
// Copyright (c) 2015 Sinha, Gyanendra. All rights reserved.
//
import Foundation
let syncQueue = dispatch_queue_create("com.DF.syncQueue", DISPATCH_QUEUE_SERIAL)
let messageQueue = dispatch_queue_create("com.DF.messageQueue", DISPATCH_QUEUE_SERIAL)
let startQueue = dispatch_queue_create("com.DF.startQueue", DISPATCH_QUEUE_CONCURRENT)
struct Connection {
let task:_Task
let number:PortNumber
}
class Computation:NSOperation {
private var _lock:NSRecursiveLock = NSRecursiveLock()
private var _suspended:Bool = false
private var _task:_Task
private var _connections:[Int:Connection] = [Int:Connection]()
var state:Observable<TaskState> = Observable(.Ready)
var queue:NSOperationQueue?
init (_ task:_Task) {
_task = task
super.init()
}
override var executing:Bool {
return state.raw == .Executing
}
override var ready:Bool {
return super.ready && state.raw == .Ready && !_suspended
}
override var finished:Bool {
return state.raw == .Done
}
final func safelyExecuteClosure(closure:()->()) {
_lock.lock()
closure()
_lock.unlock()
}
func connectedTasks()->[_Task] {
return _connections.values.map({$0.task}).array
}
func addConnection(task:_Task, number:PortNumber) {
safelyExecuteClosure { [weak self] in
if let strongSelf = self {
strongSelf.removeConnection(number)
strongSelf._connections[number.value] = Connection(task: task, number: number)
strongSelf.addDependency(task.computation)
}
}
}
func removeConnection(number:PortNumber) {
safelyExecuteClosure { [weak self] in
if let strongSelf = self {
if let task = strongSelf._connections[number.value]?.task {
strongSelf.removeDependency(task.computation)
}
strongSelf._connections.removeValueForKey(number.value)
}
}
}
func canExecute()-> Bool {
}
func removeAllConnections() {
safelyExecuteClosure { [weak self] in
if let strongSelf = self {
var connections = strongSelf._connections
}
}
}
func _prepareForExexution() {
}
func suspended() -> Bool {
return _suspended
}
func suspend() {
}
func resume() {
}
override func main() {
}
override func start() {
}
}