diff options
| author | yo mama <pepper@scannerjammer.com> | 2015-04-04 01:00:59 -0700 |
|---|---|---|
| committer | yo mama <pepper@scannerjammer.com> | 2015-04-04 01:00:59 -0700 |
| commit | c7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 (patch) | |
| tree | 8546df448afef40d3814d2581f4dacff7cebb87f /node_modules/webworker-threads/src/createPool.js | |
Diffstat (limited to 'node_modules/webworker-threads/src/createPool.js')
| -rw-r--r-- | node_modules/webworker-threads/src/createPool.js | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/node_modules/webworker-threads/src/createPool.js b/node_modules/webworker-threads/src/createPool.js new file mode 100644 index 0000000..08dc9ca --- /dev/null +++ b/node_modules/webworker-threads/src/createPool.js @@ -0,0 +1,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; +} |
