aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorleongross <[email protected]>2024-05-07 17:06:19 +0200
committerRon Evans <[email protected]>2024-06-06 16:24:33 +0200
commit3023ba584bb284fef263c4f4280a7f0e38929f1f (patch)
treecd373af02aa8877229d73ce354992ecb884dbfab
parent3a8ef33c7201ae7cd85c3ce1d012ecb707e32f67 (diff)
downloadtinygo-3023ba584bb284fef263c4f4280a7f0e38929f1f.tar.gz
tinygo-3023ba584bb284fef263c4f4280a7f0e38929f1f.zip
Add process.Release for unix
Signed-off-by: leongross <[email protected]>
-rw-r--r--src/os/exec.go10
-rw-r--r--src/os/exec_posix.go9
-rw-r--r--src/os/exec_test.go25
3 files changed, 30 insertions, 14 deletions
diff --git a/src/os/exec.go b/src/os/exec.go
index 2adcf3c1b..cf295e6fb 100644
--- a/src/os/exec.go
+++ b/src/os/exec.go
@@ -62,6 +62,9 @@ func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)
}
func (p *Process) Wait() (*ProcessState, error) {
+ if p.Pid == -1 {
+ return nil, syscall.EINVAL
+ }
return nil, ErrNotImplemented
}
@@ -78,6 +81,13 @@ func Ignore(sig ...Signal) {
return
}
+// Release releases any resources associated with the Process p,
+// rendering it unusable in the future.
+// Release only needs to be called if Wait is not.
+func (p *Process) Release() error {
+ return p.release()
+}
+
// Keep compatibility with golang and always succeed and return new proc with pid on Linux.
func FindProcess(pid int) (*Process, error) {
return findProcess(pid)
diff --git a/src/os/exec_posix.go b/src/os/exec_posix.go
index 84843a281..0a968d9a1 100644
--- a/src/os/exec_posix.go
+++ b/src/os/exec_posix.go
@@ -7,6 +7,7 @@
package os
import (
+ "runtime"
"syscall"
)
@@ -24,3 +25,11 @@ var (
func findProcess(pid int) (*Process, error) {
return &Process{Pid: pid}, nil
}
+
+func (p *Process) release() error {
+ // NOOP for unix.
+ p.Pid = -1
+ // no need for a finalizer anymore
+ runtime.SetFinalizer(p, nil)
+ return nil
+}
diff --git a/src/os/exec_test.go b/src/os/exec_test.go
index 032763abf..e960cea02 100644
--- a/src/os/exec_test.go
+++ b/src/os/exec_test.go
@@ -12,21 +12,18 @@ import (
func TestFindProcess(t *testing.T) {
// NOTE: For now, we only test the Linux case since only exec_posix.go is currently the only implementation.
- if runtime.GOOS == "linux" {
- // Linux guarantees that there is pid 0
- proc, err := FindProcess(0)
- if err != nil {
- t.Error("FindProcess(0): wanted err == nil, got %v:", err)
- }
-
- if proc.Pid != 0 {
- t.Error("Expected pid 0, got: ", proc.Pid)
- }
+ // Linux guarantees that there is pid 0
+ proc, err := FindProcess(0)
+ if err != nil {
+ t.Error("FindProcess(0): wanted err == nil, got %v:", err)
+ }
- pid0 := Process{Pid: 0}
- if *proc != pid0 {
- t.Error("Expected &Process{Pid: 0}, got", *proc)
- }
+ if proc.Pid != 0 {
+ t.Error("Expected pid 0, got: ", proc.Pid)
}
+ pid0 := Process{Pid: 0}
+ if *proc != pid0 {
+ t.Error("Expected &Process{Pid: 0}, got", *proc)
+ }
}