diff --git a/rt/src/process.rs b/rt/src/process.rs index 26bd90cfb..000d0e85d 100644 --- a/rt/src/process.rs +++ b/rt/src/process.rs @@ -746,19 +746,6 @@ impl ProcessPointer { self.as_ptr() as usize } - // TODO: remove - pub(crate) fn blocking(mut self, function: impl FnOnce() -> R) -> R { - // Safety: threads are stored in processes before running them. - let thread = unsafe { self.thread() }; - - thread.start_blocking(); - - let res = function(); - - thread.stop_blocking(self); - res - } - pub(crate) fn start_blocking(mut self) { // Safety: threads are stored in processes before running them. unsafe { self.thread() }.start_blocking(); diff --git a/rt/src/runtime/sys.rs b/rt/src/runtime/sys.rs index 2e4255cbb..8b4a6d8d5 100644 --- a/rt/src/runtime/sys.rs +++ b/rt/src/runtime/sys.rs @@ -1,5 +1,4 @@ use crate::mem::{ByteArray, String as InkoString}; -use crate::process::ProcessPointer; use crate::result::Result as InkoResult; use crate::runtime::helpers::read_into; use std::io::Write; @@ -16,7 +15,6 @@ fn stdio_for(value: i64) -> Stdio { #[no_mangle] pub(crate) unsafe extern "system" fn inko_child_process_spawn( - process: ProcessPointer, program: *const InkoString, args: *const *const InkoString, args_length: i64, @@ -52,19 +50,15 @@ pub(crate) unsafe extern "system" fn inko_child_process_spawn( cmd.current_dir(directory); } - process - .blocking(|| cmd.spawn()) - .map(InkoResult::ok_boxed) - .unwrap_or_else(InkoResult::io_error) + cmd.spawn().map(InkoResult::ok_boxed).unwrap_or_else(InkoResult::io_error) } #[no_mangle] pub(crate) unsafe extern "system" fn inko_child_process_wait( - process: ProcessPointer, child: *mut Child, ) -> InkoResult { - process - .blocking(|| (*child).wait()) + (*child) + .wait() .map(|status| status.code().unwrap_or(0) as i64) .map(|status| InkoResult::ok(status as _)) .unwrap_or_else(InkoResult::io_error) @@ -88,7 +82,6 @@ pub(crate) unsafe extern "system" fn inko_child_process_try_wait( #[no_mangle] pub(crate) unsafe extern "system" fn inko_child_process_stdout_read( - process: ProcessPointer, child: *mut Child, buffer: *mut ByteArray, size: i64, @@ -99,7 +92,7 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdout_read( child .stdout .as_mut() - .map(|stream| process.blocking(|| read_into(stream, buff, size))) + .map(|stream| read_into(stream, buff, size)) .unwrap_or(Ok(0)) .map(|size| InkoResult::ok(size as _)) .unwrap_or_else(InkoResult::io_error) @@ -107,7 +100,6 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdout_read( #[no_mangle] pub(crate) unsafe extern "system" fn inko_child_process_stderr_read( - process: ProcessPointer, child: *mut Child, buffer: *mut ByteArray, size: i64, @@ -118,7 +110,7 @@ pub(crate) unsafe extern "system" fn inko_child_process_stderr_read( child .stderr .as_mut() - .map(|stream| process.blocking(|| read_into(stream, buff, size))) + .map(|stream| read_into(stream, buff, size)) .unwrap_or(Ok(0)) .map(|size| InkoResult::ok(size as _)) .unwrap_or_else(InkoResult::io_error) @@ -126,7 +118,6 @@ pub(crate) unsafe extern "system" fn inko_child_process_stderr_read( #[no_mangle] pub(crate) unsafe extern "system" fn inko_child_process_stdin_write( - process: ProcessPointer, child: *mut Child, data: *mut u8, size: i64, @@ -137,7 +128,7 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdin_write( child .stdin .as_mut() - .map(|stream| process.blocking(|| stream.write(slice))) + .map(|stream| stream.write(slice)) .unwrap_or(Ok(0)) .map(|size| InkoResult::ok(size as _)) .unwrap_or_else(InkoResult::io_error) @@ -145,7 +136,6 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdin_write( #[no_mangle] pub(crate) unsafe extern "system" fn inko_child_process_stdin_flush( - process: ProcessPointer, child: *mut Child, ) -> InkoResult { let child = &mut *child; @@ -153,7 +143,7 @@ pub(crate) unsafe extern "system" fn inko_child_process_stdin_flush( child .stdin .as_mut() - .map(|stream| process.blocking(|| stream.flush())) + .map(|stream| stream.flush()) .unwrap_or(Ok(())) .map(|_| InkoResult::none()) .unwrap_or_else(InkoResult::io_error) diff --git a/rt/src/rustls_platform_verifier/verification/apple.rs b/rt/src/rustls_platform_verifier/verification/apple.rs index 933c0f590..06d8c8f28 100644 --- a/rt/src/rustls_platform_verifier/verification/apple.rs +++ b/rt/src/rustls_platform_verifier/verification/apple.rs @@ -123,11 +123,19 @@ impl Verifier { // Safety: well, technically none, but due to the way the runtime uses // the verifier this should never misbehave. let process = unsafe { ProcessPointer::new(CURRENT_PROCESS.get()) }; - let trust_error = - match process.blocking(|| trust_evaluation.evaluate_with_error()) { - Ok(()) => return Ok(()), - Err(e) => e, - }; + + process.start_blocking(); + + let trust_error = match trust_evaluation.evaluate_with_error() { + Ok(()) => { + process.stop_blocking(); + return Ok(()); + } + Err(e) => { + process.stop_blocking(); + e + } + }; let err_code = trust_error.code(); diff --git a/std/src/std/sys.inko b/std/src/std/sys.inko index 2ec8fce0e..69a392659 100644 --- a/std/src/std/sys.inko +++ b/std/src/std/sys.inko @@ -2,7 +2,7 @@ import std.drop (Drop) import std.fs.path (Path) import std.int (ToInt) -import std.io (Error, Read, Write, WriteInternal) +import std.io (Error, Read, Write, WriteInternal, start_blocking, stop_blocking) import std.string (ToString) class extern AnyResult { @@ -16,7 +16,6 @@ class extern IntResult { } fn extern inko_child_process_spawn( - process: Pointer[UInt8], program: String, args: Pointer[String], args_size: Int, @@ -37,26 +36,20 @@ fn extern inko_child_process_stderr_close(child: Pointer[UInt8]) fn extern inko_child_process_stdin_close(child: Pointer[UInt8]) fn extern inko_child_process_stderr_read( - process: Pointer[UInt8], child: Pointer[UInt8], buffer: mut ByteArray, size: Int, ) -> IntResult fn extern inko_child_process_stdout_read( - process: Pointer[UInt8], child: Pointer[UInt8], buffer: mut ByteArray, size: Int, ) -> IntResult -fn extern inko_child_process_stdin_flush( - process: Pointer[UInt8], - child: Pointer[UInt8], -) -> IntResult +fn extern inko_child_process_stdin_flush(child: Pointer[UInt8]) -> IntResult fn extern inko_child_process_stdin_write( - process: Pointer[UInt8], child: Pointer[UInt8], data: Pointer[UInt8], size: Int, @@ -64,10 +57,7 @@ fn extern inko_child_process_stdin_write( fn extern inko_child_process_try_wait(child: Pointer[UInt8]) -> IntResult -fn extern inko_child_process_wait( - process: Pointer[UInt8], - child: Pointer[UInt8], -) -> IntResult +fn extern inko_child_process_wait(child: Pointer[UInt8]) -> IntResult fn extern inko_exit(status: Int) -> Never @@ -366,20 +356,23 @@ class pub Command { vars.push(entry.value) }) - match - inko_child_process_spawn( - _INKO.process, - @program.to_string, - @arguments.to_pointer, - @arguments.size, - vars.to_pointer, - vars.size, - @stdin.to_int, - @stdout.to_int, - @stderr.to_int, - @directory.as_ref.or(ref ''), - ) - { + start_blocking + + let res = inko_child_process_spawn( + @program.to_string, + @arguments.to_pointer, + @arguments.size, + vars.to_pointer, + vars.size, + @stdin.to_int, + @stdout.to_int, + @stderr.to_int, + @directory.as_ref.or(ref ''), + ) + + stop_blocking + + match res { case { @tag = 0, @value = v } -> { Result.Ok(ChildProcess(v as Pointer[UInt8])) } @@ -438,9 +431,13 @@ impl Drop for Stdin { impl WriteInternal for Stdin { fn mut write_internal(data: Pointer[UInt8], size: Int) -> Result[Int, Error] { - match - inko_child_process_stdin_write(_INKO.process, @process.raw, data, size) - { + start_blocking + + let res = inko_child_process_stdin_write(@process.raw, data, size) + + stop_blocking + + match res { case { @tag = 0, @value = n } -> Result.Ok(n) case { @value = e } -> Result.Error(Error.from_os_error(e)) } @@ -457,7 +454,13 @@ impl Write for Stdin { } fn pub mut flush -> Result[Nil, Error] { - match inko_child_process_stdin_flush(_INKO.process, @process.raw) { + start_blocking + + let res = inko_child_process_stdin_flush(@process.raw) + + stop_blocking + + match res { case { @tag = 1, @value = _ } -> Result.Ok(nil) case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e)) } @@ -482,9 +485,13 @@ impl Drop for Stdout { impl Read for Stdout { fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] { - match - inko_child_process_stdout_read(_INKO.process, @process.raw, into, size) - { + start_blocking + + let res = inko_child_process_stdout_read(@process.raw, into, size) + + stop_blocking + + match res { case { @tag = 0, @value = v } -> Result.Ok(v) case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e)) } @@ -509,9 +516,13 @@ impl Drop for Stderr { impl Read for Stderr { fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] { - match - inko_child_process_stderr_read(_INKO.process, @process.raw, into, size) - { + start_blocking + + let res = inko_child_process_stderr_read(@process.raw, into, size) + + stop_blocking + + match res { case { @tag = 0, @value = v } -> Result.Ok(v) case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e)) } @@ -542,7 +553,13 @@ class pub ChildProcess { # # The STDIN stream is closed before waiting. fn pub wait -> Result[ExitStatus, Error] { - match inko_child_process_wait(_INKO.process, @raw) { + start_blocking + + let res = inko_child_process_wait(@raw) + + stop_blocking + + match res { case { @tag = 0, @value = v } -> Result.Ok(ExitStatus.new(v)) case { @tag = _, @value = e } -> Result.Error(Error.from_os_error(e)) }