-
Notifications
You must be signed in to change notification settings - Fork 33
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
EventLock API #2
Conversation
5de5a4f
to
004852c
Compare
This allows a user to issue multiple notifications during a single hold of the lock, while interleaving other operations with those notifications.
rebased |
Thanks for the PR! Do you have an example use case for this? If my thinking is right, this is useful when we want to emit a notification before knowing how many listeners to notify? And I guess we don't want to emit notifications after figuring out how many notifications to emit because that might cause spurious wakeups? |
In ringbahn, with One option is to wake all waiting tasks before all completed tasks. This doesn't require any new API here, just some code to determine how much space there will be on the queue before processing completed tasks. Another option is to wake one event listener, then one completed task - one in, one out. This sort of pattern, where the user interleaves other operations with notifications, is what this API enables. |
Are you taking that into account when interleaving other operations with notifications? |
Ah, you're right because of the change to check NOTIFIED if n is 1. EDIT: but that isn't true with this lock API. each subsequent call will add up, so two calls to |
I think notifications still don't add up because each |
Ah, got it. I'll close this for now. For my use case, anyway, it's probably better to wake all the waiting tasks with a single notify() call rather than interleaving operations like this. |
Signed-off-by: John Nunley <[email protected]>
Built on top of #1, will rebase after that merges.
I didn't make this take ownership of the Event, because I think that would not be wanted. Users should not be holding the lock on the event for unduly long anyway; having the lock take a lifetime discourages storing the lock for longer than needed. If you feel otherwise, I can revise.
All of the synchronization occurs when the lock is taken in
Event::lock
; if there are no listeners to notify at that point, a lock is simply not taken (rather than holding the lock while performing unrelated operations and doing no-op notifications). I think that's the best performance, but maybe its surprising that the lock isn't always taken - I've documented that fact in the method's docs.