Skip to content

Add cancellation support for blocking methods #71

@ekalchev

Description

@ekalchev

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions