don't need to hang on to the null handle

This commit is contained in:
laytan
2025-05-06 19:42:52 +02:00
parent bf5206968a
commit 9b218a2922
5 changed files with 12 additions and 27 deletions
-2
View File
@@ -266,8 +266,6 @@ specific process, even after it has died.
Process :: struct { Process :: struct {
pid: int, pid: int,
handle: uintptr, handle: uintptr,
// Implementation specific state/data.
_impl: _Process,
} }
Process_Open_Flags :: bit_set[Process_Open_Flag] Process_Open_Flags :: bit_set[Process_Open_Flag]
-3
View File
@@ -362,9 +362,6 @@ _current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime
return _process_info_by_pid(get_pid(), selection, allocator) return _process_info_by_pid(get_pid(), selection, allocator)
} }
@(private="package")
_Process :: struct {}
@(private="package") @(private="package")
_process_open :: proc(pid: int, _: Process_Open_Flags) -> (process: Process, err: Error) { _process_open :: proc(pid: int, _: Process_Open_Flags) -> (process: Process, err: Error) {
process.pid = pid process.pid = pid
-2
View File
@@ -46,8 +46,6 @@ _current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime
return _process_info_by_pid(_get_pid(), selection, allocator) return _process_info_by_pid(_get_pid(), selection, allocator)
} }
_Process :: struct {}
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) { _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
if len(desc.command) == 0 { if len(desc.command) == 0 {
err = .Invalid_Path err = .Invalid_Path
-2
View File
@@ -44,8 +44,6 @@ _current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime
return return
} }
_Process :: struct {}
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) { _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
err = .Unsupported err = .Unsupported
return return
+12 -18
View File
@@ -417,11 +417,6 @@ _process_open :: proc(pid: int, flags: Process_Open_Flags) -> (process: Process,
return return
} }
@(private="package")
_Process :: struct {
null_handle: win32.HANDLE,
}
@(private="package") @(private="package")
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) { _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
TEMP_ALLOCATOR_GUARD() TEMP_ALLOCATOR_GUARD()
@@ -438,8 +433,9 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
stdout_handle: win32.HANDLE stdout_handle: win32.HANDLE
stdin_handle: win32.HANDLE stdin_handle: win32.HANDLE
null_handle: win32.HANDLE
if desc.stdout == nil || desc.stderr == nil || desc.stdin == nil { if desc.stdout == nil || desc.stderr == nil || desc.stdin == nil {
process._impl.null_handle = win32.CreateFileW( null_handle = win32.CreateFileW(
win32.L("NUL"), win32.L("NUL"),
win32.GENERIC_READ|win32.GENERIC_WRITE, win32.GENERIC_READ|win32.GENERIC_WRITE,
win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE, win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE,
@@ -451,23 +447,29 @@ _process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
win32.FILE_ATTRIBUTE_NORMAL, win32.FILE_ATTRIBUTE_NORMAL,
nil, nil,
) )
assert(process._impl.null_handle != nil) // Opening NUL should always succeed.
assert(null_handle != nil)
}
// NOTE(laytan): I believe it is fine to close this handle right after CreateProcess,
// and we don't have to hold onto this until the process exits.
defer if null_handle != nil {
win32.CloseHandle(null_handle)
} }
if desc.stdout == nil { if desc.stdout == nil {
stdout_handle = process._impl.null_handle stdout_handle = null_handle
} else { } else {
stdout_handle = win32.HANDLE((^File_Impl)(desc.stdout.impl).fd) stdout_handle = win32.HANDLE((^File_Impl)(desc.stdout.impl).fd)
} }
if desc.stderr == nil { if desc.stderr == nil {
stderr_handle = process._impl.null_handle stderr_handle = null_handle
} else { } else {
stderr_handle = win32.HANDLE((^File_Impl)(desc.stderr.impl).fd) stderr_handle = win32.HANDLE((^File_Impl)(desc.stderr.impl).fd)
} }
if desc.stdin == nil { if desc.stdin == nil {
stdin_handle = process._impl.null_handle stdin_handle = null_handle
} else { } else {
stdin_handle = win32.HANDLE((^File_Impl)(desc.stdin.impl).fd) stdin_handle = win32.HANDLE((^File_Impl)(desc.stdin.impl).fd)
} }
@@ -507,10 +509,6 @@ _process_wait :: proc(process: Process, timeout: time.Duration) -> (process_stat
switch win32.WaitForSingleObject(handle, timeout_ms) { switch win32.WaitForSingleObject(handle, timeout_ms) {
case win32.WAIT_OBJECT_0: case win32.WAIT_OBJECT_0:
if process._impl.null_handle != nil {
win32.CloseHandle(process._impl.null_handle)
}
exit_code: u32 exit_code: u32
if !win32.GetExitCodeProcess(handle, &exit_code) { if !win32.GetExitCodeProcess(handle, &exit_code) {
err =_get_platform_error() err =_get_platform_error()
@@ -537,10 +535,6 @@ _process_wait :: proc(process: Process, timeout: time.Duration) -> (process_stat
err = General_Error.Timeout err = General_Error.Timeout
return return
case: case:
if process._impl.null_handle != nil {
win32.CloseHandle(process._impl.null_handle)
}
err = _get_platform_error() err = _get_platform_error()
return return
} }