11 EngCreateSemaphore ( VOID
)
13 // www.osr.com/ddk/graphics/gdifncs_95lz.htm
14 PERESOURCE psem
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(ERESOURCE
), GDITAG_SEMAPHORE
);
17 if ( !NT_SUCCESS(ExInitializeResourceLite ( psem
)) )
19 ExFreePoolWithTag ( psem
, GDITAG_SEMAPHORE
);
22 return (HSEMAPHORE
)psem
;
27 IntGdiAcquireSemaphore ( HSEMAPHORE hsem
)
29 KeEnterCriticalRegion();
30 ExAcquireResourceExclusiveLite ( (PERESOURCE
)hsem
, TRUE
);
38 EngAcquireSemaphore ( IN HSEMAPHORE hsem
)
40 // www.osr.com/ddk/graphics/gdifncs_14br.htm
41 PTHREADINFO W32Thread
;
43 IntGdiAcquireSemaphore ( hsem
);
44 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
45 if (W32Thread
) W32Thread
->dwEngAcquireCount
++;
51 IntGdiReleaseSemaphore ( HSEMAPHORE hsem
)
53 ExReleaseResourceLite ( (PERESOURCE
)hsem
);
54 KeLeaveCriticalRegion();
62 EngReleaseSemaphore ( IN HSEMAPHORE hsem
)
64 // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
65 PTHREADINFO W32Thread
;
67 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
68 if (W32Thread
) --W32Thread
->dwEngAcquireCount
;
69 IntGdiReleaseSemaphore ( hsem
);
74 EngAcquireSemaphoreShared(
80 ExEnterCriticalRegionAndAcquireResourceShared((PERESOURCE
)hsem
);
81 pti
= PsGetThreadWin32Thread(PsGetCurrentThread());
82 if (pti
) ++pti
->dwEngAcquireCount
;
90 EngDeleteSemaphore ( IN HSEMAPHORE hsem
)
92 // www.osr.com/ddk/graphics/gdifncs_13c7.htm
95 ExDeleteResourceLite((PERESOURCE
)hsem
);
97 ExFreePoolWithTag( (PVOID
)hsem
, GDITAG_SEMAPHORE
);
105 EngIsSemaphoreOwned ( IN HSEMAPHORE hsem
)
107 // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
109 return (((PERESOURCE
)hsem
)->ActiveCount
> 0);
117 EngIsSemaphoreOwnedByCurrentThread ( IN HSEMAPHORE hsem
)
119 // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
121 return ExIsResourceAcquiredExclusiveLite ( (PERESOURCE
)hsem
);
128 EngInitializeSafeSemaphore(
129 OUT ENGSAFESEMAPHORE
*Semaphore
)
133 if (InterlockedIncrement(&Semaphore
->lCount
) == 1)
135 /* Create the semaphore */
136 hSem
= EngCreateSemaphore();
139 InterlockedDecrement(&Semaphore
->lCount
);
142 /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
143 and delete semaphore if already initialized! */
144 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, hSem
);
148 /* Wait for the other thread to create the semaphore */
149 ASSERT(Semaphore
->lCount
> 1);
150 ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL
);
151 while (Semaphore
->hsem
== NULL
);
161 EngDeleteSafeSemaphore(
162 IN OUT ENGSAFESEMAPHORE
*Semaphore
)
164 if (InterlockedDecrement(&Semaphore
->lCount
) == 0)
166 /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
167 EngDeleteSemaphore(Semaphore
->hsem
);
168 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, NULL
);