-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlauncher_singleinstance_windows.go
More file actions
47 lines (41 loc) · 1003 Bytes
/
launcher_singleinstance_windows.go
File metadata and controls
47 lines (41 loc) · 1003 Bytes
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
//go:build windows
package main
import (
"errors"
"fmt"
"os"
"golang.org/x/sys/windows"
)
type windowsLauncherSingleInstanceLock struct {
handle windows.Handle
owned bool
}
func acquireLauncherSingleInstanceLock(debugPort uint16) (launcherSingleInstanceLock, bool, error) {
name := fmt.Sprintf(`Local\CodexToolsLauncher_%d`, debugPort)
namePtr, err := windows.UTF16PtrFromString(name)
if err != nil {
return nil, false, err
}
handle, err := windows.CreateMutex(nil, true, namePtr)
if err != nil {
if errors.Is(err, os.ErrExist) {
if handle != 0 {
_ = windows.CloseHandle(handle)
}
return nil, false, nil
}
return nil, false, err
}
return &windowsLauncherSingleInstanceLock{handle: handle, owned: true}, true, nil
}
func (lock *windowsLauncherSingleInstanceLock) release() {
if lock == nil || lock.handle == 0 {
return
}
if lock.owned {
_ = windows.ReleaseMutex(lock.handle)
}
_ = windows.CloseHandle(lock.handle)
lock.handle = 0
lock.owned = false
}