forked from totaljs/framework5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.js
129 lines (105 loc) · 2.98 KB
/
workers.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
// Total.js Workers
// The MIT License
// Copyright 2020-2023 (c) Peter Širka <petersirka@gmail.com>
const HEADER = { cwd: '' };
function process_thread() {
if (F.worker)
return F.worker;
F.dir(process.cwd());
const Port = F.Worker.parentPort;
F.worker = {};
F.worker.data = Port ? F.Worker.workerData : {};
F.worker.message = NOOP;
F.worker.isfork = !!Port;
F.worker.is = process.argv.indexOf('--worker') !== -1;
F.worker.setTimeout = function(timeout) {
F.worker.$timeout && clearTimeout(F.worker.$timeout);
F.worker.$timeout = setTimeout(() => F.worker.exit(1), timeout);
};
F.worker.postMessage = F.worker.send = function() {
if (Port)
Port.postMessage.apply(Port, arguments);
else
process.send.apply(process, arguments);
};
F.worker.exit = F.worker.kill = F.worker.close = function(code) {
process.exit(code || 0);
};
var onmessage = function() {
F.worker.message && F.worker.message.apply(this, arguments);
};
if (Port)
Port.on('message', onmessage);
else
process.on('message', onmessage);
return F.worker;
}
exports.createthread = function(name, data) {
if (!name)
return process_thread();
var filename = name[0] === '~' ? name.substring(1) : F.path.root('workers/' + name + '.js');
var worker = new F.Worker.Worker(filename, { workerData: data, cwd: HEADER, argv: ['--worker'] });
worker.kill = worker.exit = () => worker.terminate();
return worker;
};
exports.createfork = function(name) {
if (!name)
return process_thread();
var filename = name[0] === '~' ? name.substring(1) : F.path.root('workers/' + name + '.js');
var fork = new F.Child.fork(filename, { cwd: HEADER, argv: ['--worker'] });
fork.postMessage = fork.send;
fork.terminate = () => fork.kill('SIGTERM');
return fork;
};
exports.createpool = function(name, count, isfork) {
var pool = {};
pool.workers = [];
pool.pending = [];
pool.count = pool;
pool.next = function() {
for (let worker of pool.workers) {
if (worker.$released) {
let fn = pool.pending.shift();
if (fn) {
worker.removeAllListeners('message');
worker.$released = false;
fn.call(worker, worker, worker.release);
} else
break;
}
}
};
F.workers[name] = pool;
var release = function(worker) {
worker.on('exit', function() {
let index = pool.workers.indexOf(worker);
pool.workers.splice(index, 1);
let worker = isfork ? exports.createfork(name) : exports.createthread(name);
worker.$pool = pool;
worker.release = release(worker);
});
return function() {
worker.$released = true;
worker.$pool.next();
};
};
for (var i = 0; i < count; i++) {
var worker = isfork ? exports.createfork(name) : exports.createthread(name);
worker.$pool = pool;
worker.$released = true;
worker.release = release(worker);
pool.workers.push(worker);
}
pool.exec = function(fn) {
if (fn) {
pool.pending.push(fn);
pool.next();
} else {
return new Promise(function(resolve) {
pool.pending.push(resolve);
pool.next();
});
}
};
return pool;
};