Repository navigation
Add Process.{exit!,fork} #7733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Process.{exit!,fork} #7733
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module Kernel | ||
| module_function | ||
| ## | ||
| # call-seq: | ||
| # Kernel#fork -> integer | ||
| def fork(&) | ||
| Process.fork(&) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| #include <limits.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
| #include <stdlib.h> | ||
|
|
||
| /* | ||
| * Refuse a value that would not survive the narrowing a port has to do with | ||
|
|
@@ -187,6 +188,64 @@ process_euid(mrb_state *mrb, mrb_value self) | |
| return mrb_int_value(mrb, euid); | ||
| } | ||
|
|
||
| static mrb_value | ||
| _process_fork_yield(mrb_state *mrb, mrb_value b) | ||
| { | ||
| return mrb_funcall(mrb, b, "call", 0, NULL); | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: | ||
| * Process.fork -> integer | ||
| * | ||
| * The fork(2) system call creates a new process that | ||
| * inherits from its parent. | ||
| */ | ||
| static mrb_value | ||
| process_fork(mrb_state *mrb, mrb_value self) | ||
| { | ||
| mrb_int pid; | ||
| mrb_value b; | ||
| mrb_get_args(mrb, "&", &b); | ||
| pid = mrb_hal_process_fork(mrb); | ||
| if (pid < 0) { | ||
| mrb_sys_fail(mrb, "fork"); | ||
| } else if (pid == 0) { | ||
| /* context: child process */ | ||
| mrb_gv_set(mrb, mrb_intern_lit(mrb, "$$"), mrb_int_value(mrb, mrb_hal_process_pid(mrb))); | ||
| if(mrb_block_given_p(mrb)) { | ||
| mrb_bool error; | ||
| mrb_value exc = mrb_protect(mrb, _process_fork_yield, b, &error); | ||
| if (error) { | ||
| mrb->exc = mrb_obj_ptr(exc); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| mrb_print_error(mrb); | ||
| mrb_hal_process_exit(mrb, EXIT_FAILURE); | ||
| } | ||
| mrb_hal_process_exit(mrb, EXIT_SUCCESS); | ||
| } | ||
| /* ditto: cruby returns nil from child */ | ||
| return mrb_nil_value(); | ||
| } else { | ||
| /* context: parent process */ | ||
| return mrb_int_value(mrb, pid); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: | ||
| * Process.exit -> nil | ||
| * | ||
| * The exit(2) system call terminates the current process. | ||
| */ | ||
| static mrb_value | ||
| process_exit(mrb_state *mrb, mrb_value self) | ||
| { | ||
| mrb_int i = 0; | ||
| mrb_get_args(mrb, "|i", &i); | ||
| mrb_hal_process_exit(mrb, i); | ||
| return mrb_nil_value(); | ||
| } | ||
|
|
||
| /* | ||
| * call-seq: | ||
| * Process.gid -> integer | ||
|
|
@@ -397,6 +456,8 @@ mrb_mruby_process_gem_init(mrb_state *mrb) | |
| mrb_define_module_function_id(mrb, process, MRB_SYM(ppid), process_ppid, MRB_ARGS_NONE()); | ||
| mrb_define_module_function_id(mrb, process, MRB_SYM(uid), process_uid, MRB_ARGS_NONE()); | ||
| mrb_define_module_function_id(mrb, process, MRB_SYM(euid), process_euid, MRB_ARGS_NONE()); | ||
| mrb_define_module_function_id(mrb, process, MRB_SYM(fork), process_fork, MRB_ARGS_NONE()); | ||
| mrb_define_module_function_id(mrb, process, mrb_intern_lit(mrb, "exit!"), process_exit, MRB_ARGS_OPT(1)); | ||
| mrb_define_module_function_id(mrb, process, MRB_SYM(gid), process_gid, MRB_ARGS_NONE()); | ||
| mrb_define_module_function_id(mrb, process, MRB_SYM(egid), process_egid, MRB_ARGS_NONE()); | ||
| mrb_define_module_function_id(mrb, process, MRB_SYM(kill), process_kill, MRB_ARGS_REQ(2)|MRB_ARGS_REST()); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.