forked from EFForg/https-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.js
More file actions
141 lines (124 loc) · 3.79 KB
/
Thread.js
File metadata and controls
141 lines (124 loc) · 3.79 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var Thread = {
hostRunning: true,
activeQueues: 0,
activeLoops: 0,
_timers: [],
get canSpin() {
delete this.canSpin;
return this.canSpin = this.current instanceof CI.nsIEventTarget;
},
runWithQueue: function(callback, self) {
var thread = this.current;
thread instanceof CI.nsIThreadInternal;
try {
this.activeQueues++;
thread.pushEventQueue(null);
return self ? callback.apply(self) : callback();
} finally {
thread.popEventQueue();
this.activeQueues--;
}
},
spinWithQueue: function(ctrl) {
return this.runWithQueue(function() { return Thread.spin(ctrl); });
},
spin: function(ctrl) {
if (!this.canSpin) throw new Error("Thread: can't spin!");
ctrl.startTime = ctrl.startTime || Date.now();
ctrl.timeout = false;
this.activeLoops++;
this._spinInternal(ctrl);
this.activeLoops--;
ctrl.elapsed = Date.now() - ctrl.startTime;
return ctrl.timeout;
},
_spinInternal: function(ctrl) {
var t = ctrl.startTime;
var maxTime = parseInt(ctrl.maxTime)
if (maxTime) {
while(ctrl.running && this.hostRunning) {
this.yield();
if (Date.now() - t > maxTime) {
ctrl.timeout = true;
ctrl.running = false;
break;
}
}
} else while(ctrl.running && this.hostRunning) this.yield();
},
yield: function() {
this.current.processNextEvent(true);
},
yieldAll: function() {
var t = this.current;
while(t.hasPendingEvents()) t.processNextEvent(false);
},
get current() {
delete this.current;
var obj = "@mozilla.org/thread-manager;1" in CC
? CC["@mozilla.org/thread-manager;1"].getService()
: CC["@mozilla.org/thread;1"].createInstance(CI.nsIThread);
this.__defineGetter__("current", function() { return obj.currentThread; });
return this.current;
},
get currentQueue() {
delete this.currentQueue;
var eqs = null;
const CTRID = "@mozilla.org/event-queue-service;1";
if (CTRID in CC) {
const IFace = CI.nsIEventQueueService;
eqs = CC[CTRID].getService(IFace);
}
this.__defineGetter__("currentQueue", eqs
? function() { return eqs.getSpecialEventQueue(IFace.CURRENT_THREAD_EVENT_QUEUE); }
: this.__lookupGetter__("current")
);
return this.currentQueue;
},
delay: function(callback, time, self, args) {
var timer = CC["@mozilla.org/timer;1"].createInstance(CI.nsITimer);
this._timers.push(timer);
timer.initWithCallback({
notify: this._delayRunner,
context: { callback: callback, args: args || [], self: self || null }
}, time || 1, 0);
},
asap: function(callback, self, args) {
if (this.canSpin) {
this.current.dispatch({
run: function() {
callback.apply(self, args || []);
}
}, CI.nsIEventTarget.DISPATCH_NORMAL);
} else {
this.delay(callback, 0, self, args);
}
},
basap: function(callback, self, args) { // before as soon as possible
if (!this.canSpin) {
this.asap(callback, self, args);
return;
}
var thread = this.current;
thread instanceof CI.nsIThreadInternal;
this.activeQueues++;
thread.pushEventQueue(null);
this.asap(function() {
callback.apply(self, args || []);
thread.popEventQueue();
Thread.activeQueues--;
}, self, args);
},
_delayRunner: function(timer) {
var ctx = this.context;
try {
ctx.callback.apply(ctx.self, ctx.args);
} finally {
this.context = null;
var tt = Thread._timers;
var pos = tt.indexOf(timer);
if (pos > -1) tt.splice(pos, 1);
timer.cancel();
}
}
}