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

make stderr mutex recursive and lock it when panicking #20268

Merged
merged 4 commits into from
Jun 13, 2024

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Jun 11, 2024

This patch adds std.Thread.Mutex.Recursive which is a recursive mutex implementation (cc @kprotty for review).

Next, it changes the mutex used by std.debug.lockStdErr() to be a recursive mutex, and obtains the lock when panicking.

The previously used panic_mutex is no longer needed.

@andrewrk andrewrk force-pushed the keep-calm-and-continue-panicking branch 2 times, most recently from 044d604 to b62b0f3 Compare June 11, 2024 22:25
@andrewrk andrewrk changed the title std.debug: lock stderr mutex when panicking; std.Progress: fix race assertion failure std.debug: lock stderr mutex when panicking Jun 11, 2024
@andrewrk
Copy link
Member Author

I realized that what I'm describing is simply making the stderr mutex a recursive mutex.

The doc comments for this global said:
"Locked to avoid interleaving panic messages from multiple threads."

Huh? There's already a mutex for that, it's the stderr mutex. Lock that
one instead.
@andrewrk andrewrk force-pushed the keep-calm-and-continue-panicking branch from b62b0f3 to 2cca9de Compare June 13, 2024 00:19
@andrewrk andrewrk requested a review from kprotty as a code owner June 13, 2024 00:19
@andrewrk andrewrk changed the title std.debug: lock stderr mutex when panicking make stderr mutex recursive and lock it when panicking Jun 13, 2024
@andrewrk andrewrk force-pushed the keep-calm-and-continue-panicking branch from 2cca9de to fad223d Compare June 13, 2024 00:43
Comment on lines 45 to 49
if (!tryLockInner(r, current_thread_id)) {
r.mutex.lock();
assert(r.lock_count == 0);
r.lock_count = 1;
@atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .monotonic);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this could be simplified:

lock():
  c = LOAD(&count, Unordered)
  if not (c > 0 and LOAD(&current_tid, Unorderd) == tid):
    mutex.lock() // make this failable for tryLock()
    c = LOAD(&count, Unordered)
    assert(c == 0) 
    STORE(&current_id, tid, Unordered)
  STORE(&count, c + 1, Unordered)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, an invalid_thread_id would be simplest:

lock():
  if LOAD(&current_tid, Unordered) != tid:
    mutex.lock()
    STORE(&current_tid, tid, Unordered)
  count += 1
  
unlock():
  count -= 1
  if count == 0:
    STORE(&current_tid, invalid_tid, Unordered)
    mutex.unlock()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that last impl looks pretty good.

This version is simpler. Thanks King!
@andrewrk andrewrk merged commit 4aa1544 into master Jun 13, 2024
10 checks passed
@andrewrk andrewrk deleted the keep-calm-and-continue-panicking branch June 13, 2024 04:21
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

Successfully merging this pull request may close these issues.

2 participants