namespace Bts.PubSub; using BCG.Utilities.General; using bts; using System; using System.Threading; /// /// An wrapper that asserts doWork is always called on the same thread. /// public class SingleThreadedWorker : IWorker { readonly IWorker worker; int threadId = noThreadId; public SingleThreadedWorker(IWorker worker) => this.worker = worker; static int noThreadId => -1; public void doWork() { var currThreadId = Environment.CurrentManagedThreadId; if (!(currThreadId == threadId || Interlocked.CompareExchange(ref threadId, currThreadId, noThreadId) == noThreadId || currThreadId == threadId)) { Log.assert(false, $"SingleThreadedWorker doWork called on a different thread. expectedThread: {threadId}, currentThread: {currThreadId}"); } worker.doWork(); } }