z3-solver
Version:
This project provides high-level and low-level TypeScript bindings for the [Z3 theorem prover](https://github.com/Z3Prover/z3). It is available on npm as [z3-solver](https://www.npmjs.com/package/z3-solver).
43 lines (42 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.killThreads = killThreads;
/**
* Terminates all Emscripten worker threads spawned by Z3.
*
* Long-running Z3 operations (such as `solver.check()`) run on background
* worker threads. In Node.js these workers keep the process alive even after
* Z3 has finished. Call this function once you are done using Z3 to allow the
* Node.js process to exit cleanly.
*
* ```typescript
* import { init, killThreads } from 'z3-solver';
*
* const api = await init();
* // ... use Z3 ...
* await killThreads(api.em);
* ```
* @category Global
*/
function killThreads(em) {
em.PThread.terminateAllThreads();
// Poll until all workers have been terminated.
let intervalHandle;
let timeoutHandle;
const lockPromise = new Promise(resolve => {
intervalHandle = setInterval(() => {
if (!em.PThread.unusedWorkers.length && !em.PThread.runningWorkers.length) {
clearInterval(intervalHandle);
clearTimeout(timeoutHandle);
resolve();
}
}, 100);
});
const delayPromise = new Promise((_, reject) => {
timeoutHandle = setTimeout(() => {
clearInterval(intervalHandle);
reject(new Error('Waiting for threads to be killed timed out'));
}, 5000);
});
return Promise.race([lockPromise, delayPromise]);
}