9 __drv_allocatesMem(Mem
)
10 _Post_writable_byte_size_(sizeof(ERESOURCE
))
16 // www.osr.com/ddk/graphics/gdifncs_95lz.htm
17 PERESOURCE psem
= ExAllocatePoolWithTag(NonPagedPool
,
23 if (!NT_SUCCESS(ExInitializeResourceLite(psem
)))
25 ExFreePoolWithTag(psem
, GDITAG_SEMAPHORE
);
29 return (HSEMAPHORE
)psem
;
35 _Requires_lock_not_held_(*hsem
)
36 _Acquires_exclusive_lock_(*hsem
)
37 _Acquires_lock_(_Global_critical_region_
)
41 _Inout_ HSEMAPHORE hsem
)
43 // www.osr.com/ddk/graphics/gdifncs_14br.htm
44 PTHREADINFO W32Thread
;
46 /* On Windows a NULL hsem is ignored */
49 DPRINT1("EngAcquireSemaphore called with hsem == NULL!\n");
53 ExEnterCriticalRegionAndAcquireResourceExclusive((PERESOURCE
)hsem
);
54 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
55 if (W32Thread
) W32Thread
->dwEngAcquireCount
++;
61 _Requires_lock_held_(*hsem
)
62 _Releases_lock_(*hsem
)
63 _Releases_lock_(_Global_critical_region_
)
67 _Inout_ HSEMAPHORE hsem
)
69 // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
70 PTHREADINFO W32Thread
;
72 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
73 if (W32Thread
) --W32Thread
->dwEngAcquireCount
;
74 ExReleaseResourceAndLeaveCriticalRegion((PERESOURCE
)hsem
);
77 _Acquires_lock_(_Global_critical_region_
)
78 _Requires_lock_not_held_(*hsem
)
79 _Acquires_shared_lock_(*hsem
)
82 EngAcquireSemaphoreShared(
83 _Inout_ HSEMAPHORE hsem
)
88 ExEnterCriticalRegionAndAcquireResourceShared((PERESOURCE
)hsem
);
89 pti
= PsGetThreadWin32Thread(PsGetCurrentThread());
90 if (pti
) ++pti
->dwEngAcquireCount
;
96 _Requires_lock_not_held_(*hsem
)
100 _Inout_
__drv_freesMem(Mem
) HSEMAPHORE hsem
)
102 // www.osr.com/ddk/graphics/gdifncs_13c7.htm
105 ExDeleteResourceLite((PERESOURCE
)hsem
);
106 ExFreePoolWithTag((PVOID
)hsem
, GDITAG_SEMAPHORE
);
115 _In_ HSEMAPHORE hsem
)
117 // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
119 return (((PERESOURCE
)hsem
)->ActiveCount
> 0);
127 EngIsSemaphoreOwnedByCurrentThread(
128 _In_ HSEMAPHORE hsem
)
130 // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
132 return ExIsResourceAcquiredExclusiveLite((PERESOURCE
)hsem
);
140 EngInitializeSafeSemaphore(
141 _Out_ ENGSAFESEMAPHORE
*Semaphore
)
145 if (InterlockedIncrement(&Semaphore
->lCount
) == 1)
147 /* Create the semaphore */
148 hSem
= EngCreateSemaphore();
151 InterlockedDecrement(&Semaphore
->lCount
);
154 /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
155 and delete semaphore if already initialized! */
156 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, hSem
);
160 /* Wait for the other thread to create the semaphore */
161 ASSERT(Semaphore
->lCount
> 1);
162 ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL
);
163 while (Semaphore
->hsem
== NULL
);
174 EngDeleteSafeSemaphore(
175 _Inout_ _Post_invalid_ ENGSAFESEMAPHORE
*pssem
)
177 if (InterlockedDecrement(&pssem
->lCount
) == 0)
179 /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
180 EngDeleteSemaphore(pssem
->hsem
);
181 (void)InterlockedExchangePointer((volatile PVOID
*)&pssem
->hsem
, NULL
);