ReactOS 0.4.15-dev-8339-g4028de8
semaphor.c
Go to the documentation of this file.
1#include <win32k.h>
2
3#define NDEBUG
4#include <debug.h>
5
6/*
7 * @implemented
8 */
14 VOID)
15{
16 // www.osr.com/ddk/graphics/gdifncs_95lz.htm
18 sizeof(ERESOURCE),
20 if (!psem)
21 return NULL;
22
24 {
26 return NULL;
27 }
28
29 return (HSEMAPHORE)psem;
30}
31
32/*
33 * @implemented
34 */
37_Acquires_lock_(_Global_critical_region_)
38VOID
42{
43 // www.osr.com/ddk/graphics/gdifncs_14br.htm
44 PTHREADINFO W32Thread;
45
46 /* On Windows a NULL hsem is ignored */
47 if (hsem == NULL)
48 {
49 DPRINT1("EngAcquireSemaphore called with hsem == NULL!\n");
50 return;
51 }
52
55 if (W32Thread) W32Thread->dwEngAcquireCount++;
56}
57
58/*
59 * @implemented
60 */
62_Releases_lock_(*hsem)
63_Releases_lock_(_Global_critical_region_)
64VOID
68{
69 // www.osr.com/ddk/graphics/gdifncs_5u3r.htm
70 PTHREADINFO W32Thread;
71
72 /* On Windows a NULL hsem is ignored */
73 if (hsem == NULL)
74 {
75 DPRINT1("EngReleaseSemaphore called with hsem == NULL!\n");
76 return;
77 }
78
80 if (W32Thread) --W32Thread->dwEngAcquireCount;
82}
83
84_Acquires_lock_(_Global_critical_region_)
87VOID
89EngAcquireSemaphoreShared(
91{
92 PTHREADINFO pti;
93
94 ASSERT(hsem);
97 if (pti) ++pti->dwEngAcquireCount;
98}
99
100/*
101 * @implemented
102 */
104VOID
108{
109 // www.osr.com/ddk/graphics/gdifncs_13c7.htm
110 ASSERT(hsem);
111
114}
115
116/*
117 * @implemented
118 */
119BOOL
122 _In_ HSEMAPHORE hsem)
123{
124 // www.osr.com/ddk/graphics/gdifncs_6wmf.htm
125 ASSERT(hsem);
126 return (((PERESOURCE)hsem)->ActiveCount > 0);
127}
128
129/*
130 * @implemented
131 */
132BOOL
135 _In_ HSEMAPHORE hsem)
136{
137 // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
138 ASSERT(hsem);
140}
141
142/*
143 * @implemented
144 */
145BOOL
148 _Out_ ENGSAFESEMAPHORE *Semaphore)
149{
150 HSEMAPHORE hSem;
151
152 if (InterlockedIncrement(&Semaphore->lCount) == 1)
153 {
154 /* Create the semaphore */
155 hSem = EngCreateSemaphore();
156 if (hSem == 0)
157 {
158 InterlockedDecrement(&Semaphore->lCount);
159 return FALSE;
160 }
161 /* FIXME: Not thread-safe! Check result of InterlockedCompareExchangePointer
162 and delete semaphore if already initialized! */
163 (void)InterlockedExchangePointer((volatile PVOID *)&Semaphore->hsem, hSem);
164 }
165 else
166 {
167 /* Wait for the other thread to create the semaphore */
168 ASSERT(Semaphore->lCount > 1);
170 while (Semaphore->hsem == NULL);
171 }
172
173 return TRUE;
174}
175
176/*
177 * @implemented
178 */
179VOID
183{
184 if (InterlockedDecrement(&pssem->lCount) == 0)
185 {
186 /* FIXME: Not thread-safe! Use result of InterlockedCompareExchangePointer! */
187 EngDeleteSemaphore(pssem->hsem);
188 (void)InterlockedExchangePointer((volatile PVOID *)&pssem->hsem, NULL);
189 }
190}
191
192/* EOF */
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
DWORD_PTR HSEMAPHORE
Definition: axcore.idl:60
#define DPRINT1
Definition: precomp.h:8
#define _Acquires_lock_(lock)
#define _Requires_lock_held_(lock)
#define _Acquires_exclusive_lock_(lock)
#define _Acquires_shared_lock_(lock)
#define _Requires_lock_not_held_(lock)
#define _Releases_lock_(lock)
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define APIENTRY
Definition: api.h:79
#define __drv_freesMem(kind)
Definition: driverspecs.h:272
#define __drv_allocatesMem(kind)
Definition: driverspecs.h:257
#define InterlockedExchangePointer(Target, Value)
Definition: dshow.h:45
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
NTSTATUS ExInitializeResourceLite(PULONG res)
Definition: env_spec_w32.h:641
ERESOURCE * PERESOURCE
Definition: env_spec_w32.h:595
#define ExDeleteResourceLite(res)
Definition: env_spec_w32.h:647
#define NonPagedPool
Definition: env_spec_w32.h:307
ULONG ERESOURCE
Definition: env_spec_w32.h:594
unsigned int BOOL
Definition: ntddk_ex.h:94
VOID WINAPI EngReleaseSemaphore(IN HSEMAPHORE hsem)
Definition: eng.c:235
HSEMAPHORE WINAPI EngCreateSemaphore(VOID)
Definition: eng.c:75
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define _Inout_
Definition: ms_sal.h:378
#define _Post_invalid_
Definition: ms_sal.h:695
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
#define _Post_writable_byte_size_(size)
Definition: ms_sal.h:652
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite(IN PERESOURCE Resource)
Definition: resource.c:1624
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceShared(IN PERESOURCE Resource)
Definition: resource.c:2223
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceExclusive(IN PERESOURCE Resource)
Definition: resource.c:2194
VOID FASTCALL ExReleaseResourceAndLeaveCriticalRegion(IN PERESOURCE Resource)
Definition: resource.c:2283
PVOID NTAPI PsGetThreadWin32Thread(IN PETHREAD Thread)
Definition: thread.c:795
#define ASSERT_IRQL_LESS_OR_EQUAL(x)
Definition: debug.h:251
BOOL APIENTRY EngInitializeSafeSemaphore(_Out_ ENGSAFESEMAPHORE *Semaphore)
Definition: semaphor.c:147
_Requires_lock_not_held_ hsem VOID APIENTRY EngDeleteSemaphore(_Inout_ __drv_freesMem(Mem) HSEMAPHORE hsem)
Definition: semaphor.c:106
BOOL APIENTRY EngIsSemaphoreOwnedByCurrentThread(_In_ HSEMAPHORE hsem)
Definition: semaphor.c:134
BOOL APIENTRY EngIsSemaphoreOwned(_In_ HSEMAPHORE hsem)
Definition: semaphor.c:121
VOID APIENTRY EngDeleteSafeSemaphore(_Inout_ _Post_invalid_ ENGSAFESEMAPHORE *pssem)
Definition: semaphor.c:181
#define NTAPI
Definition: typedefs.h:36
#define GDITAG_SEMAPHORE
Definition: tags.h:159
ENGAPI VOID APIENTRY EngAcquireSemaphore(_Inout_ HSEMAPHORE hsem)