1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +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 (defn make-worker
[name interval] [name interval]
(-> (thread/new) (-> (thread/new worker-main)
(:send worker-main)
(:send name) (:send name)
(:send interval))) (:send interval)))
@ -39,8 +38,7 @@
(if (< depth 5) (if (< depth 5)
(do (do
(defn subtree [] (defn subtree []
(-> (thread/new) (-> (thread/new worker-tree)
(:send worker-tree)
(:send (string name "/" (choose "bob" "marley" "harry" "suki" "anna" "yu"))) (:send (string name "/" (choose "bob" "marley" "harry" "suki" "anna" "yu")))
(:send (inc depth)))) (:send (inc depth))))
(let [l (subtree) (let [l (subtree)
@ -51,7 +49,7 @@
(do (do
(:send parent [name])))) (:send parent [name]))))
(-> (thread/new) (:send worker-tree) (:send "adam") (:send 0)) (-> (thread/new worker-tree) (:send "adam") (:send 0))
(def lines (thread/receive)) (def lines (thread/receive))
(map print lines) (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) { static Janet cfun_thread_new(int32_t argc, Janet *argv) {
janet_arity(argc, 0, 1); janet_arity(argc, 1, 2);
int32_t cap = janet_optinteger(argv, argc, 0, 10); /* Just type checking */
janet_getfunction(argv, 0);
int32_t cap = janet_optinteger(argv, argc, 1, 10);
if (cap < 1 || cap > UINT16_MAX) { if (cap < 1 || cap > UINT16_MAX) {
janet_panicf("bad slot #1, expected integer in range [1, 65535], got %d", cap); 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_mailbox_ref(janet_vm_mailbox, -1); /* ->parent reference */
janet_panic("could not start thread"); 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); return janet_wrap_abstract(thread);
} }
@ -606,9 +612,9 @@ static const JanetReg threadlib_cfuns[] = {
}, },
{ {
"thread/new", cfun_thread_new, "thread/new", cfun_thread_new,
JDOC("(thread/new &opt capacity)\n\n" JDOC("(thread/new func &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 " "Start a new thread that will start immediately. "
"via thread/send. If capacity is provided, that is how many messages can be stored in the thread's mailbox before blocking senders. " "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. " "The capacity must be between 1 and 65535 inclusive, and defaults to 10. "
"Returns a handle to the new thread.") "Returns a handle to the new thread.")
}, },