ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

semaphor.c
Go to the documentation of this file.
00001 #include <win32k.h>
00002 
00003 #define NDEBUG
00004 #include <debug.h>
00005 
00006 /*
00007  * @implemented
00008  */
00009 HSEMAPHORE
00010 APIENTRY
00011 EngCreateSemaphore ( VOID )
00012 {
00013   // www.osr.com/ddk/graphics/gdifncs_95lz.htm
00014   PERESOURCE psem = ExAllocatePoolWithTag( NonPagedPool, sizeof(ERESOURCE), GDITAG_SEMAPHORE );
00015   if ( !psem )
00016     return NULL;
00017   if ( !NT_SUCCESS(ExInitializeResourceLite ( psem )) )
00018   {
00019     ExFreePoolWithTag ( psem, GDITAG_SEMAPHORE );
00020     return NULL;
00021   }
00022   return (HSEMAPHORE)psem;
00023 }
00024 
00025 VOID
00026 FASTCALL
00027 IntGdiAcquireSemaphore ( HSEMAPHORE hsem )
00028 {
00029   KeEnterCriticalRegion();
00030   ExAcquireResourceExclusiveLite ( (PERESOURCE)hsem, TRUE );
00031 }
00032 
00033 /*
00034  * @implemented
00035  */
00036 VOID
00037 APIENTRY
00038 EngAcquireSemaphore ( IN HSEMAPHORE hsem )
00039 {
00040   // www.osr.com/ddk/graphics/gdifncs_14br.htm
00041   PTHREADINFO W32Thread;
00042   ASSERT(hsem);
00043   IntGdiAcquireSemaphore ( hsem );
00044   W32Thread = PsGetThreadWin32Thread(PsGetCurrentThread());
00045   if (W32Thread) W32Thread->dwEngAcquireCount++;
00046 }
00047 
00048 
00049 VOID
00050 FASTCALL
00051 IntGdiReleaseSemaphore ( HSEMAPHORE hsem )
00052 {
00053   ExReleaseResourceLite ( (PERESOURCE)hsem );
00054   KeLeaveCriticalRegion();
00055 }
00056 
00057 /*
00058  * @implemented
00059  */
00060 VOID
00061 APIENTRY
00062 EngReleaseSemaphore ( IN HSEMAPHORE hsem )
00063 {
00064   // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
00065   PTHREADINFO W32Thread;
00066   ASSERT(hsem);
00067   W32Thread = PsGetThreadWin32Thread(PsGetCurrentThread());
00068   if (W32Thread) --W32Thread->dwEngAcquireCount;
00069   IntGdiReleaseSemaphore ( hsem );
00070 }
00071 
00072 VOID
00073 NTAPI
00074 EngAcquireSemaphoreShared(
00075     IN HSEMAPHORE hsem)
00076 {
00077     PTHREADINFO pti;
00078 
00079     ASSERT(hsem);
00080     ExEnterCriticalRegionAndAcquireResourceShared((PERESOURCE)hsem);
00081     pti = PsGetThreadWin32Thread(PsGetCurrentThread());
00082     if (pti) ++pti->dwEngAcquireCount;
00083 }
00084 
00085 /*
00086  * @implemented
00087  */
00088 VOID
00089 APIENTRY
00090 EngDeleteSemaphore ( IN HSEMAPHORE hsem )
00091 {
00092   // www.osr.com/ddk/graphics/gdifncs_13c7.htm
00093   ASSERT ( hsem );
00094 
00095   ExDeleteResourceLite((PERESOURCE)hsem);
00096 
00097   ExFreePoolWithTag( (PVOID)hsem, GDITAG_SEMAPHORE);
00098 }
00099 
00100 /*
00101  * @implemented
00102  */
00103 BOOL
00104 APIENTRY
00105 EngIsSemaphoreOwned ( IN HSEMAPHORE hsem )
00106 {
00107   // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
00108   ASSERT(hsem);
00109   return (((PERESOURCE)hsem)->ActiveCount > 0);
00110 }
00111 
00112 /*
00113  * @implemented
00114  */
00115 BOOL
00116 APIENTRY
00117 EngIsSemaphoreOwnedByCurrentThread ( IN HSEMAPHORE hsem )
00118 {
00119   // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
00120   ASSERT(hsem);
00121   return ExIsResourceAcquiredExclusiveLite ( (PERESOURCE)hsem );
00122 }
00123 
00124 /*
00125  * @implemented
00126  */
00127 BOOL APIENTRY
00128 EngInitializeSafeSemaphore(
00129    OUT ENGSAFESEMAPHORE *Semaphore)
00130 {
00131    HSEMAPHORE hSem;
00132 
00133    if (InterlockedIncrement(&Semaphore->lCount) == 1)
00134    {
00135       /* Create the semaphore */
00136       hSem = EngCreateSemaphore();
00137       if (hSem == 0)
00138       {
00139          InterlockedDecrement(&Semaphore->lCount);
00140          return FALSE;
00141       }
00142       /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
00143                 and delete semaphore if already initialized! */
00144       (void)InterlockedExchangePointer((volatile PVOID *)&Semaphore->hsem, hSem);
00145    }
00146    else
00147    {
00148       /* Wait for the other thread to create the semaphore */
00149       ASSERT(Semaphore->lCount > 1);
00150       ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL);
00151       while (Semaphore->hsem == NULL);
00152    }
00153 
00154    return TRUE;
00155 }
00156 
00157 /*
00158  * @implemented
00159  */
00160 VOID APIENTRY
00161 EngDeleteSafeSemaphore(
00162    IN OUT ENGSAFESEMAPHORE *Semaphore)
00163 {
00164    if (InterlockedDecrement(&Semaphore->lCount) == 0)
00165    {
00166       /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
00167       EngDeleteSemaphore(Semaphore->hsem);
00168       (void)InterlockedExchangePointer((volatile PVOID *)&Semaphore->hsem, NULL);
00169    }
00170 }
00171 
00172 /* EOF */

Generated on Sat May 26 2012 04:37:09 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.