// Process stores the information about a process created by StartProcess.
type Process struct {
Pid int
- handle uintptr // handle is accessed atomically on Windows
+ handle atomic.Uintptr
isdone atomic.Bool // process has been successfully waited on
sigMu sync.RWMutex // avoid race between wait and signal
}
func newProcess(pid int, handle uintptr) *Process {
- p := &Process{Pid: pid, handle: handle}
+ p := &Process{Pid: pid}
+ p.handle.Store(handle)
runtime.SetFinalizer(p, (*Process).Release)
return p
}
"errors"
"internal/syscall/windows"
"runtime"
- "sync/atomic"
"syscall"
"time"
)
func (p *Process) wait() (ps *ProcessState, err error) {
- handle := atomic.LoadUintptr(&p.handle)
+ handle := p.handle.Load()
s, e := syscall.WaitForSingleObject(syscall.Handle(handle), syscall.INFINITE)
switch s {
case syscall.WAIT_OBJECT_0:
}
func (p *Process) signal(sig Signal) error {
- handle := atomic.LoadUintptr(&p.handle)
+ handle := p.handle.Load()
if handle == uintptr(syscall.InvalidHandle) {
return syscall.EINVAL
}
}
func (p *Process) release() error {
- handle := atomic.SwapUintptr(&p.handle, uintptr(syscall.InvalidHandle))
+ handle := p.handle.Swap(uintptr(syscall.InvalidHandle))
if handle == uintptr(syscall.InvalidHandle) {
return syscall.EINVAL
}