Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sig_block() does not nest properly #55

Closed
jdemeyer opened this issue Apr 21, 2017 · 10 comments
Closed

sig_block() does not nest properly #55

jdemeyer opened this issue Apr 21, 2017 · 10 comments

Comments

@jdemeyer
Copy link
Collaborator

This doesn't work as expected:

sig_block()
sig_block()
sig_unblock()
sig_unblock()

This causes warnings in Sage when cysignals is compiled in debug mode.
@embray

@embray
Copy link
Collaborator

embray commented Apr 21, 2017

Would it make sense if sig_block/unblock just incremented/decremented cysigs.block_sigint rather than set it to 1/0? Then the only warning should be if there are more unblocks than blocks (it would be hard to warn about the opposite if it can be arbitrarily deeply nested).

@jdemeyer
Copy link
Collaborator Author

Right, that general idea should work. It's just that we have to think about potential race conditions here (incrementing a variable is not an atomic operation, while setting it to a fixed number is atomic).

@embray
Copy link
Collaborator

embray commented Apr 21, 2017

True--though that raises the question as to what is cysignals' story with respect to thread-safety in the first place? sig_on/off are definitely not safe as is either.

@jdemeyer
Copy link
Collaborator Author

cysignals is not thread-safe at all (that's a different topic: #21).

I'm talking about race conditions given signals (which should be handled correctly by a package which is meant to deal with signals).

@embray
Copy link
Collaborator

embray commented Apr 21, 2017

Ah I see what you're saying--just in general if a signal interrupted the increment/decrement.

@embray
Copy link
Collaborator

embray commented Apr 21, 2017

@jdemeyer
Copy link
Collaborator Author

Thinking about it... just incrementing/decrementing looks safe, but the reasoning why it's safe is non-trivial and needs to be documented.

@vbraun
Copy link
Member

vbraun commented Apr 22, 2017

C++11 has atomic operations that are portable, and not just some gcc implementation details:

static std::atomic_int x = 1
x += 1   // no race condition

C11 has similar operations in <stdatomic.h>, just uglier syntax.

Setting variables may or may not be atomic depending on the CPU. Eg on armeabi-v7a (basically all smartphones) you need a special ASM instruction to atomically set a 64-bit int which the compiler does not necessarily emit for a C/C++ assignment.

@jdemeyer
Copy link
Collaborator Author

4801f95

@embray
Copy link
Collaborator

embray commented Apr 24, 2017

I was thinking that as well--as long as no actual signal handlers touch the sig_block value it doesn't matter if the increment is interrupted by a signal. There's still a race condition, technically, but one that would exist either way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants