有没有办法在没有锁的情况下同步它

Is there a way to synchronize this without locks?

本文关键字:情况下 同步 有没有      更新时间:2023-10-16

假设我有 3 个可以由上层调用的函数:

  • Start - 仅当我们尚未启动或之前调用 Stop 时才会调用
  • Stop - 仅在成功调用"开始"屏幕后才会调用
  • Process - 可以随时调用(同时在不同的线程上);如果启动,将调用到下层

Stop中,它必须等待所有Process调用完成对下层的调用,并阻止任何进一步的调用。使用锁定机制,我可以想出以下伪代码:

Start() {
  ResetEvent(&StopCompleteEvent);
  IsStarted = true;
  RefCount = 0;
}
Stop() {
   AcquireLock();
   IsStarted = false;
   WaitForCompletionEvent = (RefCount != 0);
   ReleaseLock();
   if (WaitForCompletionEvent)
     WaitForEvent(&StopCompleteEvent);
   ASSERT(RefCount == 0);
}
Process() {
  AcquireLock();
  AddedRef = IsStarted;
  if (AddedRef)
    RefCount++;
  ReleaseLock();
  if (!AddedRef) return;
  ProcessLowerLayer();
  AcquireLock();
  FireCompletionEvent = (--RefCount == 0);
  ReleaseLock();
  if (FilreCompletionEvent)
    SetEvent(&StopCompleteEvent);
}

有没有办法在没有锁定机制的情况下实现相同的行为?也许有一些花哨的用法InterlockedCompareExchange和InterlockedIncremenet/InterlockedDecrement?

问的原因是这是在网络驱动程序的数据路径中,我真的不希望有任何锁。

我相信可以

避免使用显式锁和任何不必要的阻塞或内核调用。

请注意,这只是伪代码,仅用于说明目的;它没有看到编译器。 虽然我相信线程逻辑是合理的,但请自己验证它的正确性,或者让专家来验证它;无锁编程很难

#define STOPPING 0x20000000;
#define STOPPED 0x40000000;
volatile LONG s = STOPPED;
  // state and count
  // bit 30 set -> stopped
  // bit 29 set -> stopping
  // bits 0 through 28 -> thread count
Start() 
{
   KeClearEvent(&StopCompleteEvent);
   LONG n = InterlockedExchange(&s, 0);  // sets s to 0
   if ((n & STOPPED) == 0) 
       bluescreen("Invalid call to Start()");
}
Stop()
{
   LONG n = InterlockedCompareExchange(&s, STOPPED, 0);
   if (n == 0)
   {
       // No calls to Process() were running so we could jump directly to stopped.
       // Mission accomplished!
       return;
   }
   LONG n = InterlockedOr(&s, STOPPING);
   if ((n & STOPPED) != 0)
       bluescreen("Stop called when already stopped");
   if ((n & STOPPING) != 0)
       bluescreen("Stop called when already stopping");
   n = InterlockedCompareExchange(&s, STOPPED, STOPPING);
   if (n == STOPPING)
   {
       // The last call to Process() exited before we set the STOPPING flag.
       // Mission accomplished!
       return;
   }
   // Now that STOPPING mode is set, and we know at least one call to Process 
   // is running, all we need do is wait for the event to be signaled.
   KeWaitForSingleObject(...);
   // The event is only ever signaled after a thread has successfully
   // changed the state to STOPPED.  Mission accomplished!
   return;
}
Process()
{
    LONG n = InterlockedCompareExchange(&s, STOPPED, STOPPING);
    if (n == STOPPING)
    {
         // We've just stopped; let the call to Stop() complete.
         KeSetEvent(&StopCompleteEvent);
         return;
    }
    if ((n & STOPPED) != 0 || (n & STOPPING) != 0)
    {
         // Checking here avoids changing the state unnecessarily when
         // we already know we can't enter the lower layer.
         // It also ensures that the transition from STOPPING to STOPPED can't
         // be delayed even if there are lots of threads making new calls to Process().
         return;
    }
    n = InterlockedIncrement(&s);
    if ((n & STOPPED) != 0)
    {
        // Turns out we've just stopped, so the call to Process() must be aborted.
        // Explicitly set the state back to STOPPED, rather than decrementing it,
        // in case Start() has been called.  At least one thread will succeed.
        InterlockedCompareExchange(&s, STOPPED, n);
        return;
    }
    if ((n & STOPPING) == 0)
    {
        ProcessLowerLayer();
    }
    n = InterlockedDecrement(&s);
    if ((n & STOPPED) != 0 || n == (STOPPED - 1))
        bluescreen("Stopped during call to Process, shouldn't be possible!");
    if (n != STOPPING)
        return;
    // Stop() has been called, and it looks like we're the last 
    // running call to Process() in which case we need to change the 
    // status to STOPPED and signal the call to Stop() to exit.
    // However, another thread might have beaten us to it, so we must 
    // check again.  The event MUST only be set once per call to Stop().
    n = InterlockedCompareExchange(&s, STOPPED, STOPPING);
    if (n == STOPPING)
    {
         // We've just stopped; let the call to Stop() complete.
         KeSetEvent(&StopCompleteEvent);
    }
    return;
}