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 ExEnterCriticalRegionAndAcquireResourceExclusive((PERESOURCE
)hsem
);
47 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
48 if (W32Thread
) W32Thread
->dwEngAcquireCount
++;
54 _Requires_lock_held_(*hsem
)
55 _Releases_lock_(*hsem
)
56 _Releases_lock_(_Global_critical_region_
)
60 _Inout_ HSEMAPHORE hsem
)
62 // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
63 PTHREADINFO W32Thread
;
65 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
66 if (W32Thread
) --W32Thread
->dwEngAcquireCount
;
67 ExReleaseResourceAndLeaveCriticalRegion((PERESOURCE
)hsem
);
70 _Acquires_lock_(_Global_critical_region_
)
71 _Requires_lock_not_held_(*hsem
)
72 _Acquires_shared_lock_(*hsem
)
75 EngAcquireSemaphoreShared(
76 _Inout_ HSEMAPHORE hsem
)
81 ExEnterCriticalRegionAndAcquireResourceShared((PERESOURCE
)hsem
);
82 pti
= PsGetThreadWin32Thread(PsGetCurrentThread());
83 if (pti
) ++pti
->dwEngAcquireCount
;
89 _Requires_lock_not_held_(*hsem
)
93 _Inout_
__drv_freesMem(Mem
) HSEMAPHORE hsem
)
95 // www.osr.com/ddk/graphics/gdifncs_13c7.htm
98 ExDeleteResourceLite((PERESOURCE
)hsem
);
99 ExFreePoolWithTag((PVOID
)hsem
, GDITAG_SEMAPHORE
);
108 _In_ HSEMAPHORE hsem
)
110 // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
112 return (((PERESOURCE
)hsem
)->ActiveCount
> 0);
120 EngIsSemaphoreOwnedByCurrentThread(
121 _In_ HSEMAPHORE hsem
)
123 // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
125 return ExIsResourceAcquiredExclusiveLite((PERESOURCE
)hsem
);
133 EngInitializeSafeSemaphore(
134 _Out_ ENGSAFESEMAPHORE
*Semaphore
)
138 if (InterlockedIncrement(&Semaphore
->lCount
) == 1)
140 /* Create the semaphore */
141 hSem
= EngCreateSemaphore();
144 InterlockedDecrement(&Semaphore
->lCount
);
147 /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
148 and delete semaphore if already initialized! */
149 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, hSem
);
153 /* Wait for the other thread to create the semaphore */
154 ASSERT(Semaphore
->lCount
> 1);
155 ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL
);
156 while (Semaphore
->hsem
== NULL
);
167 EngDeleteSafeSemaphore(
168 _Inout_ _Post_invalid_ ENGSAFESEMAPHORE
*pssem
)
170 if (InterlockedDecrement(&pssem
->lCount
) == 0)
172 /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
173 EngDeleteSemaphore(pssem
->hsem
);
174 (void)InterlockedExchangePointer((volatile PVOID
*)&pssem
->hsem
, NULL
);