mirror of https://github.com/Hilbis/Hilbish
feat: add job.background function
parent
2940b82a91
commit
4c8cb686a0
18
job.go
18
job.go
|
@ -111,6 +111,7 @@ func (j *job) lua() rt.Value {
|
|||
jobFuncs := map[string]util.LuaExport{
|
||||
"stop": {j.luaStop, 0, false},
|
||||
"start": {j.luaStart, 0, false},
|
||||
"foreground": {j.luaForeground, 0, false},
|
||||
}
|
||||
luaJob := rt.NewTable()
|
||||
util.SetExports(l, luaJob, jobFuncs)
|
||||
|
@ -146,9 +147,26 @@ func (j *job) luaStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func (j *job) luaForeground(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if !j.running {
|
||||
return nil, errors.New("job not running")
|
||||
}
|
||||
|
||||
// lua code can run in other threads and goroutines, so this exists
|
||||
jobs.foreground = true
|
||||
err := j.foreground()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobs.foreground = false
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
type jobHandler struct {
|
||||
jobs map[int]*job
|
||||
latestID int
|
||||
foreground bool // if job currently in the foreground
|
||||
mu *sync.RWMutex
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
// +build darwin linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func (j *job) foreground() error {
|
||||
if jobs.foreground {
|
||||
return errors.New("(another) job already foregrounded")
|
||||
}
|
||||
|
||||
pgid, _ := syscall.Getpgid(j.pid)
|
||||
// tcsetpgrp
|
||||
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, pgid)
|
||||
proc, _ := os.FindProcess(j.pid)
|
||||
proc.Wait()
|
||||
|
||||
hshPgid, _ := syscall.Getpgid(os.Getpid())
|
||||
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, hshPgid)
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
// +build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func (j *job) foreground() error {
|
||||
return errors.New("not supported on windows")
|
||||
}
|
Loading…
Reference in New Issue