cysignals
cysignals copied to clipboard
cysignals is not thread-safe
It would be nice if cysignals would support multi-threading. This would be useful for use with Python's threading module or with Cython's parallel features.
Interesting read: http://www.dabeaz.com/python/GIL.pdf
I'd be interested in helping to work on this. I've spent some time in Python's threading code, though really that's less important than just getting it right at the C level.
The problem is that I want the following properties:
- Correctness
- Portability
- Speed
This is a classic case of "pick any two" (the current implementation has properties 2 and 3).
Right. For a multi-threaded approach I suspect what we're really going to have to have are just completely separate implementations that can be enabled/disabled at compile time and/or runtime.
I'd like to focus on the case where there is a single thread in Python, but possibly multiple threads in the code guarded by sig_on()
. This is probably the most typical use case for cysignals.
Apparently on FreeBSD one cannot link cysignals against multithreaded Pari, it just does not work, see https://trac.sagemath.org/ticket/28242#comment:48
[cysignals-1.10.2] cc -pthread -shared -L/usr/local/lib -fstack-protector-strong -L/usr/ports/math/sage/work/stage/usr/local/lib -Wl,-rpath,/usr/ports/math/sage/work/stage/usr/local/lib -L/usr/local/llvm90/lib -L/usr/local/lib -Wl,-rpath=/usr/local/lib/gcc9 -L/usr/local/lib/gcc9 -B/usr/local/bin -L/usr/local/lib -fstack-protector-strong -O2 -pipe -DLIBICONV_PLUG -fstack-protector-strong -isystem /usr/local/include -fno-strict-aliasing -DLIBICONV_PLUG -I/usr/local/include -isystem /usr/local/include -Wp,-U_FORTIFY_SOURCE build/temp.freebsd-12.1-STABLE-amd64-3.7/build/src/cysignals/signals.o -L/usr/local/lib -lpython3.7m -o build/lib.freebsd-12.1-STABLE-amd64-3.7/cysignals/signals.so -lpari -lomp -pthread -L/usr/local/lib
[cysignals-1.10.2] /usr/local/bin/ld: PARI_SIGINT_block: TLS definition in /usr/local/lib/libpari.so section .tbss mismatches non-TLS reference in build/temp.freebsd-12.1-STABLE-amd64-3.7/build/src/cysignals/signals.o
[cysignals-1.10.2] /usr/local/bin/ld: /usr/local/lib/libpari.so: error adding symbols: bad value
I opened issue 122 before checking this. It turns out that struct_signals.h
won't even compile with -fopenmp
in some instances.
While sig_on()/sig_off()
isn't thread safe, I didn't notice any problem with sig_check()
.