Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/src/signal.zig
1067 views
1
const std = @import("std");
2
const python = @cImport(@cInclude("Python.h"));
3
4
// wasmGetSignalState has to be defined at the typescript level, since it potentially
5
// reads from a small SharedArrayBuffer that WASM does not have access to. It returns
6
// any pending signal.
7
extern fn wasmGetSignalState() i32;
8
9
export fn _Py_CheckEmscriptenSignals() void {
10
const signal = wasmGetSignalState();
11
// std.debug.print("python-wasm: _Py_CheckEmscriptenSignals: signal={}\n", .{signal});
12
if (signal != 0) {
13
// std.debug.print("_Py_CheckEmscriptenSignals: got a signal! {}\n", .{signal});
14
if (python.PyErr_SetInterruptEx(signal) != 0) {
15
std.debug.print("_Py_CheckEmscriptenSignals: ERROR -- invalid signal = {}\n", .{signal});
16
}
17
}
18
}
19
20
// Upstream uses 50 for analogue of SIGNAL_INTERVAL (see Python/emscripten_signal.c in the cpython sources).
21
// However, this may or may not be called frequently, so not checking more could be a problem. It would
22
// make a lot more sense to space these out with a high performance system clock... but of course access to
23
// that is at least as expensive as wasmGetSignalState, so there's no point.
24
const SIGNAL_INTERVAL: i32 = 50;
25
var signal_counter: i32 = SIGNAL_INTERVAL;
26
export fn _Py_CheckEmscriptenSignalsPeriodically() void {
27
// std.debug.print("python-wasm: _Py_CheckEmscriptenSignalsPeriodically\n", .{});
28
signal_counter -= 1;
29
if (signal_counter <= 0) {
30
signal_counter = SIGNAL_INTERVAL;
31
_Py_CheckEmscriptenSignals();
32
}
33
}
34
35
pub fn keepalive() void {}
36
37