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 /* On Windows a NULL hsem is ignored */
75 DPRINT1("EngReleaseSemaphore called with hsem == NULL!\n");
79 W32Thread
= PsGetThreadWin32Thread(PsGetCurrentThread());
80 if (W32Thread
) --W32Thread
->dwEngAcquireCount
;
81 ExReleaseResourceAndLeaveCriticalRegion((PERESOURCE
)hsem
);
84 _Acquires_lock_(_Global_critical_region_
)
85 _Requires_lock_not_held_(*hsem
)
86 _Acquires_shared_lock_(*hsem
)
89 EngAcquireSemaphoreShared(
90 _Inout_ HSEMAPHORE hsem
)
95 ExEnterCriticalRegionAndAcquireResourceShared((PERESOURCE
)hsem
);
96 pti
= PsGetThreadWin32Thread(PsGetCurrentThread());
97 if (pti
) ++pti
->dwEngAcquireCount
;
103 _Requires_lock_not_held_(*hsem
)
107 _Inout_
__drv_freesMem(Mem
) HSEMAPHORE hsem
)
109 // www.osr.com/ddk/graphics/gdifncs_13c7.htm
112 ExDeleteResourceLite((PERESOURCE
)hsem
);
113 ExFreePoolWithTag((PVOID
)hsem
, GDITAG_SEMAPHORE
);
122 _In_ HSEMAPHORE hsem
)
124 // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
126 return (((PERESOURCE
)hsem
)->ActiveCount
> 0);
134 EngIsSemaphoreOwnedByCurrentThread(
135 _In_ HSEMAPHORE hsem
)
137 // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
139 return ExIsResourceAcquiredExclusiveLite((PERESOURCE
)hsem
);
147 EngInitializeSafeSemaphore(
148 _Out_ ENGSAFESEMAPHORE
*Semaphore
)
152 if (InterlockedIncrement(&Semaphore
->lCount
) == 1)
154 /* Create the semaphore */
155 hSem
= EngCreateSemaphore();
158 InterlockedDecrement(&Semaphore
->lCount
);
161 /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
162 and delete semaphore if already initialized! */
163 (void)InterlockedExchangePointer((volatile PVOID
*)&Semaphore
->hsem
, hSem
);
167 /* Wait for the other thread to create the semaphore */
168 ASSERT(Semaphore
->lCount
> 1);
169 ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL
);
170 while (Semaphore
->hsem
== NULL
);
181 EngDeleteSafeSemaphore(
182 _Inout_ _Post_invalid_ ENGSAFESEMAPHORE
*pssem
)
184 if (InterlockedDecrement(&pssem
->lCount
) == 0)
186 /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
187 EngDeleteSemaphore(pssem
->hsem
);
188 (void)InterlockedExchangePointer((volatile PVOID
*)&pssem
->hsem
, NULL
);