⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semaphor.c

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 C
字号:
#include <w32k.h>

#define NDEBUG
#include <debug.h>

/*
 * @implemented
 */
HSEMAPHORE
STDCALL
EngCreateSemaphore ( VOID )
{
  // www.osr.com/ddk/graphics/gdifncs_95lz.htm
  PERESOURCE psem = ExAllocatePool ( NonPagedPool, sizeof(ERESOURCE) );
  if ( !psem )
    return NULL;
  if ( !NT_SUCCESS(ExInitializeResourceLite ( psem )) )
  {
    ExFreePool ( psem );
    return NULL;
  }
  return (HSEMAPHORE)psem;
}

/*
 * @implemented
 */
VOID
STDCALL
EngAcquireSemaphore ( IN HSEMAPHORE hsem )
{
  // www.osr.com/ddk/graphics/gdifncs_14br.htm
  ASSERT(hsem);
  KeEnterCriticalRegion();
  ExAcquireResourceExclusiveLite ( (PERESOURCE)hsem, TRUE );
}

/*
 * @implemented
 */
VOID
STDCALL
EngReleaseSemaphore ( IN HSEMAPHORE hsem )
{
  // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
  ASSERT(hsem);
  ExReleaseResourceLite ( (PERESOURCE)hsem );
  KeLeaveCriticalRegion();
}

/*
 * @implemented
 */
VOID
STDCALL
EngDeleteSemaphore ( IN HSEMAPHORE hsem )
{
  // www.osr.com/ddk/graphics/gdifncs_13c7.htm
  ASSERT ( hsem );
  ExFreePool ( (PVOID)hsem );
}

/*
 * @implemented
 */
BOOL
STDCALL
EngIsSemaphoreOwned ( IN HSEMAPHORE hsem )
{
  // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
  ASSERT(hsem);
  return (((PERESOURCE)hsem)->ActiveCount > 0);
}

/*
 * @implemented
 */
BOOL
STDCALL
EngIsSemaphoreOwnedByCurrentThread ( IN HSEMAPHORE hsem )
{
  // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
  ASSERT(hsem);
  return ExIsResourceAcquiredExclusiveLite ( (PERESOURCE)hsem );
}

/*
 * @implemented
 */
BOOL STDCALL
EngInitializeSafeSemaphore(
   OUT ENGSAFESEMAPHORE *Semaphore)
{
   HSEMAPHORE hSem;

   if (InterlockedIncrement(&Semaphore->lCount) == 1)
   {
      /* Create the semaphore */
      hSem = EngCreateSemaphore();
      if (hSem == 0)
      {
         InterlockedDecrement(&Semaphore->lCount);
         return FALSE;
      }
      /* FIXME - not thread-safe! Check result of InterlockedCompareExchangePointer
                 and delete semaphore if already initialized! */
      (void)InterlockedExchangePointer((volatile PVOID *)&Semaphore->hsem, hSem);
   }
   else
   {
      /* Wait for the other thread to create the semaphore */
      ASSERT(Semaphore->lCount > 1);
      ASSERT_IRQL(PASSIVE_LEVEL);
      while (Semaphore->hsem == NULL);
   }

   return TRUE;
}

/*
 * @implemented
 */
VOID STDCALL
EngDeleteSafeSemaphore(
   IN OUT ENGSAFESEMAPHORE *Semaphore)
{
   if (InterlockedDecrement(&Semaphore->lCount) == 0)
   {
      /* FIXME - not thread-safe! Use result of InterlockedCompareExchangePointer! */
      EngDeleteSemaphore(Semaphore->hsem);
      (void)InterlockedExchangePointer((volatile PVOID *)&Semaphore->hsem, NULL);
   }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -