-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
why is Signals volatile ? #431
Comments
I don't see signals are lock protected, for instance 'stop' is not. Regarding atomic, why you think we should use atomic instead of a bool in this case? (please if possible specific answer, no general stuff). |
As far as I know, and correct me if I'm wrong:
In practice, perhaps the SF code is such that all the possible scenarios that can result from this do not cause any problem, and have been thought through correctly. But if that's the case, it makes the code much harder to understand (and easier to break by seemingly trivial modifications that fail to understand the whole logic). It really takes a lot more brain power to envisage all possibilities and be sure that all of them are handled correctly. With lock/unlock or atomics, you have the standard to guarantee your expectations, and you have piece of mind, and a codebase that is future proof. Is there a mesurable performance penalty in imposing the following constraints?
Edit: The "after a reasonable amount of time" is only for atomics. For volatile, it seems that no guarantee is given. |
Yes, there is a possible slowdown
http://stackoverflow.com/questions/13135834/why-is-stdatomicbool-much-slower-than-volatile-bool
But this should not be a problem in our case. The practical difference is
that atomic enforces also a memory barrier, volatile bool does not. But
also this should not be problem in our case, nor required.
The signals should not necessarily set under lock protection, indeed there
is not need for this. For instance stop can raise at any time and code
checking for stop will simply see it as soon as (or a little bit after it
is raised), but because rising is a random event, there is no need of
synchronize anything here.
The most sensible reason to use atomic instead of bool is to improve
self-documenting quality of the code.
|
Marco, I'm not familiar with this code but your explanation makes complete sense to me. |
@mcostalba: By default atomics use memory_order_seq_cst. You could use memory_order_relaxed when reading the signal (on the hot path). But, of course,
|
anyway. i'm closing this, because it's not a bug it's a feature. |
@mcostalba: atomics with memory_order_acquire/release are completely free on x86. no memory barriers. it's only if you use the default memory_order_seq_cst that you have memory barriers. if you don't believe me, write a trivial program, and look at the assembly generated. it's only on ARM that you will see some memory barriers. I submitted tests with and without memory barriers on fishtest. My point is two-fold:
|
Actually, there is now a good tool to detect data races in code, please try -fsanitize=thread with a recent gcc (using 5.2 here) on linux. It does trigger often in stockfish, with quite clean output. For a good discussion on data races by the author of the implementation in gcc, see: So, I think performance is one aspect, but code correctness an other one, and as mentioned by lucasart, on x86 atomics are partially for free depending on the memory_order. For example, the first race found by the tool (on stockfish benchmark with 1 thread): WARNING: ThreadSanitizer: data race (pid=46204) Previous read of size 1 at 0x7d1c0000dff9 by thread T1 (mutexes: write M79): This one can be fixed by using Leading to the next: WARNING: ThreadSanitizer: data race (pid=46785) Previous read of size 8 at 0x00000065b268 by thread T1: Location is global 'Time' of size 32 at 0x00000065b260 (stockfish+0x00000065b268) |
Signals are now atomic. Closing this issue. |
Support two kings variant (wild9)
Add subvariant twokingsymmetric official-stockfish#431
So long as they are only read/written under lock protection, we are safe from unwanted compiler optimization.
Also, the bool members of SignalsType should be
std::atomic_flag
, orstd::atomic<bool>
, if we wanted to modify them outside lock protection.Am I missing something ?
The text was updated successfully, but these errors were encountered: