@@ -66,14 +66,41 @@ bool JSemaphore::Wait(unsigned int time_ms) {
66
66
}
67
67
}
68
68
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
+
69
89
int JSemaphore::GetValue () {
90
+ SEMAPHORE_BASIC_INFORMATION BasicInfo;
91
+ LONG retval;
70
92
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 );
76
97
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
+ }
78
105
}
79
106
0 commit comments