Replies: 7 comments 5 replies
-
Looking at src/core/fiber.c, I don't see anything about threads, and so it's looking to me like fibers are a way to call a function but then temporarily leave it (when it yields) and then get back to it at another time (via I see from the fiber api docs that we have fiber/getenv and fiber/setenv. The dynamic bindings chapter of the docs mention contexts... In Janet, is a context another name for an environment? |
Beta Was this translation helpful? Give feedback.
-
My current take is that there isn't an official definition of Two of the places I've seen the term used include what you pointed out in the dynamic bindings chapter of the docs and in the function For the former location, there are at least these two paragraphs:
The first paragraph suggests perhaps somewhere in the source (or may be someone's project?) one might be able to find an instance of "reset the context if an error is raised". May be that would give more clues. The second paragraph suggests to me that an implicit context can be stored in a table. Perhaps not much of a clue. For Lines 2430 to 2449 in 848d4a1 The single argument to this function is a table IIUC. So perhaps it is either this table of something within it. |
Beta Was this translation helpful? Give feedback.
-
As I understand it, fibers are Janet's answer to Lua's coroutines. They allow for cooperative multitasking, as well as providing a flexible abstraction for things like catching errors, or generating infinite sequences. |
Beta Was this translation helpful? Give feedback.
-
Thanks, @sogaiu , thanks @yumaikas . I think I see that fibers are single-threaded, cooperative multi-tasking, where the fiber has some of its very own state (environment) that it keeps track of. @yumaikas , thanks, though I'm unfamiliar with Lua's coroutines. |
Beta Was this translation helpful? Give feedback.
-
The way I see fibers are the extraction of certain parts of a function from the function. Specifically:
Being able to leverage these attributes without functions leads to:
So it is an abstraction that extracts useful tools from functions without having to define a function. Iiuc the above can be defined with functions instead, but functions tend to be more opaque, whereas fibers are easier to inspect and simply does less. |
Beta Was this translation helpful? Give feedback.
-
The simplest mental model which works for me is this:
It is definitely not everything, but this works for me. |
Beta Was this translation helpful? Give feedback.
-
Thanks, @saikyun , thanks @pepe . As part of this, I think I was conflating:
I see that fibers give you a way to get the former (specifically, single-threaded, cooperatively scheduled concurrency), but not the latter. |
Beta Was this translation helpful? Give feedback.
-
Looking at the docs for fiber, it appears to me that a fiber is quite like a function, but you never call it --- you instead (possibly repeatedly) call
resume
on it to let it execute. Is that the case?It looks like you wrap a zero-arg function (possibly containing zero or more
yield
s) infiber/new
. You can check its status with(fiber/status fbr)
. Does a fiber (function) run only once?It looks like fibers have parent/child relationships with other fibers. Is that correct?
What is the purpose of fibers? It doesn't appear to me that they run in a different thread than the current thread. Do they have anything to do with multithreading?
The docs on dynamic binding say that "each fiber contains an environment table". Is this environment table based on (does it inherit from) the parent fiber?
Does my Janet program run in one "top-level" fiber?
Beta Was this translation helpful? Give feedback.
All reactions