-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsemaphore_windows.go
127 lines (106 loc) · 3.18 KB
/
semaphore_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//go:build windows
// +build windows
package jumpboot
import (
"fmt"
"sync"
"unsafe"
"golang.org/x/sys/windows"
)
var (
kernel32DLL *windows.LazyDLL
procCreateSemaphore *windows.LazyProc
procOpenSemaphore *windows.LazyProc
procReleaseSemaphore *windows.LazyProc
procWaitForSingleObject *windows.LazyProc
procCloseHandle *windows.LazyProc
initOnce sync.Once
)
func initProcs() {
kernel32DLL = windows.NewLazySystemDLL("kernel32.dll")
procCreateSemaphore = kernel32DLL.NewProc("CreateSemaphoreW")
procOpenSemaphore = kernel32DLL.NewProc("OpenSemaphoreW")
procReleaseSemaphore = kernel32DLL.NewProc("ReleaseSemaphore")
procWaitForSingleObject = kernel32DLL.NewProc("WaitForSingleObject")
procCloseHandle = kernel32DLL.NewProc("CloseHandle")
}
type windowsSemaphore struct {
handle windows.Handle
name string
}
func NewSemaphore(name string, initialValue int) (Semaphore, error) {
initOnce.Do(initProcs)
utf16Name, err := windows.UTF16PtrFromString(name)
if err != nil {
return nil, fmt.Errorf("failed to convert semaphore name: %w", err)
}
handle, _, err := procCreateSemaphore.Call(
0,
uintptr(initialValue),
0x7fffffff,
uintptr(unsafe.Pointer(utf16Name)),
)
if handle == 0 {
return nil, fmt.Errorf("failed to create semaphore: %w", err)
}
return &windowsSemaphore{handle: windows.Handle(handle), name: name}, nil
}
func OpenSemaphore(name string) (Semaphore, error) {
initOnce.Do(initProcs)
utf16Name, err := windows.UTF16PtrFromString(name)
if err != nil {
return nil, fmt.Errorf("failed to convert semaphore name: %w", err)
}
handle, _, err := procOpenSemaphore.Call(
windows.SEMAPHORE_ALL_ACCESS,
0,
uintptr(unsafe.Pointer(utf16Name)),
)
if handle == 0 {
return nil, fmt.Errorf("failed to open semaphore: %w", err)
}
return &windowsSemaphore{handle: windows.Handle(handle), name: name}, nil
}
func (s *windowsSemaphore) Acquire() error {
ret, _, err := procWaitForSingleObject.Call(uintptr(s.handle), windows.INFINITE)
if ret != windows.WAIT_OBJECT_0 {
return fmt.Errorf("failed to acquire semaphore: %w", err)
}
return nil
}
func (s *windowsSemaphore) Release() error {
ret, _, err := procReleaseSemaphore.Call(uintptr(s.handle), 1, 0)
if ret == 0 {
return fmt.Errorf("failed to release semaphore: %w", err)
}
return nil
}
func (s *windowsSemaphore) TryAcquire() (bool, error) {
ret, _, err := procWaitForSingleObject.Call(uintptr(s.handle), 0)
switch ret {
case windows.WAIT_OBJECT_0:
return true, nil
case uintptr(windows.WAIT_TIMEOUT):
return false, nil
default:
return false, fmt.Errorf("error trying to acquire semaphore: %w", err)
}
}
func (s *windowsSemaphore) AcquireTimeout(timeoutMs int) (bool, error) {
ret, _, err := procWaitForSingleObject.Call(uintptr(s.handle), uintptr(timeoutMs))
switch ret {
case windows.WAIT_OBJECT_0:
return true, nil
case uintptr(windows.WAIT_TIMEOUT):
return false, nil
default:
return false, fmt.Errorf("error acquiring semaphore with timeout: %w", err)
}
}
func (s *windowsSemaphore) Close() error {
ret, _, err := procCloseHandle.Call(uintptr(s.handle))
if ret == 0 {
return fmt.Errorf("failed to close semaphore: %w", err)
}
return nil
}