close null_handle

This commit is contained in:
Laytan
2025-05-06 20:57:26 +02:00
parent 2bce446d08
commit bf5206968a
5 changed files with 24 additions and 24 deletions

View File

@@ -264,8 +264,10 @@ specific process, even after it has died.
**Note(linux)**: The `handle` will be referring to pidfd.
*/
Process :: struct {
pid: int,
pid: int,
handle: uintptr,
// Implementation specific state/data.
_impl: _Process,
}
Process_Open_Flags :: bit_set[Process_Open_Flag]
@@ -290,21 +292,10 @@ process_open :: proc(pid: int, flags := Process_Open_Flags {}) -> (Process, Erro
return _process_open(pid, flags)
}
/*
OS-specific process attributes.
*/
Process_Attributes :: struct {
sys_attr: _Sys_Process_Attributes,
}
/*
The description of how a process should be created.
*/
Process_Desc :: struct {
// OS-specific attributes.
sys_attr: Process_Attributes,
// The working directory of the process. If the string has length 0, the
// working directory is assumed to be the current working directory of the
// current process.

View File

@@ -362,6 +362,9 @@ _current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime
return _process_info_by_pid(get_pid(), selection, allocator)
}
@(private="package")
_Process :: struct {}
@(private="package")
_process_open :: proc(pid: int, _: Process_Open_Flags) -> (process: Process, err: Error) {
process.pid = pid
@@ -378,9 +381,6 @@ _process_open :: proc(pid: int, _: Process_Open_Flags) -> (process: Process, err
return
}
@(private="package")
_Sys_Process_Attributes :: struct {}
@(private="package")
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
TEMP_ALLOCATOR_GUARD()

View File

@@ -46,7 +46,7 @@ _current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime
return _process_info_by_pid(_get_pid(), selection, allocator)
}
_Sys_Process_Attributes :: struct {}
_Process :: struct {}
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
if len(desc.command) == 0 {

View File

@@ -44,7 +44,7 @@ _current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime
return
}
_Sys_Process_Attributes :: struct {}
_Process :: struct {}
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
err = .Unsupported

View File

@@ -418,7 +418,9 @@ _process_open :: proc(pid: int, flags: Process_Open_Flags) -> (process: Process,
}
@(private="package")
_Sys_Process_Attributes :: struct {}
_Process :: struct {
null_handle: win32.HANDLE,
}
@(private="package")
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
@@ -436,9 +438,8 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
stdout_handle: win32.HANDLE
stdin_handle: win32.HANDLE
null_handle: win32.HANDLE
if desc.stdout == nil || desc.stderr == nil || desc.stdin == nil {
null_handle := win32.CreateFileW(
process._impl.null_handle = win32.CreateFileW(
win32.L("NUL"),
win32.GENERIC_READ|win32.GENERIC_WRITE,
win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE,
@@ -450,23 +451,23 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
win32.FILE_ATTRIBUTE_NORMAL,
nil,
)
assert(null_handle != nil)
assert(process._impl.null_handle != nil)
}
if desc.stdout == nil {
stdout_handle = null_handle
stdout_handle = process._impl.null_handle
} else {
stdout_handle = win32.HANDLE((^File_Impl)(desc.stdout.impl).fd)
}
if desc.stderr == nil {
stderr_handle = null_handle
stderr_handle = process._impl.null_handle
} else {
stderr_handle = win32.HANDLE((^File_Impl)(desc.stderr.impl).fd)
}
if desc.stdin == nil {
stdin_handle = null_handle
stdin_handle = process._impl.null_handle
} else {
stdin_handle = win32.HANDLE((^File_Impl)(desc.stdin.impl).fd)
}
@@ -506,6 +507,10 @@ _process_wait :: proc(process: Process, timeout: time.Duration) -> (process_stat
switch win32.WaitForSingleObject(handle, timeout_ms) {
case win32.WAIT_OBJECT_0:
if process._impl.null_handle != nil {
win32.CloseHandle(process._impl.null_handle)
}
exit_code: u32
if !win32.GetExitCodeProcess(handle, &exit_code) {
err =_get_platform_error()
@@ -532,6 +537,10 @@ _process_wait :: proc(process: Process, timeout: time.Duration) -> (process_stat
err = General_Error.Timeout
return
case:
if process._impl.null_handle != nil {
win32.CloseHandle(process._impl.null_handle)
}
err = _get_platform_error()
return
}