Struct DispatchQueue
Double buffered thread-safe queue
Limited by a maximum size that is passed in the constructor. This is an important for performance, to avoid memory reallocation during the work.
- One buffer is used for write: several threads can add new Log
Message 's to it, no one can read from that buffer. That is done via Enqueue(PayloadHandle, long, Log method that does that under the read lock[1].Level) - Second buffer is used as a read-only buffer: several sinks can read from it, without modifying anything. This is done via Begin
Read() and EndRead() that enters/exits the read lock[1]. - Second buffer also can be in an exclusive mode: Cleanup system calls Begin
Read to enter the exclusive lock[2] to make sure only it can access the read buffer at the time to release the memory used by the messages. EndExclusive() Read at the end of its work, cleaning the read buffer and flipping write/read ones, so now the write buffer is empty. Exclusive lock is unlocked after this.Exclusive Clear And Flip() - Sort() of the second (read) buffer is done under an exclusive lock[2], since it modifies the list, so it is not safe to read it.
When the access is finished End
This is used in the case when full synchronous flush is needed. Lock is used to control the thread-safe access for this data structure:
[1] Read-lock allows several threads to take it, but only if no exclusive lock is taken. Guarantees that no exclusive lock [2] will be taken during it. Read-lock is used in situations when it is ok for multiple threads to do some kind of access, like: add new elements (see Enqueue(Payload
[2] Exclusive lock is used when only one thread can enter and no read-locks are taken. This lock is used to cleanup the memory of the messages (see Begin
Implements
Inherited Members
Namespace: Unity.Logging
Assembly: Unity.Logging.dll
Syntax
[BurstCompile]
public struct DispatchQueue : IDisposable
Constructors
DispatchQueue(int)
Constructor for Dispatch
Declaration
public DispatchQueue(int size)
Parameters
Type | Name | Description |
---|---|---|
int | size | Maximum length of the queue |
Remarks
Creates two lists with the maximum amount of Log
Properties
IsCreated
Is true if this struct was initialized.
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool |
TotalLength
Total length of read-only and write buffers. Usually used for testing of internal state of the queue.
Declaration
public int TotalLength { get; }
Property Value
Type | Description |
---|---|
int |
Methods
BeginRead()
Puts Dispatch
Declaration
public UnsafeList<LogMessage>.ParallelReader BeginRead()
Returns
Type | Description |
---|---|
Unsafe |
ParallelReader is returned that can be used to get all the LogMessage's from the read buffer |
BeginReadExclusive()
Puts Dispatch
Declaration
public UnsafeList<LogMessage>.ParallelReader BeginReadExclusive()
Returns
Type | Description |
---|---|
Unsafe |
ParallelReader is returned that can be used to get all the LogMessage's from read buffer, usually to dispose them |
Dispose()
Dispose call that will call Dispose for the lists (under lock) if IsCreated is true. IDisposable
Declaration
public void Dispose()
EndLockAfterSyncAccess()
Unlocks Dispatch
Declaration
public void EndLockAfterSyncAccess()
EndRead()
Unlocks Dispatch
Declaration
public void EndRead()
EndReadExclusiveClearAndFlip()
Unlocks Dispatch
Declaration
public void EndReadExclusiveClearAndFlip()
Enqueue(PayloadHandle, long, LogLevel)
Adds new message to the write buffer under read lock. Gets the timestamp just before adding the message to the queue.
Declaration
public void Enqueue(PayloadHandle payload, long stacktraceId, LogLevel logLevel)
Parameters
Type | Name | Description |
---|---|---|
Payload |
payload | PayloadHandle of the log message |
long | stacktraceId | Stacktrace id of the log message or 0 if none |
Log |
logLevel | LogLevel of the log message |
LockAndSortForSyncAccess(out UnsafeList<LogMessage>, out UnsafeList<LogMessage>)
Enters the exclusive lock for Dispatch
Declaration
public void LockAndSortForSyncAccess(out UnsafeList<LogMessage> olderMessages, out UnsafeList<LogMessage> newerMessages)
Parameters
Type | Name | Description |
---|---|---|
Unsafe |
olderMessages | Returns the buffer with older messages (read buffer) |
Unsafe |
newerMessages | Returns the buffer with newer messages (write buffer) |