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

CommandQueueMT: Optimize & fix handling of sync/ret commands #90760

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RandomShaper
Copy link
Member

@RandomShaper RandomShaper commented Apr 16, 2024

While working on changes to threaded servers, I realized this class lacked a lock-unlock before marking a sync semaphore as unused. The bug itself would be solved just by locking there.

However, I also noticed that the approach of having a pool of semaphores could be replaced by one based on a condition variable and sequence numbering. Besides simplifying the code, this reduces the duplication of mutexes to lock-unlock (the main in the command queue class and the one in the sempahore) and avoids the spinning wait. Since both have overlapping concerns, we can go one level down, that is, splitting the semaphore into its building blocks (mutex and cond var) and letting our waits-for-sync be based on such cond var.

The only downside is that all threads waiting for syncing on commands will be awaken. Nonetheless, I think that's a no-issue in practice since normally you won't have multiple threads trying to sync on different commands. And, anyway, if a thread waiting to sync just awakes it can quickly check the command its interested in hasn't come yet and go back to waiting.

TL;DR

Pros:

  • Race condition fixed.
  • Simpler code.
  • No redundant lock-unlock.
  • No limit on awaiters (spinning wait gone).

Cons:

  • Awaiters of a given command are spuriously awaken whenever any other sync command is executed (an actual issue in practice?).

@RandomShaper RandomShaper added this to the 4.3 milestone Apr 16, 2024
@RandomShaper RandomShaper requested a review from reduz April 16, 2024 15:53
@RandomShaper RandomShaper requested a review from a team as a code owner April 16, 2024 15:53
@RandomShaper RandomShaper force-pushed the cmd_queue_good_sync branch 3 times, most recently from 3772d80 to f94eb31 Compare April 23, 2024 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants