-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Simplify 'sf::Err' and prevent data races #3008
Conversation
It's been implemented like that since the pre git days, unfortunately I don't know how to find FS#10 😄 76de05a#diff-bd73b69ba08ad3c155e32487a2d553ed69f7ec4cbea6fa3ef7a5fcb5a95ecab8 |
I'm not stressed about the performance of error paths so if we can fix threading issues and removes lots of code at the cost of error-path performance then I'm cool with that. Seems like an easy win. Let's see if those experiencing crashes can still recreate it with this patch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we simplify sf::err
down this far, then if I'm understanding correctly the only reason this API exists is to provide a way to redirect the SFML error messages without redirecting all of std::cerr
. Is that right? I suppose in theory someone may want to send all SFML errors to a different stream or silence them entirely but not affect std::cerr
which they may still be using for other purposes.
Yes, I believe this is done in
|
If Somewhat tangential but I wonder if the |
The additional safety that this PR brings is that every thread has its own clone of Prior to this PR, if the buffer used was thread-safe, we would still get data races because After this PR, if the buffer used is thread-safe, |
Right, duh, For users who want to silence or redirect SFML log messages, they now have to do that in each thread individually. Not sure how much crossover there is between those who redirect |
Which you can't really, since for example the audio threads are spawned by SFML and not accessible for redirection? |
That's a very good point! I'll add something in the documentation for sure. The alternative is to have a single global buffer pointer that all thread-local instances share, but then we introduce back complexity that I don't think is needed. |
Interesting point. Not all threads are owned and accessible by users. Vittorio floated the idea of a |
True. So we'd have to do something like std::atomic<std::streambuf*> errBufPtr{std::cerr.rdbuf()}; // global, private
void redirectErrStream(std::streambuf* bufPtr); // public
std::ostream& err()
{
thread_local std::ostream stream;
stream.rdbuf(errBufPtr.fetch());
return stream;
} |
I think I can come up with an alternative design that uses a single err stream that locks at the beginning of a streaming chain and unlocks automatically at the end. That would avoid the issues with the buffer redirection. Will do it tomorrow |
Closing this in favour of #3010 which I think is superior because it has fewer caveats. |
Should fix the crash in #3003 and also simplifies
sf::Err
's implementation by removingDefaultErrStreamBuf
. AFAICS, that custom stream buffer was used to make error printing buffered, asstd::cerr
is unbuffered by default.But I don't think the extra complexity is worth it, we're basically optimizing the performance of... error message printing.
Am I missing something?