ReactOS  0.4.13-dev-242-g611e6d7
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_)
38 VOID
41  _Inout_ HSEMAPHORE hsem)
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_)
64 VOID
67  _Inout_ HSEMAPHORE hsem)
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_)
87 VOID
88 NTAPI
89 EngAcquireSemaphoreShared(
90  _Inout_ HSEMAPHORE hsem)
91 {
92  PTHREADINFO pti;
93 
94  ASSERT(hsem);
97  if (pti) ++pti->dwEngAcquireCount;
98 }
99 
100 /*
101  * @implemented
102  */
104 VOID
105 APIENTRY
108 {
109  // www.osr.com/ddk/graphics/gdifncs_13c7.htm
110  ASSERT(hsem);
111 
114 }
115 
116 /*
117  * @implemented
118  */
119 BOOL
120 APIENTRY
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  */
132 BOOL
133 APIENTRY
135  _In_ HSEMAPHORE hsem)
136 {
137  // www.osr.com/ddk/graphics/gdifncs_9yxz.htm
138  ASSERT(hsem);
140 }
141 
142 /*
143  * @implemented
144  */
145 BOOL
146 APIENTRY
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  */
179 VOID
180 APIENTRY
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 */
VOID WINAPI EngReleaseSemaphore(IN HSEMAPHORE hsem)
Definition: eng.c:235
#define ASSERT_IRQL_LESS_OR_EQUAL(x)
Definition: debug.h:251
PVOID NTAPI PsGetThreadWin32Thread(IN PETHREAD Thread)
Definition: thread.c:795
#define TRUE
Definition: types.h:120
#define _Post_invalid_
Definition: no_sal2.h:457
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceShared(IN PERESOURCE Resource)
Definition: resource.c:2218
BOOL APIENTRY EngIsSemaphoreOwnedByCurrentThread(_In_ HSEMAPHORE hsem)
Definition: semaphor.c:134
VOID APIENTRY EngDeleteSafeSemaphore(_Inout_ _Post_invalid_ ENGSAFESEMAPHORE *pssem)
Definition: semaphor.c:181
NTSTATUS ExInitializeResourceLite(PULONG res)
Definition: env_spec_w32.h:641
NTSTATUS NTAPI ExDeleteResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1456
_Acquires_shared_lock_(Vcb->Resource)) FINISHED FatAcquireSharedVcb(IN PIRP_CONTEXT IrpContext
_Requires_lock_not_held_(Vcb->fcb_lock) _Acquires_shared_lock_(Vcb -> fcb_lock) static __inline void acquire_fcb_lock_shared(device_extension *Vcb)
Definition: btrfs_drv.h:919
_Acquires_exclusive_lock_(Vcb->Resource)) FINISHED FatAcquireExclusiveVcb_Real(IN PIRP_CONTEXT IrpContext
#define _Post_writable_byte_size_(size)
Definition: no_sal2.h:467
#define GDITAG_SEMAPHORE
Definition: tags.h:158
_Requires_lock_not_held_ hsem VOID APIENTRY EngDeleteSemaphore(_Inout_ __drv_freesMem(Mem) HSEMAPHORE hsem)
Definition: semaphor.c:106
ERESOURCE * PERESOURCE
Definition: env_spec_w32.h:595
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
unsigned int BOOL
Definition: ntddk_ex.h:94
_Requires_lock_not_held_ hsem _Acquires_exclusive_lock_ hsem _Acquires_lock_(_Global_critical_region_)
Definition: semaphor.c:37
_Requires_lock_held_ hsem _Releases_lock_ hsem _Releases_lock_(_Global_critical_region_)
Definition: semaphor.c:63
ENGAPI VOID APIENTRY EngAcquireSemaphore(_Inout_ HSEMAPHORE hsem)
smooth NULL
Definition: ftsmooth.c:416
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite(IN PERESOURCE Resource)
Definition: resource.c:1619
#define _Out_
Definition: no_sal2.h:323
__drv_allocatesMem(Mem)
Definition: semaphor.c:9
#define InterlockedExchangePointer(Target, Value)
Definition: dshow.h:45
DWORD_PTR HSEMAPHORE
Definition: axcore.idl:60
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define _Inout_
Definition: no_sal2.h:244
BOOL APIENTRY EngInitializeSafeSemaphore(_Out_ ENGSAFESEMAPHORE *Semaphore)
Definition: semaphor.c:147
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define InterlockedDecrement
Definition: armddk.h:52
VOID FASTCALL ExReleaseResourceAndLeaveCriticalRegion(IN PERESOURCE Resource)
Definition: resource.c:2278
BOOL APIENTRY EngIsSemaphoreOwned(_In_ HSEMAPHORE hsem)
Definition: semaphor.c:121
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define _In_
Definition: no_sal2.h:204
#define InterlockedIncrement
Definition: armddk.h:53
#define __drv_freesMem(kind)
Definition: driverspecs.h:254
#define DPRINT1
Definition: precomp.h:8
HSEMAPHORE WINAPI EngCreateSemaphore(VOID)
Definition: eng.c:75
ULONG ERESOURCE
Definition: env_spec_w32.h:594
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceExclusive(IN PERESOURCE Resource)
Definition: resource.c:2189
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define _Requires_lock_held_(a)
Definition: btrfs_drv.h:168
#define APIENTRY
Definition: api.h:79