Skip to content

Commit 49dcc81

Browse files
committed
Update os/shell to be non-blocking as well.
1 parent fa61c70 commit 49dcc81

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

src/boot/boot.janet

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3146,10 +3146,17 @@
31463146

31473147
(compwhen (dyn 'ev/go)
31483148
(defn net/close "Alias for ev/close." [stream] (ev/close stream))
3149+
3150+
(defn ev/call
3151+
"Call a function asynchronously. Returns a fiber that is scheduled to "
3152+
"run the function."
3153+
[f & args]
3154+
(ev/go (fiber/new (fn [&] (f ;args)) :tp)))
3155+
31493156
(defmacro ev/spawn
31503157
"Run some code in a new fiber. This is shorthand for (ev/call (fn [] ;body))."
31513158
[& body]
3152-
~(,ev/call (fn [] ,;body)))
3159+
~(,ev/go (fiber/new (fn _spawn [&] ,;body) :tp)))
31533160

31543161
(defmacro ev/with-deadline
31553162
`Run a body of code with a deadline, such that if the code does not complete before

src/core/ev.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,12 +1435,16 @@ void janet_ev_default_threaded_callback(JanetEVGenericMessage return_value) {
14351435
case JANET_EV_TCTAG_ERR_KEYWORD:
14361436
janet_cancel(return_value.fiber, janet_ckeywordv((const char *) return_value.argp));
14371437
break;
1438+
case JANET_EV_TCTAG_BOOLEAN:
1439+
janet_schedule(return_value.fiber, janet_wrap_boolean(return_value.argi));
1440+
break;
14381441
}
14391442
janet_gcunroot(janet_wrap_fiber(return_value.fiber));
14401443
}
14411444

14421445

14431446
/* Convenience method for common case */
1447+
JANET_NO_RETURN
14441448
void janet_ev_threaded_await(JanetThreadedSubroutine fp, int tag, int argi, void *argp) {
14451449
JanetEVGenericMessage arguments;
14461450
arguments.tag = tag;
@@ -2016,11 +2020,6 @@ int janet_make_pipe(JanetHandle handles[2], int mode) {
20162020
#endif
20172021
}
20182022

2019-
static void janet_ev_go(JanetFiber *fiber, Janet value, JanetChannel *supervisor_channel) {
2020-
fiber->supervisor_channel = supervisor_channel;
2021-
janet_schedule(fiber, value);
2022-
}
2023-
20242023
/* C functions */
20252024

20262025
static Janet cfun_ev_go(int32_t argc, Janet *argv) {
@@ -2029,21 +2028,11 @@ static Janet cfun_ev_go(int32_t argc, Janet *argv) {
20292028
Janet value = argc == 2 ? argv[1] : janet_wrap_nil();
20302029
JanetChannel *supervisor_channel = janet_optabstract(argv, argc, 2, &ChannelAT,
20312030
janet_vm_root_fiber->supervisor_channel);
2032-
janet_ev_go(fiber, value, supervisor_channel);
2031+
fiber->supervisor_channel = supervisor_channel;
2032+
janet_schedule(fiber, value);
20332033
return argv[0];
20342034
}
20352035

2036-
static Janet cfun_ev_call(int32_t argc, Janet *argv) {
2037-
janet_arity(argc, 1, -1);
2038-
JanetFunction *fn = janet_getfunction(argv, 0);
2039-
JanetFiber *fiber = janet_fiber(fn, 64, argc - 1, argv + 1);
2040-
if (NULL == fiber) janet_panicf("invalid arity to function %v", argv[0]);
2041-
fiber->env = janet_table(0);
2042-
fiber->env->proto = janet_current_fiber()->env;
2043-
janet_ev_go(fiber, janet_wrap_nil(), (JanetChannel *)(janet_vm_root_fiber->supervisor_channel));
2044-
return janet_wrap_fiber(fiber);
2045-
}
2046-
20472036
static Janet cfun_ev_give_supervisor(int32_t argc, Janet *argv) {
20482037
janet_arity(argc, 1, -1);
20492038
JanetChannel *chan = janet_vm_root_fiber->supervisor_channel;
@@ -2148,12 +2137,6 @@ Janet janet_cfun_stream_write(int32_t argc, Janet *argv) {
21482137
}
21492138

21502139
static const JanetReg ev_cfuns[] = {
2151-
{
2152-
"ev/call", cfun_ev_call,
2153-
JDOC("(ev/call fn & args)\n\n"
2154-
"Call a function asynchronously. Returns a fiber that is scheduled to "
2155-
"run the function.")
2156-
},
21572140
{
21582141
"ev/go", cfun_ev_go,
21592142
JDOC("(ev/go fiber &opt value supervisor)\n\n"

src/core/os.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,15 +973,34 @@ static Janet os_spawn(int32_t argc, Janet *argv) {
973973
return os_execute_impl(argc, argv, 1);
974974
}
975975

976+
#ifdef JANET_EV
977+
/* Runs in a separate thread */
978+
static JanetEVGenericMessage os_shell_subr(JanetEVGenericMessage args) {
979+
int stat = system((const char *) args.argp);
980+
free(args.argp);
981+
if (args.argi) {
982+
args.tag = JANET_EV_TCTAG_INTEGER;
983+
} else {
984+
args.tag = JANET_EV_TCTAG_BOOLEAN;
985+
}
986+
args.argi = stat;
987+
return args;
988+
}
989+
#endif
990+
976991
static Janet os_shell(int32_t argc, Janet *argv) {
977992
janet_arity(argc, 0, 1);
978993
const char *cmd = argc
979994
? janet_getcstring(argv, 0)
980995
: NULL;
996+
#ifdef JANET_EV
997+
janet_ev_threaded_await(os_shell_subr, 0, argc, cmd ? strdup(cmd) : NULL);
998+
#else
981999
int stat = system(cmd);
9821000
return argc
9831001
? janet_wrap_integer(stat)
9841002
: janet_wrap_boolean(stat);
1003+
#endif
9851004
}
9861005

9871006
#endif /* JANET_NO_PROCESSES */

src/include/janet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ typedef struct {
13241324
#define JANET_EV_TCTAG_ERR_STRING 5 /* cancel with janet_cstringv((const char *) argp) */
13251325
#define JANET_EV_TCTAG_ERR_STRINGF 6 /* cancel with janet_cstringv((const char *) argp), then call free on argp. */
13261326
#define JANET_EV_TCTAG_ERR_KEYWORD 7 /* cancel with janet_ckeywordv((const char *) argp) */
1327+
#define JANET_EV_TCTAG_BOOLEAN 8 /* resume with janet_wrap_boolean(argi) */
13271328

13281329
/* Function pointer that is run in the thread pool */
13291330
typedef JanetEVGenericMessage(*JanetThreadedSubroutine)(JanetEVGenericMessage arguments);
@@ -1333,7 +1334,7 @@ typedef void (*JanetThreadedCallback)(JanetEVGenericMessage return_value);
13331334

13341335
/* API calls for quickly offloading some work in C to a new thread or thread pool. */
13351336
JANET_API void janet_ev_threaded_call(JanetThreadedSubroutine fp, JanetEVGenericMessage arguments, JanetThreadedCallback cb);
1336-
JANET_API void janet_ev_threaded_await(JanetThreadedSubroutine fp, int tag, int argi, void *argp);
1337+
JANET_NO_RETURN JANET_API void janet_ev_threaded_await(JanetThreadedSubroutine fp, int tag, int argi, void *argp);
13371338

13381339
/* Callback used by janet_ev_threaded_await */
13391340
JANET_API void janet_ev_default_threaded_callback(JanetEVGenericMessage return_value);

test/suite0009.janet

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@
161161
(ev/sleep 0)
162162
(ev/cancel fiber "boop")
163163

164-
(assert-error "bad arity to ev/call" (ev/call inc 1 2 3))
165-
166164
(assert (os/execute [janet "-e" `(+ 1 2 3)`] :xp) "os/execute self")
167165

168166
(end-suite)

0 commit comments

Comments
 (0)