Subscribe
Garbage collected coroutines?
I've used the following method in other programming languages (Lua, Janet) and I'm wondering whether I can do something similar in PicoLisp: I've got a coroutine that generates an infinite sequence of data. I've got another coroutine that takes data from the previous coroutine, processes it and generates a sequence (also infinite) of modified data. The original coroutine is kept in its parent coroutine's local variable. Another coroutine takes this modified data, modifies it further and generates another sequence. This happens on several levels, around 5 - 10 coroutines chained like this, each of them generating an infinite sequence of data based on its "child" coroutine (kept in local variable) data and passing the modified sequence to its "parent" using "yield". Finally the top level parent does something with this generated data and, after some time stops and forgets the reference to it direct "child" coroutine. At this time, the direct child coroutine is automatically garbage collected. This releases the reference to next level "child" coroutine, this is also garbage collected etc., etc. until the original coroutine that was generating the first sequence. The upshot of this method is that I don't have to manually kill any of the coroutines, they are all automatically garbage collected when I no longer use them. If I understand it correctly, creating a coroutine in PicoLisp creates a global symbol that keeps the reference to this coroutine so I have to explicitly remove the coroutine when I no longer need it - it can never get automatically garbage collected because it's linked to a global symbol. Is there a similar mechanism in PicoLisp? Keeping the coroutine reference in the local scope so that it's automatically destroyed when it goes out of scope? P.S: It seems that I have problem receiving any email from this mailing list so I'll probably have to post follow ups to this topic in the IRC channel after I read the replies in the mailing list web archive... -- -- František Fuka
Re: Garbage collected coroutines?
Hi František, > I've used the following method in other programming languages (Lua, Janet) > and I'm wondering whether I can do something similar in PicoLisp: > > I've got a coroutine that generates an infinite sequence of data. > ... > The upshot of this method is that I don't have to manually kill any of the > coroutines, they are all automatically garbage collected when I no longer > use them. In PicoLisp, execution and garbage collection of coroutines are related in this way: While a coroutine is running - *independent* from whether it is referenced from anywhere else - it will not be garbage collected. Only if it finished execution (either by dropping off the end of its code, by doing a 'throw' outside itself, or by being explicitly stopped by another (co)routine), it will be garbage collected. Collected are then all data which are referenced from the now freed stack segment. > If I understand it correctly, creating a coroutine in PicoLisp creates > a global symbol that keeps the reference to this coroutine This not correct. The coroutine does not create a symbol. It is the Lisp *reader* which finds or creates a symbol 'myCoroutine' when reading an expression like (co 'myCoroutine (...)) But this symbol is just a tag to access the coroutine. It is not relevant for garbage collecting the coroutine itself. This symbol does not need to be global. You can use a transient symbol (co "myCoroutine" (...)) and thus have a file-local scope, or use a namespace. The transient symbol may go out of scope, but the coroutine continues to exist until it terminates as described above. > explicitly remove the coroutine when I no longer need it - it can never get > automatically garbage collected because it's linked to a global symbol. So you indeed need to call (co 'myCoroutine) if you are not sure if it did not already terminate by itself. But this has nothing to do with the tag symbol. Let's clear up this in IRC :) ☺/ A!ex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Test - please ignore
Test ☺ -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe