QueryWorker is a Class and QueryWorker.Next is a QueryWorker Field
This will Enqueue the Writer and put it last in line.
Returns true if you are the first in line.
Do you see any problems with this, this is only for enqueue, the pop part is a single consumer and therefore do not need locking.
internal bool TryStartWriting(QueryWorker Writer)
{
QueryWorker? Last;
do
{
Last = WriterQueueHead;
while (Last is null)
{
if (Interlocked.CompareExchange(ref WriterQueueHead, Writer, null) is null)
return true;
Last = WriterQueueHead;
}
while (Last.Next is not null)
Last = Last.Next;
} while (Interlocked.CompareExchange(ref Last.Next, Writer, null) is not null);
WriterQueueHead = Writer;
return false;
}