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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
function createPool(n){
var T, pool, idleThreads, q, poolObject, e, RUN, EMIT;
T = this;
n = Math.floor(n);
if (!(n > 0)) {
throw '.createPool( num ): number of threads must be a Number > 0';
}
pool = [];
idleThreads = [];
q = {
first: null,
last: null,
length: 0
};
poolObject = {
on: onEvent,
load: poolLoad,
destroy: destroy,
pendingJobs: getPendingJobs,
idleThreads: getIdleThreads,
totalThreads: getNumThreads,
any: {
eval: evalAny,
emit: emitAny
},
all: {
eval: evalAll,
emit: emitAll
}
};
try {
while (n--) {
pool[n] = idleThreads[n] = T.create();
}
} catch (e$) {
e = e$;
destroy('rudely');
throw e;
}
return poolObject;
RUN = 1;
EMIT = 2;
function poolLoad(path, cb){
var i;
i = pool.length;
while (i--) {
pool[i].load(path, cb);
}
}
function nextJob(t){
var job;
job = qPull();
if (job) {
if (job.type === RUN) {
t.eval(job.srcTextOrEventType, function(e, d){
var f;
nextJob(t);
f = job.cbOrData;
if (f) {
return job.cbOrData.call(t, e, d);
}
});
} else {
if (job.type === EMIT) {
t.emit(job.srcTextOrEventType, job.cbOrData);
nextJob(t);
}
}
} else {
idleThreads.push(t);
}
}
function qPush(srcTextOrEventType, cbOrData, type){
var job;
job = {
srcTextOrEventType: srcTextOrEventType,
cbOrData: cbOrData,
type: type,
next: null
};
if (q.last) {
q.last = q.last.next = job;
} else {
q.first = q.last = job;
}
q.length++;
}
function qPull(){
var job;
job = q.first;
if (job) {
if (q.last === job) {
q.first = q.last = null;
} else {
q.first = job.next;
}
q.length--;
}
return job;
}
function evalAny(src, cb){
qPush(src, cb, RUN);
if (idleThreads.length) {
nextJob(idleThreads.pop());
}
return poolObject;
}
function evalAll(src, cb){
pool.forEach(function(v, i, o){
return v.eval(src, cb);
});
return poolObject;
}
function emitAny(event, data){
qPush(event, data, EMIT);
if (idleThreads.length) {
nextJob(idleThreads.pop());
}
return poolObject;
}
function emitAll(event, data){
pool.forEach(function(v, i, o){
return v.emit(event, data);
});
return poolObject;
}
function onEvent(event, cb){
pool.forEach(function(v, i, o){
return v.on(event, cb);
});
return this;
}
function destroy(rudely){
var err, beNice, beRude;
err = function(){
throw 'This thread pool has been destroyed';
};
beNice = function(){
if (q.length) {
return setTimeout(beNice, 666);
} else {
return beRude();
}
};
beRude = function(){
q.length = 0;
q.first = null;
pool.forEach(function(v, i, o){
return v.destroy();
});
return poolObject.eval = poolObject.totalThreads = poolObject.idleThreads = poolObject.pendingJobs = poolObject.destroy = err;
};
if (rudely) {
beRude();
} else {
beNice();
}
}
function getNumThreads(){
return pool.length;
}
function getIdleThreads(){
return idleThreads.length;
}
function getPendingJobs(){
return q.length;
}
return getPendingJobs;
}
|