Skip to content

Commit 4896d4b

Browse files
sapiersapier
sapier
authored and
sapier
committedJan 15, 2014
Fix win32 reading semaphore count not working (broke all queues)
1 parent f42f017 commit 4896d4b

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed
 

‎src/jthread/win32/jsemaphore.cpp

+33-6
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,41 @@ bool JSemaphore::Wait(unsigned int time_ms) {
6666
}
6767
}
6868

69+
typedef LONG (NTAPI *_NtQuerySemaphore)(
70+
HANDLE SemaphoreHandle,
71+
DWORD SemaphoreInformationClass,
72+
PVOID SemaphoreInformation,
73+
ULONG SemaphoreInformationLength,
74+
PULONG ReturnLength OPTIONAL
75+
);
76+
77+
typedef struct _SEMAPHORE_BASIC_INFORMATION {
78+
ULONG CurrentCount;
79+
ULONG MaximumCount;
80+
} SEMAPHORE_BASIC_INFORMATION;
81+
82+
/* Note: this will only work as long as jthread is directly linked to application */
83+
/* it's gonna fail if someone tries to build jthread as dll */
84+
static _NtQuerySemaphore NtQuerySemaphore =
85+
(_NtQuerySemaphore)
86+
GetProcAddress
87+
(GetModuleHandle ("ntdll.dll"), "NtQuerySemaphore");
88+
6989
int JSemaphore::GetValue() {
90+
SEMAPHORE_BASIC_INFORMATION BasicInfo;
91+
LONG retval;
7092

71-
long int retval = 0;
72-
ReleaseSemaphore(
73-
m_hSemaphore,
74-
0,
75-
&retval);
93+
assert(NtQuerySemaphore);
94+
95+
retval = NtQuerySemaphore (m_hSemaphore, 0,
96+
&BasicInfo, sizeof (SEMAPHORE_BASIC_INFORMATION), NULL);
7697

77-
return retval;
98+
if (retval == ERROR_SUCCESS)
99+
{
100+
return BasicInfo.CurrentCount;
101+
}
102+
else {
103+
assert("unable to read semaphore count" == 0);
104+
}
78105
}
79106

0 commit comments

Comments
 (0)
Please sign in to comment.