예제 #1
0
 /// <summary>
 /// Set the affinity to the current thread.
 /// </summary>
 /// <param name="affinity"></param>
 public static void SetAffinity(this ThreadAffinity affinity)
 {
     if (affinity != null)
     {
         int threadID = Thread.CurrentThread.ManagedThreadID;
         affinity.OwnerThreadID   = threadID;
         affinity.OwnerThreadName = Thread.CurrentThread.Name;
         affinity.UnregisterWorkerThreadInternal(threadID);
     }
 }
예제 #2
0
 /// <summary>
 /// Validate that current code has the desired thread affinity
 /// </summary>
 /// <param name="affinity"></param>
 public static void Validate(this ThreadAffinity affinity)
 {
     if (affinity != null)
     {
         // Careful,Thread.Name allocates in Mono,so don`t access it until there`s a problem to report
         Thread currentThread   = Thread.CurrentThread;
         int    currentThreadID = currentThread.ManagedThreadID;
         if (currentThreadID != affinity.OwnerThreadID && !affinity.IsWorkerThreadRegisteredInternal(currentThreadID))
         {
             Assert.DebugFail($"Invoked on wrong thread;expected thread \"{affinity.OwnerThreadName}\"(ID {affinity.OwnerThreadID}) or registered worker thread but was thrad \"{currentThread.Name}\"(ID {currentThread.ManagedThreadID})");
         }
     }
 }