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

Swappable Locking Implementation #893

Merged
merged 1 commit into from
May 22, 2024

Conversation

JakenVeina
Copy link
Collaborator

Added a shared/reusable implementation for multi-locking within stream operators, I.E. being able to process upstream notifications and downstream notifications at the same time, with different locks, while still preserving notification order.

I've been testing this strategy for locking within the new .Filter() operators I'm working on. Usage of this class in that case would look like....

private void OnNext(IChangeSet<T> changes)
{
    using var @lock = SwappableLock.CreateAndEnter(UpstreamSynchronizationGate);
    
    foreach (var change in changes)
    {
        ...
    }
    
    var downstreamChanges = ChangeSet<T>.Empty;
    if (_downstreamChangesBuffer.Count is not 0)
    {
        downstreamChanges = new ChangeSet<T>(_downstreamChangesBuffer);
        _downstreamChangesBuffer.Clear();
    }
 
    if ((downstreamChanges.Count is not 0) || !_suppressEmptyChangeSets)
    {
        @lock.SwapTo(DownstreamSynchronizationGate);
        
        _downstreamObserver.OnNext(downstreamChanges);
    }
}

…m operators, I.E. being able to process upstream notifications and downstream notifications at the same time, with different locks, while still preserving notification order.
@dwcullop
Copy link
Member

Why would you need this? Is it as an optimization?

@JakenVeina
Copy link
Collaborator Author

Why would you need this? Is it as an optimization?

Yes. Nothing specifically more than that.

Copy link
Collaborator

@RolandPheasant RolandPheasant left a comment

Choose a reason for hiding this comment

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

When I .wrote DD, I was looking for something like this but never got it quite right. Looking forward to seeing it in action

@JakenVeina JakenVeina merged commit a02c6d6 into main May 22, 2024
1 check passed
@JakenVeina JakenVeina deleted the feature/upstream-downstream-locking branch May 22, 2024 05:46
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.

None yet

3 participants