Files
Reims-Go/internal/plugin/sidecarproc/vmjob_other.go
T
千川白浪 9006e6d9a7
CI / verify (push) Canceled after 0s
CI / rolling-release (push) Canceled after 0s
refactor: 抽离 HostAPIService 并将 internal/plugin 拆分为四层无环子包 (#202)
2026-12-05 06:05:41 -07:00

82 lines
1.5 KiB
Go

//go:build !windows
package sidecarproc
import (
"sync"
"syscall"
)
// VMJob approximates the Windows per-VM job with a process group: it tracks
// the root process and the group it leads. Reims-Go runs VMs on Windows; this
// keeps the package compiling and testable elsewhere.
type VMJob struct {
mu sync.Mutex
pgid int
done chan struct{}
doneOnce sync.Once
}
func NewVMJob() (*VMJob, error) { return &VMJob{done: make(chan struct{})}, nil }
func (j *VMJob) markDone() { j.doneOnce.Do(func() { close(j.done) }) }
// Done is closed once the root process has exited.
func (j *VMJob) Done() <-chan struct{} { return j.done }
// RootExited closes Done: without a job object the root is the whole tree.
func (j *VMJob) RootExited() {
if j != nil {
j.markDone()
}
}
func (j *VMJob) Assign(pid int) error {
if j == nil || pid <= 0 {
return nil
}
j.mu.Lock()
j.pgid = pid
j.mu.Unlock()
return nil
}
func (j *VMJob) PIDs() []uint32 {
if j == nil {
return nil
}
j.mu.Lock()
defer j.mu.Unlock()
select {
case <-j.done:
return nil
default:
}
if j.pgid <= 0 {
return nil
}
return []uint32{uint32(j.pgid)}
}
// SetAffinity is a no-op off Windows.
func (j *VMJob) SetAffinity(uint64) error { return nil }
func (j *VMJob) Terminate() error {
if j == nil {
return nil
}
j.mu.Lock()
pgid := j.pgid
j.mu.Unlock()
if pgid > 0 {
return syscall.Kill(-pgid, syscall.SIGKILL)
}
return nil
}
func (j *VMJob) Close() {
if j != nil {
j.markDone()
}
}