-
Notifications
You must be signed in to change notification settings - Fork 2
/
threadful.js
980 lines (813 loc) · 22.9 KB
/
threadful.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
(function(){
var isBrowser = (typeof window !== "undefined" || typeof self !== "undefined");
var isNode = (typeof process !== "undefined");
var isMaster = (isBrowser && typeof window !== "undefined") || (isNode && process !== "undefined" && process.env._IS_NODE_SLAVE_PROCESS!=="true");
var isSlave = (isBrowser && typeof window === "undefined") || (isNode && process !== "undefined" && process.env._IS_NODE_SLAVE_PROCESS==="true");
if (!isBrowser && !isNode) throw new Error("Unable to understand execution context, neither Browser nor Node.");
var threadfulName = "threadful.js";
var threadfulPath = threadfulName;
var oneup = 1;
var getId = function() {
return new Date().getTime()+"."+(oneup++);
};
var toArray = function(a) {
return Array.prototype.slice.call(a);
};
var keys = function(o) {
if (Object.keys) return Object.keys(o);
var keys = [];
for (var key in o) keys.push(key);
return keys;
};
var extend = function(src,dest) {
each(keys(src),function(key){
if (dest[key]===undefined) dest[key] = src[key];
});
return dest;
};
var each = function(a,f) {
if (Array.prototype.forEach) a.forEach(f);
else if (Array.prototype.each) a.each(f);
else for (var i=0;i<a.length;i++) f(a[i],i);
return a;
};
var indexOf = function(a,x) {
if (Array.prototype.indexOf) return a.indexOf(x);
for (var i=0;i<a.length;i++) {
if (a[i]===x) return i;
}
return -1;
};
var isFunction = function(f) {
return typeof f === "function";
};
var sanatizeArguments = function(a) {
a = toArray(a);
each(a,function(v,i){
if (isFunction(v)) v = v.toString();
if (v.toString()==="[object Arguments]") v = toArray(v);
if (typeof self !=="undefined" && v===self) v = "[object Self]";
a[i] = v;
});
return a;
};
var functionToString = function(f) {
if (!f || !isFunction(f)) throw new Error("Not a Function.");
var s = f.toString();
s = s.replace(/\/\/(.*)[\n\r\v\f]/g,"/* $1 */");
s = s.replace(/[\n\r\v\f]/g," ");
s = s.replace(/\s\s|\t/g," ");
return s;
};
var stringToFunction = function(s) {
try {
var f = eval("("+s+")");
return f;
}
catch (ex) {
throw wrapError(ex,"Compilation Error");
}
};
var wrapError = function(ex,msg) {
var s = "";
if (ex.name) s += (msg?": ":"")+ex.name;
if (ex.message) s += (msg?": ":"")+ex.message;
if (ex.fileName) s += (msg?": ":"")+ex.fileName;
if (ex.lineNumber) s += (msg?": ":"")+ex.lineNumber;
if (!s) s = ex.toString();
s = msg +": "+s;
return s;
};
var traverse = function(o,s) {
var p = s.split(/\./);
if (p.length<2) return o;
var f = p.shift();
var r = p.join(".");
if (o[f]===undefined || o[f]===null) o[f] = {};
return traverse(o[f],r);
};
var timing = function(name,f) {
if (name && !f && isFunction(name)) {
f = name
name = "Anonymous Function";
}
if (!name) throw new Error("Timing should supply a name.");
if (!f || !isFunction(f)) throw new Error("Timing require a function.");
var total = 0;
var count = 0;
var g = function() {
count += 1;
var start = new Date().getTime();
var result,error;
try {
result = f.apply(this,toArray(arguments));
}
catch (ex) {
error = ex;
};
var time = new Date().getTime()-start;
total += time;
console.log(name+": "+time+" ms. (Total: "+total+" ms. / Count: "+count+" / Average: "+(((total/count*10000)|0)/10000)+" ms.)");
if (error) throw error;
return result;
};
return g;
};
// Setup Slave
var SlaveCode = function() {
var totalStart = new Date().getTime();
var installed = {};
var stats = {
successfulResults: 0,
unsuccessfulResults: 0,
totalTime: 0,
activeTime: 0,
averageTime: 0,
lastTime: 0,
lastCall: null,
selfies: 0,
executes: 0,
installs: 0,
uninstalls: 0,
};
var send = null;
var listen = null;
var unlisten = null;
var close = null;
var payload = null;
// What is the top level for this, global for node or self for browsers.
var top = isNode ? global : self;
if (isBrowser) {
send = function(payload) {
return top.postMessage(payload);
};
listen = function(event,listener) {
return top.addEventListener(event,listener);
};
unlisten = function(event,listener) {
return top.removeEventListener(event,listener);
};
close = function() {
setTimeout(function(){
top.close();
},1);
};
payload = function(event) {
return event.data;
};
}
else if (isNode) {
send = function(payload) {
return process.send(payload);
};
listen = function(event,listener) {
return process.on(event,listener);
};
unlisten = function(event,listener) {
return process.removeListener(event,listener);
};
close = function() {
setTimeout(function(){
process.exit();
},1);
};
payload = function(event) {
return event;
};
}
var respondError = function(id,error) {
stats.unsuccessfulResults += 1;
send({
id: id,
error: error instanceof Error ? wrapError(error,"Uknown Error") : error,
result: null
});
throw error;
};
var respondResult = function(id,result) {
stats.successfulResults += 1;
send({
id: id,
error: null,
result: result
});
};
var cmdSelfie = function(id,options) {
var name = options.name;
if (!name) return respondError(id,new Error("Selfie requires a name option."));
var last = name.split(/\./).slice(-1)[0];
if (!last) return respondError(id,new Error("Selfie did not understand the name option: "+name));
var f = function() {
var a = sanatizeArguments(arguments);
send({
id: "selfie",
command: name,
arguments: a
});
stats.selfies += 1;
};
var obj = traverse(top,name);
if (!obj[last]) obj[last] = f;
respondResult(id,true);
};
var cmdUnselfie = function(id,options) {
var name = options.name;
if (!name) return respondError(new Error("Unselfie requires a name option."));
var last = name.split(/\./).slice(-1)[0];
if (!last) return respondError(new Error("Unselfie did not understand the name option: "+name));
var obj = traverse(top,name);
delete obj[last];
respondResult(id,true);
};
var cmdInstall = function(id,options) {
var name = options.name;
if (!name) return respondError(id,new Error("Install requires a name option."));
var code = options.code;
if (!code) return respondError(id,new Error("Install requires a code option."));
var f = stringToFunction(code);
if (installed[name]) return respondError(id,new Error("Install already defined for name option: "+name));
installed[name] = f;
respondResult(id,true);
stats.installs += 1;
};
var cmdUninstall = function(id,options) {
var name = options.name;
if (!name) return respondError(new Error("Uninstall requires a name option."));
delete installed[name];
respondResult(id,true);
stats.uninstalls += 1;
};
var cmdExecute = function(id,options) {
var start = new Date().getTime();
var name = options.name;
if (!name) return respondError(id,new Error("Execute requires a name option."));
var args = options.arguments;
if (!args) return respondError(id,new Error("Execute requires an arguments option."));
var exec = installed[name]
if (!exec) return respondError(id,new Error("Execute did not find any installed code for name option: "+name));
try {
var result = exec.apply(this,args);
respondResult(id,result);
}
catch (ex) {
respondError(id,wrapError(ex,"Execution Error"));
}
var time = new Date().getTime()-start;
stats.executes += 1;
stats.lastTime = time;
stats.lastCall = name;
};
var cmdStatus = function(id,options) {
stats.totalTime = new Date().getTime()-totalStart;
respondResult(id,stats);
};
var cmdClose = function(id,options) {
close();
respondResult(id,true);
};
var commands = {
selfie: cmdSelfie,
unselfie: cmdUnselfie,
install: cmdInstall,
uninstall: cmdUninstall,
execute: cmdExecute,
status: cmdStatus,
close: cmdClose
};
listen("message",function(event){
var data = payload(event);
var id = data.id;
if (!id) throw new Error("Worker message requires an id.");
var cmd = data.command || "";
if (!cmd) throw new Error("Worker message requires a command.");
var opt = data.options || {};
var count = 0;
var fun = commands[cmd];
if (fun) {
try {
var start = new Date().getTime();
fun(id,opt);
stats.activeTime += new Date().getTime()-start;
stats.averageTime = stats.activeTime/(++count);
}
catch (ex) {
// do nothing, the exception should already have been handled.
}
}
else {
throw new Error("Worker did not understand command: "+cmd);
}
});
};
// setup Master
var MasterCode = function() {
var pools = [];
var callbacks = {};
var selfies = {};
var create = null;
var send = null;
var listen = null;
var unlisten = null;
var payload = null;
if (isBrowser) {
var scripts = document.getElementsByTagName("script");
var nametest = new RegExp(threadfulName+"$");
for (var i=0;i<scripts.length;i++) {
var src = scripts[i].src;
if (nametest.test(src)) {
var pathre = new RegExp("^(.*)\/"+threadfulName+"$");
var path = src.replace(pathre,"$1");
threadfulPath = path+"/"+threadfulName;
}
}
create = function() {
return new Worker(threadfulPath);
};
send = function(worker,payload) {
return worker.postMessage(payload);
};
listen = function(worker,event,listener) {
return worker.addEventListener(event,listener);
};
unlisten = function(worker,event,listener) {
return worker.removeEventListener(event,listener);
};
payload = function(event) {
return event.data;
};
}
else if (isNode) {
threadfulPath = require.resolve("./"+threadfulName);
create = function() {
var fork = require("child_process").fork;
var env = JSON.parse(JSON.stringify(process.env));
env._IS_NODE_SLAVE_PROCESS = "true";
return fork(threadfulPath,{
env: env
});
};
send = function(worker,payload) {
return worker.send(payload);
};
listen = function(worker,event,listener) {
return worker.on(event,listener);
};
unlisten = function(worker,event,listener) {
return worker.removeListener(event,listener);
};
payload = function(event) {
return event;
};
}
var registerCallback = function(id,worker,callback) {
callbacks[id] = {
callback: isFunction(callback) ? callback : null,
worker: worker
};
};
var executeCallback = function(id,error,result) {
var cb = callbacks[id];
delete callbacks[id];
if (cb.callback) cb.callback(error,result);
};
var registerSelfie = function(name,f) {
selfies[name] = f;
};
var executeSelfie = function(name,args) {
var f = selfies[name];
if (f) f.apply(this,args);
};
var Selfie = function(worker,name,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "selfie",
options: {
name: name
}
});
};
var Unselfie = function(worker,name,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "unselfie",
options: {
name: name
}
});
};
var Install = function(worker,name,functn,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "install",
options: {
name: name,
code: functionToString(functn)
}
});
};
var Uninstall = function(worker,name,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "uninstall",
options: {
name: name
}
});
};
var Execute = function(worker,name,args,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "execute",
options: {
name: name,
arguments: args
}
});
};
var Status = function(worker,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "status"
});
};
var Close = function(worker,callback) {
if (!worker) throw new Error("Worker closed or disconnected.");
var id = getId();
registerCallback(id,worker,callback);
send(worker,{
id: id,
command: "close"
});
};
registerSelfie("console.log",function(){
var a = toArray(arguments);
a.unshift("["+threadfulName+"]");
console.log.apply(console,a);
});
registerSelfie("console.info",function(){
var a = toArray(arguments);
a.unshift("["+threadfulName+"]");
console.info.apply(console,a);
});
registerSelfie("console.warn",function(){
var a = toArray(arguments);
a.unshift("["+threadfulName+"]");
console.warn.apply(console,a);
});
registerSelfie("console.error",function(){
var a = toArray(arguments);
a.unshift("["+threadfulName+"]");
console.error.apply(console,a);
});
var Thread = function(options) {
return new ThreadPool({
threads: 1
});
};
var ThreadPool = function(options) {
var me = this;
pools.push(this);
var options = extend({
threads: 1,
timeout: 500,
},options);
var workers = [];
var installed = {};
var last = 0;
var addThread = function() {
var worker = create();
listen(worker,"message",function(event){
var data = payload(event);
var id = data.id;
if (id==="selfie") {
var command = data.command || "";
var args = data.arguments || [];
executeSelfie(command,args);
}
else {
executeCallback(id,data.error,data.result);
}
// console.log(me.outstanding()+" / "+(((me.utilization()*10000)|0)/100)+"%");
});
each(keys(selfies),function(key,i){
Selfie(worker,key);
});
workers.push(worker);
};
var removeThread = function() {
var worker = workers.pop();
if (!worker) return;
Close(worker);
};
var busy = function(worker) {
if (!worker) return false;
var busy = false;
each(keys(callbacks),function(id){
if (busy) return;
if (callbacks[id].worker===worker) busy = true;
});
return busy;
};
for (var i=0;i<options.threads;i++) addThread();
this.getThreadPoolSize = function() {
return options.threads;
};
this.outstanding = function() {
var pending = 0;
if (!workers) return 0;
each(keys(callbacks),function(id){
if (indexOf(workers,callbacks[id].worker)>-1) pending += 1;
});
return pending;
};
this.utilization = function() {
return this.outstanding()/options.threads;
};
this.install = function(name,functn,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
installed[name] = functn;
var done = [];
each(workers,function(w,i){
var start = new Date().getTime();
Install(w,name,functn,function(){
done[i] = true;
var alldone = true;
each(workers,function(w,j){
if (!alldone) return;
if (done[j]!==true) alldone = false;
});
if (callback && alldone) callback(null,true);
else if (callback && options.timeout>-1 && start-new Date().getTime()>options.timeout) callback("Install timed out.",false);
});
});
return me;
};
this.uninstall = function(name,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
delete installed[name];
var done = [];
each(workers,function(w,i){
var start = new Date().getTime();
Uninstall(w,name,function(){
done[i] = true;
var alldone = true;
each(workers,function(w,j){
if (!alldone) return;
if (done[j]!==true) alldone = false;
});
if (callback && alldone) callback(null,true);
else if (callback && options.timeout>-1 && start-new Date().getTime()>options.timeout) callback("Uninstall timed out.",false);
});
});
return me;
};
this.installed = function(name) {
return !!installed[name];
};
this.list = function(callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
callback(null,keys(installed));
return me;
};
this.execute = function(name,arg,arg,etc,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
if (!installed[name]) throw new Error("Nothing installed as '"+name+"' in ThreadPool.");
var a = toArray(arguments);
a.shift();
var callback = a.pop();
if (callback && !isFunction(callback)) {
a.push(callback);
callback = null;
}
if (++last>=workers.length) last = 0;
var worker = workers[last];
if (!worker) throw new Error("Unable to obtain thread.");
Execute(worker,name,a,callback);
return me;
};
var iteration = function(iterable,i) {
if (isFunction(iterable)) return iterable(i);
else if (iterable.length!==undefined && iterable.length!==null) return iterable[i];
return undefined;
};
this.distribute = function(name,iterable,resolver,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
if (!me.installed(name)) throw new Error("Function "+name+" is not installed in the pool.");
if (!iterable) throw new Error("Iterable must be an interable object or a function.");
if (resolver && !isFunction(resolver)) throw new Error("Resolver must be null or a function.");
var pos = 0;
var done = false;
var using = [];
var results = [];
var next = function() {
if (done) return;
each(workers,function(worker,i){
if (busy(worker)) return;
using[i] = true;
exec(worker);
});
setTimeout(next,0);
};
var exec = function(worker) {
var i = pos++;
var val = iteration(iterable,i);
if (val===undefined || val===null) done = true;
if (!done) {
Execute(worker,name,[val],function(error,result){
if (resolver) resolver(error,result,val,i);
results[i] = error || result;
next();
});
}
else {
finish();
}
};
var finish = function(){
if (!done) return;
var exit = true;
each(workers,function(worker,i){
if (!exit) return;
if (using[i] && busy(worker)) exit = false;
});
if (exit) callback(null,results);
else setTimeout(finish,0);
};
next();
};
this.map = this.distribute;
this.some = function(name,iterable,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
var answer = false;
this.distribute(name,function(i){
if (answer!==true) return iteration(iterable,i);
return undefined;
},function(error,result){
if (error || result===true) {
answer = true;
return true;
}
return false;
},function(error,result){
callback(error,answer);
});
};
this.every = function(name,iterable,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
var answer = true;
this.distribute(name,function(i){
if (answer!==false) return iteration(iterable,i);
return undefined;
},function(error,result,val,i){
if (error || result===false) {
answer = false;
return false;
}
return true;
},function(error,result){
callback(error,answer);
});
};
this.filter = function(name,iterable,callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
var answer = [];
this.distribute(name,iterable,function(error,result,val,i){
if (!error && result===true) answer.push(val);
},function(error,result){
callback(error,answer);
});
};
this.status = function(callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
var answers = [];
each(workers,function(w,i){
var start = new Date().getTime();
Status(w,function(error,result){
result.state = busy(w) ? "busy" : "free";
answers[i] = result;
var alldone = true;
each(workers,function(w,j){
if (!alldone) return;
if (answers[j]===undefined) alldone = false;
});
if (callback && alldone) {
var stats = {
poolSize: options.threads,
options: options,
installed: installed,
threads: {}
};
each(answers,function(answer,i){
stats.threads[i] = answer;
});
callback(null,stats);
}
else if (callback && options.timeout>-1 && start-new Date().getTime()>options.timeout) callback("Status timed out.",null);
});
});
};
this.close = function(callback) {
if (!workers) throw new Error("ThreadPool has been closed.");
var done = [];
each(workers,function(worker,i){
Close(worker);
workers = null;
});
options.threads = 0;
return me;
};
this.closed = function() {
return workers===null;
};
this.threadify = function(f) {
if (!workers) throw new Error("ThreadPool has been closed.");
var id = getId();
var ready = false;
me.install(id,f,function(){
ready = true;
});
var g = function(arg,arg,etc,callback) {
var t = this;
var a = toArray(arguments);
if (!ready) {
setTimeout(function(){
g.apply(t,a);
},0);
return;
}
a.unshift(id);
return me.execute.apply(me,a);
};
return g;
};
return this;
};
var CloseAll = function(callback) {
each(pools,function(pool,i){
if (!pool || pool.closed()) return;
pool.close();
});
if (callback) callback(null,true);
};
var SoloThreadify = function(f) {
var w = new Thread();
return w.threadify(f);
};
var SoloExecute = function(f,arg,arg,etc,callback) {
var a = toArray(arguments);
var f = a.shift();
var callback = a.pop();
if (callback && !isFunction(callback)) {
a.push(callback);
callback = null;
}
var name = getId();
var w = new Thread();
w.install(name,f,function(error,result){
if (error && callback) callback(error,null);
else if (!error) {
a.push(callback);
a.unshift(name);
w.execute.apply(w,a);
w.uninstall(name);
}
});
};
var Threadful = {
Thread: Thread,
ThreadPool: ThreadPool,
Threadify: SoloThreadify,
Execute: SoloExecute,
CloseAll: CloseAll
};
if (isNode) {
module.exports = Threadful;
}
else if (isBrowser) {
window.Threadful = Threadful;
}
return Threadful;
};
// Depending on which runtime execution we are in, launch the appropriate setup.
if (isSlave) SlaveCode();
else if (isNode || isBrowser) MasterCode();
else throw new Error("Threadful is unable to determine what kind of runtime in which it is executing.");
})();