1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +00:00

Pass thread body explicitly in thread/new.

Doing it via thread/send make sense, but is a bit
strange. Passing the body explicitly will make more
sense to API users.
This commit is contained in:
Calvin Rose 2019-12-18 15:07:46 -05:00
parent b9f0f14e31
commit 6a39c4b91d
2 changed files with 14 additions and 10 deletions

View File

@ -10,8 +10,7 @@
(defn make-worker
[name interval]
(-> (thread/new)
(:send worker-main)
(-> (thread/new worker-main)
(:send name)
(:send interval)))
@ -39,8 +38,7 @@
(if (< depth 5)
(do
(defn subtree []
(-> (thread/new)
(:send worker-tree)
(-> (thread/new worker-tree)
(:send (string name "/" (choose "bob" "marley" "harry" "suki" "anna" "yu")))
(:send (inc depth))))
(let [l (subtree)
@ -51,7 +49,7 @@
(do
(:send parent [name]))))
(-> (thread/new) (:send worker-tree) (:send "adam") (:send 0))
(-> (thread/new worker-tree) (:send "adam") (:send 0))
(def lines (thread/receive))
(map print lines)

View File

@ -530,8 +530,10 @@ static Janet cfun_thread_current(int32_t argc, Janet *argv) {
}
static Janet cfun_thread_new(int32_t argc, Janet *argv) {
janet_arity(argc, 0, 1);
int32_t cap = janet_optinteger(argv, argc, 0, 10);
janet_arity(argc, 1, 2);
/* Just type checking */
janet_getfunction(argv, 0);
int32_t cap = janet_optinteger(argv, argc, 1, 10);
if (cap < 1 || cap > UINT16_MAX) {
janet_panicf("bad slot #1, expected integer in range [1, 65535], got %d", cap);
}
@ -547,6 +549,10 @@ static Janet cfun_thread_new(int32_t argc, Janet *argv) {
janet_mailbox_ref(janet_vm_mailbox, -1); /* ->parent reference */
janet_panic("could not start thread");
}
/* If thread started, send the worker function. */
janet_thread_send(thread, argv[0], -1.0);
return janet_wrap_abstract(thread);
}
@ -606,9 +612,9 @@ static const JanetReg threadlib_cfuns[] = {
},
{
"thread/new", cfun_thread_new,
JDOC("(thread/new &opt capacity)\n\n"
"Start a new thread. The thread will wait for a message containing the function used to start the thread, which should be passed to the thread "
"via thread/send. If capacity is provided, that is how many messages can be stored in the thread's mailbox before blocking senders. "
JDOC("(thread/new func &opt capacity)\n\n"
"Start a new thread that will start immediately. "
"If capacity is provided, that is how many messages can be stored in the thread's mailbox before blocking senders. "
"The capacity must be between 1 and 65535 inclusive, and defaults to 10. "
"Returns a handle to the new thread.")
},