internal void OnSyncReady(WorkSyncContext sync) { if (_waitingSyncedWork.Options.HasFlag(WorkOptions.Atomic)) { if (_isInAtomic) { throw new InvalidOperationException("Already in atomic"); } _isInAtomic = true; _atomicWork = _waitingSyncedWork; Swap(ref _workQueue, ref _pendingWorkQueue); if (_workQueue == null) { _workQueue = new Queue <Work>(); } } // all waitor workers stops for waiting this work done. // we can process this work safely. ProcessWork(_waitingSyncedWork); lock (_lock) { _waitingSyncedWork = null; if (_isWorkLoopSpawned == false) { _isWorkLoopSpawned = true; ThreadPool.UnsafeQueueUserWorkItem(WorkLoop, null); } } }
internal void OnSyncEnd(WorkSyncContext sync) { // when synched work finshed, resume work loop again lock (_lock) { if (_waitingSync.Source == sync) { _waitingSync = null; if (_isWorkLoopSpawned == false) { _isWorkLoopSpawned = true; ThreadPool.UnsafeQueueUserWorkItem(WorkLoop, null); } } else { throw new InvalidOperationException("Waiting state doesn't match with event source"); } } }
internal void QueueSync(WorkSyncContext source) { var work = new WorkSync { Source = source, Options = WorkOptions.Sync }; bool willSpawn = false; lock (_lock) { _workQueue.Enqueue(work); if (_isWorkLoopSpawned == false) { _isWorkLoopSpawned = true; willSpawn = true; } } if (willSpawn) { WorkLoop(null); } }