Skip to content

Commit 0ebf18d

Browse files
committed
update the thread priority of the loop on Windows
1 parent 96a1006 commit 0ebf18d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

api_wasapi_windows.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ import (
2424
)
2525

2626
var (
27-
ole32 = windows.NewLazySystemDLL("ole32")
27+
kernel32 = windows.NewLazySystemDLL("kernel32")
28+
ole32 = windows.NewLazySystemDLL("ole32")
2829
)
2930

3031
var (
32+
procGetCurrentThread = kernel32.NewProc("GetCurrentThread")
33+
procSetThreadPriority = kernel32.NewProc("SetThreadPriority")
34+
3135
procCoCreateInstance = ole32.NewProc("CoCreateInstance")
3236
)
3337

@@ -49,6 +53,7 @@ const (
4953
_SPEAKER_FRONT_CENTER = 0x4
5054
_SPEAKER_FRONT_LEFT = 0x1
5155
_SPEAKER_FRONT_RIGHT = 0x2
56+
_THREAD_PRIORITY_ABOVE_NORMAL = 1
5257
_WAVE_FORMAT_EXTENSIBLE = 0xfffe
5358
)
5459

@@ -188,6 +193,16 @@ func _CoCreateInstance(rclsid *windows.GUID, pUnkOuter unsafe.Pointer, dwClsCont
188193
return v, nil
189194
}
190195

196+
func _GetCurrentThread() windows.Handle {
197+
r, _, _ := procGetCurrentThread.Call()
198+
return windows.Handle(uint32(r))
199+
}
200+
201+
func _SetThreadPriority(hThread windows.Handle, nPriority int) bool {
202+
r, _, _ := procSetThreadPriority.Call(uintptr(hThread), uintptr(nPriority))
203+
return uint32(r) != 0
204+
}
205+
191206
type _IAudioClient2 struct {
192207
vtbl *_IAudioClient2_Vtbl
193208
}

driver_wasapi_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ func (c *wasapiContext) loop() error {
316316
runtime.LockOSThread()
317317
defer runtime.UnlockOSThread()
318318

319+
_SetThreadPriority(_GetCurrentThread(), _THREAD_PRIORITY_ABOVE_NORMAL)
320+
319321
// S_FALSE is returned when CoInitializeEx is nested. This is a successful case.
320322
if err := windows.CoInitializeEx(0, windows.COINIT_MULTITHREADED); err != nil && !errors.Is(err, syscall.Errno(windows.S_FALSE)) {
321323
c.client.Stop()

0 commit comments

Comments
 (0)