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

Add cancellation support for blocking methods #71

Open
ekalchev opened this issue Nov 1, 2023 · 0 comments
Open

Add cancellation support for blocking methods #71

ekalchev opened this issue Nov 1, 2023 · 0 comments

Comments

@ekalchev
Copy link

ekalchev commented Nov 1, 2023

There is no way to interrupt a method like CircularBuffer.Read before the timeout limit is exceeded. This is problem is you want to gracefully wait for exit of a worker thread, you must wait the timeout of Read method. This applies to all methods that accept timeouts. To support cancellation for Read method, GetNodeForReading method should be modified like this

protected unsafe virtual Node* GetNodeForReading(int timeout, CancellationToken cancellationToken)
{
    Node* ptr;
    while (true)
    {
        int readStart = _nodeHeader->ReadStart;
        ptr = this[readStart];
        if (readStart == _nodeHeader->WriteEnd)
        {
            int result = WaitHandle.WaitAny(new WaitHandle[] { cancellationToken.WaitHandle, DataExists }, timeout, false);

            if(result == WaitHandle.WaitTimeout || result == 0)
            {
                return null;
            }
        }
        else if (Interlocked.CompareExchange(ref _nodeHeader->ReadStart, ptr->Next, readStart) == readStart)
        {
            break;
        }
    }

same goes for other methods that are having wait handles.
    return ptr;
}
@ekalchev ekalchev changed the title Add cancellation to blocking methods Add cancellation support for blocking methods Nov 1, 2023
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

No branches or pull requests

1 participant