// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build unix || (js && wasm) || wasip1

package os

import (
	
	
	
	
)

func ( *Process) () ( *ProcessState,  error) {
	if .Pid == -1 {
		return nil, syscall.EINVAL
	}

	// If we can block until Wait4 will succeed immediately, do so.
	,  := .blockUntilWaitable()
	if  != nil {
		return nil, 
	}
	if  {
		// Mark the process done now, before the call to Wait4,
		// so that Process.signal will not send a signal.
		.setDone()
		// Acquire a write lock on sigMu to wait for any
		// active call to the signal method to complete.
		.sigMu.Lock()
		.sigMu.Unlock()
	}

	var (
		 syscall.WaitStatus
		 syscall.Rusage
		   int
		      error
	)
	for {
		,  = syscall.Wait4(.Pid, &, 0, &)
		if  != syscall.EINTR {
			break
		}
	}
	if  != nil {
		return nil, NewSyscallError("wait", )
	}
	.setDone()
	 = &ProcessState{
		pid:    ,
		status: ,
		rusage: &,
	}
	return , nil
}

func ( *Process) ( Signal) error {
	if .Pid == -1 {
		return errors.New("os: process already released")
	}
	if .Pid == 0 {
		return errors.New("os: process not initialized")
	}
	.sigMu.RLock()
	defer .sigMu.RUnlock()
	if .done() {
		return ErrProcessDone
	}
	,  := .(syscall.Signal)
	if ! {
		return errors.New("os: unsupported signal type")
	}
	if  := syscall.Kill(.Pid, );  != nil {
		if  == syscall.ESRCH {
			return ErrProcessDone
		}
		return 
	}
	return nil
}

func ( *Process) () error {
	// NOOP for unix.
	.Pid = -1
	// no need for a finalizer anymore
	runtime.SetFinalizer(, nil)
	return nil
}

func findProcess( int) ( *Process,  error) {
	// NOOP for unix.
	return newProcess(, 0), nil
}

func ( *ProcessState) () time.Duration {
	return time.Duration(.rusage.Utime.Nano()) * time.Nanosecond
}

func ( *ProcessState) () time.Duration {
	return time.Duration(.rusage.Stime.Nano()) * time.Nanosecond
}