11 EngCreateSemaphore(VOID
)
13 // www.osr.com/ddk/graphics/gdifncs_95lz.htm
14 PERESOURCE psem
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(ERESOURCE
), GDITAG_SEMAPHORE
);
18 if (!NT_SUCCESS(ExInitializeResourceLite(psem
)))
20 ExFreePoolWithTag ( psem
, GDITAG_SEMAPHORE
);
24 return (HSEMAPHORE
)psem
;
29 IntGdiAcquireSemaphore(HSEMAPHORE hsem
)
31 KeEnterCriticalRegion();
32 ExAcquireResourceExclusiveLite ((PERESOURCE
)hsem
, TRUE
);
40 EngAcquireSemaphore(IN HSEMAPHORE hsem
)
42 // www.osr.com/ddk/graphics/gdifncs_14br.htm
43 PTHREADINFO W32Thread
;
45 IntGdiAcquireSemaphore(hsem
);
46 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
47 if (W32Thread
) W32Thread
->dwEngAcquireCount
++;
53 IntGdiReleaseSemaphore ( HSEMAPHORE hsem
)
55 ExReleaseResourceLite((PERESOURCE
)hsem
);
56 KeLeaveCriticalRegion();
64 EngReleaseSemaphore ( IN HSEMAPHORE hsem
)
66 // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
67 PTHREADINFO W32Thread
;
69 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
70 if (W32Thread
) --W32Thread
->dwEngAcquireCount
;
71 IntGdiReleaseSemaphore(hsem
);
76 EngAcquireSemaphoreShared(
82 ExEnterCriticalRegionAndAcquireResourceShared((PERESOURCE
)hsem
);
83 pti
= PsGetThreadWin32Thread(PsGetCurrentThread());
84 if (pti
) ++pti
->dwEngAcquireCount
;
92 EngDeleteSemaphore ( IN HSEMAPHORE hsem
)
94 // www.osr.com/ddk/graphics/gdifncs_13c7.htm
97 ExDeleteResourceLite((PERESOURCE
)hsem
);
98 ExFreePoolWithTag((PVOID
)hsem
, GDITAG_SEMAPHORE
);
106 EngIsSemaphoreOwned ( IN HSEMAPHORE hsem
)
108 // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
110 return (((PERESOURCE
)hsem
)->ActiveCount
> 0);
118 EngIsSemaphoreOwnedByCurrentThread ( IN HSEMAPHORE hsem
)
120 // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
122 return ExIsResourceAcquiredExclusiveLite((PERESOURCE
)hsem
);
129 EngInitializeSafeSemaphore(
130 OUT ENGSAFESEMAPHORE
*Semaphore
)
134 if (InterlockedIncrement(&Semaphore
->lCount
) == 1)
136 /* Create the semaphore */
137 hSem
= EngCreateSemaphore();
140 InterlockedDecrement(&Semaphore
->lCount
);
143 /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
144 and delete semaphore if already initialized! */
145 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, hSem
);
149 /* Wait for the other thread to create the semaphore */
150 ASSERT(Semaphore
->lCount
> 1);
151 ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL
);
152 while (Semaphore
->hsem
== NULL
);
162 EngDeleteSafeSemaphore(
163 IN OUT ENGSAFESEMAPHORE
*Semaphore
)
165 if (InterlockedDecrement(&Semaphore
->lCount
) == 0)
167 /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
168 EngDeleteSemaphore(Semaphore
->hsem
);
169 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, NULL
);