Skip to content

Commit acc21d0

Browse files
authored
Merge pull request #724 from sogaiu/new-style-core-fn-decl-for-thread
Update thread.c with new style core function declarations.
2 parents db5df70 + ac98dbc commit acc21d0

File tree

1 file changed

+42
-55
lines changed

1 file changed

+42
-55
lines changed

src/core/thread.c

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,24 @@ JanetThread *janet_thread_current(void) {
596596
* Cfuns
597597
*/
598598

599-
static Janet cfun_thread_current(int32_t argc, Janet *argv) {
599+
JANET_CORE_FN(cfun_thread_current,
600+
"(thread/current)",
601+
"Get the current running thread.") {
600602
(void) argv;
601603
janet_fixarity(argc, 0);
602604
return janet_wrap_abstract(janet_thread_current());
603605
}
604606

605-
static Janet cfun_thread_new(int32_t argc, Janet *argv) {
607+
JANET_CORE_FN(cfun_thread_new,
608+
"(thread/new func &opt capacity flags)",
609+
"Start a new thread that will start immediately. "
610+
"If capacity is provided, that is how many messages can be stored in the thread's mailbox before blocking senders. "
611+
"The capacity must be between 1 and 65535 inclusive, and defaults to 10. "
612+
"Can optionally provide flags to the new thread - supported flags are:\n\n"
613+
"* :h - Start a heavyweight thread. This loads the core environment by default, so may use more memory initially. Messages may compress better, though.\n\n"
614+
"* :a - Allow sending over registered abstract types to the new thread\n\n"
615+
"* :c - Send over cfunction information to the new thread.\n\n"
616+
"Returns a handle to the new thread.") {
606617
janet_arity(argc, 1, 3);
607618
/* Just type checking */
608619
janet_getfunction(argv, 0);
@@ -645,7 +656,11 @@ static Janet cfun_thread_new(int32_t argc, Janet *argv) {
645656
return janet_wrap_abstract(thread);
646657
}
647658

648-
static Janet cfun_thread_send(int32_t argc, Janet *argv) {
659+
JANET_CORE_FN(cfun_thread_send,
660+
"(thread/send thread msgi &opt timeout)",
661+
"Send a message to the thread. By default, the timeout is 1 second, but an optional timeout "
662+
"in seconds can be provided. Use math/inf for no timeout. "
663+
"Will throw an error if there is a problem sending the message.") {
649664
janet_arity(argc, 2, 3);
650665
JanetThread *thread = janet_getthread(argv, 0);
651666
int status = janet_thread_send(thread, argv[1], janet_optnumber(argv, argc, 2, 1.0));
@@ -660,7 +675,12 @@ static Janet cfun_thread_send(int32_t argc, Janet *argv) {
660675
return argv[0];
661676
}
662677

663-
static Janet cfun_thread_receive(int32_t argc, Janet *argv) {
678+
JANET_CORE_FN(cfun_thread_receive,
679+
"(thread/receive &opt timeout)",
680+
"Get a message sent to this thread. If timeout (in seconds) is provided, an error "
681+
"will be thrown after the timeout has elapsed but "
682+
"no messages are received. The default timeout is 1 second, and math/inf cam be passed to "
683+
"turn off the timeout.") {
664684
janet_arity(argc, 0, 1);
665685
double wait = janet_optnumber(argv, argc, 0, 1.0);
666686
Janet out;
@@ -676,14 +696,20 @@ static Janet cfun_thread_receive(int32_t argc, Janet *argv) {
676696
return out;
677697
}
678698

679-
static Janet cfun_thread_close(int32_t argc, Janet *argv) {
699+
JANET_CORE_FN(cfun_thread_close,
700+
"(thread/close thread)",
701+
"Close a thread, unblocking it and ending communication with it. Note that closing "
702+
"a thread is idempotent and does not cancel the thread's operation. Returns nil.") {
680703
janet_fixarity(argc, 1);
681704
JanetThread *thread = janet_getthread(argv, 0);
682705
janet_close_thread(thread);
683706
return janet_wrap_nil();
684707
}
685708

686-
static Janet cfun_thread_exit(int32_t argc, Janet *argv) {
709+
JANET_CORE_FN(cfun_thread_exit,
710+
"(thread/exit &opt code)",
711+
"Exit from the current thread. If no more threads are running, ends the process, but otherwise does "
712+
"not end the current process.") {
687713
(void) argv;
688714
janet_arity(argc, 0, 1);
689715
#if defined(JANET_WINDOWS)
@@ -712,57 +738,18 @@ static Janet janet_thread_next(void *p, Janet key) {
712738
return janet_nextmethod(janet_thread_methods, key);
713739
}
714740

715-
static const JanetReg threadlib_cfuns[] = {
716-
{
717-
"thread/current", cfun_thread_current,
718-
JDOC("(thread/current)\n\n"
719-
"Get the current running thread.")
720-
},
721-
{
722-
"thread/new", cfun_thread_new,
723-
JDOC("(thread/new func &opt capacity flags)\n\n"
724-
"Start a new thread that will start immediately. "
725-
"If capacity is provided, that is how many messages can be stored in the thread's mailbox before blocking senders. "
726-
"The capacity must be between 1 and 65535 inclusive, and defaults to 10. "
727-
"Can optionally provide flags to the new thread - supported flags are:\n\n"
728-
"* :h - Start a heavyweight thread. This loads the core environment by default, so may use more memory initially. Messages may compress better, though.\n\n"
729-
"* :a - Allow sending over registered abstract types to the new thread\n\n"
730-
"* :c - Send over cfunction information to the new thread.\n\n"
731-
"Returns a handle to the new thread.")
732-
},
733-
{
734-
"thread/send", cfun_thread_send,
735-
JDOC("(thread/send thread msgi &opt timeout)\n\n"
736-
"Send a message to the thread. By default, the timeout is 1 second, but an optional timeout "
737-
"in seconds can be provided. Use math/inf for no timeout. "
738-
"Will throw an error if there is a problem sending the message.")
739-
},
740-
{
741-
"thread/receive", cfun_thread_receive,
742-
JDOC("(thread/receive &opt timeout)\n\n"
743-
"Get a message sent to this thread. If timeout (in seconds) is provided, an error "
744-
"will be thrown after the timeout has elapsed but "
745-
"no messages are received. The default timeout is 1 second, and math/inf cam be passed to "
746-
"turn off the timeout.")
747-
},
748-
{
749-
"thread/close", cfun_thread_close,
750-
JDOC("(thread/close thread)\n\n"
751-
"Close a thread, unblocking it and ending communication with it. Note that closing "
752-
"a thread is idempotent and does not cancel the thread's operation. Returns nil.")
753-
},
754-
{
755-
"thread/exit", cfun_thread_exit,
756-
JDOC("(thread/exit &opt code)\n\n"
757-
"Exit from the current thread. If no more threads are running, ends the process, but otherwise does "
758-
"not end the current process.")
759-
},
760-
{NULL, NULL, NULL}
761-
};
762-
763741
/* Module entry point */
764742
void janet_lib_thread(JanetTable *env) {
765-
janet_core_cfuns(env, NULL, threadlib_cfuns);
743+
JanetRegExt threadlib_cfuns[] = {
744+
JANET_CORE_REG("thread/current", cfun_thread_current),
745+
JANET_CORE_REG("thread/new", cfun_thread_new),
746+
JANET_CORE_REG("thread/send", cfun_thread_send),
747+
JANET_CORE_REG("thread/receive", cfun_thread_receive),
748+
JANET_CORE_REG("thread/close", cfun_thread_close),
749+
JANET_CORE_REG("thread/exit", cfun_thread_exit),
750+
JANET_REG_END
751+
};
752+
janet_core_cfuns_ext(env, NULL, threadlib_cfuns);
766753
janet_register_abstract_type(&janet_thread_type);
767754
}
768755

0 commit comments

Comments
 (0)