<div style="color:black;font: 10pt arial;">yes, it's in the github repo https://github.com/sebshader/lscore.
<div>the interface with pd using pdlua is lscore.pd_lua (relevant part is lscore:postinitialize() and the function self.score:clock_callback)<br>

<div>the scheduler stuff is mainly in lualibs/Score.lua (in Score:new() there's a delay() function in the prototype object, and there's also Score:callback() to deal with the priority queue stuff. the makepqmbr() function returns a new element for the queue with a new coroutine. the add() and addbase() functions add the new members to the priority queue) and the heap implementation is in lualibs/Heap.lua</div>

<div>it's a bit messy right now because I'm in the middle of (slowly) adding the new feature, maybe https://github.com/sebshader/lscore/tree/6ab3499ac5c3b53b2e40b6b01d50679f7975bd97 is better to look at.</div>

<div>In my case I also made a system of dynamic scope, where the scope of any variable is in the nearest ancestor coroutine that defined it. (so if you create a "tempo" variable that variable will be accessible in all descendant coroutines sprouted from it, unless one redefines it)</div>

<div><br>
</div>

<div>-Seb<br>
<br>

<div style="font-family:helvetica,arial;font-size:10pt;color:black"><font size="2">-----Original Message-----<br>
From: Iain Duncan <iainduncanlists@gmail.com><br>
To: Sebastian Shader <sebfumaster@aol.com><br>
Cc: pd-dev@lists.iem.at <pd-dev@lists.iem.at><br>
Sent: Tue, Jun 15, 2021 7:18 am<br>
Subject: Re: [PD-dev] More clock questions<br>
<br>

<div id="yiv0647894613">
<div>
<div dir="ltr">Thanks Seb, that's helpful and  gives me some stuff to chew on. Is the code in which you've done this public?
<div><br clear="none"></div>

<div>iain</div>
</div>
<br clear="none">
<div class="yiv0647894613gmail_quote">
<div class="yiv0647894613yqt3210775193" id="yiv0647894613yqt82489">
<div class="yiv0647894613gmail_attr" dir="ltr">On Sun, Jun 13, 2021 at 9:40 PM Sebastian Shader via Pd-dev <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:pd-dev@lists.iem.at" target="_blank" href="mailto:pd-dev@lists.iem.at">pd-dev@lists.iem.at</a>> wrote:<br clear="none"></div>
<blockquote class="yiv0647894613gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;">

<div style="color:black;font:10pt arial;">><span style="font-family:helvetica, arial;font-size:10pt;">1) Is the Pd clock scheduler only accurate to the ms or does it operate at</span>

<div dir="ltr" style="font-family:helvetica, arial;">>higher than ms accuracy? (As in, if I wanted to role my own mechanism<br clear="none">
</div>



<div dir="ltr" style="font-family:helvetica, arial;">>closer to what Seb did, how frequently should my master time go off?)</div>



<div><br clear="none">
</div>

from what I can tell Pd seems to use its own unit of time in order to interface better with the audio side of things, (but it's stored in a double)

<div>lua uses double for it's numbers, and I just used milliseconds (partially because the pd clocks seem to work/have worked with milliseconds primarily/historically)</div>



<div><br clear="none">
</div>



<div>I'm not sure I understand "how frequently should my master time go off?"<br clear="none">
You have the clock callbacks stored in the priority queue, and use the scheduled time (in ms) for the keys the queue is sorted by.</div>



<div>So in order to "start a delay", you put a callback on the queue. Then to start your scheduler, call the underlying t_clock delay with the value of the difference between the time of the top value in the queue (which will be the earliest callback because you set up the priority queue to sort as a min-heap, so that the lowest times get popped out first) and the current time.</div>



<div>Every time you return from the underlying t_clock callback, increment your own time by the amount the underlying t_clock was called with/delayed for (in milliseconds stored in a double, in my case).</div>



<div><br clear="none">
</div>



<div>The nice thing about using t_clock is that you don't need to implement the delay part yourself, just the priority queue and figuring out when to start and stop it. (along with dispatching the callbacks associated with members in the queue, wherever those go.)</div>



<div><br clear="none">
</div>



<div>What I do is put my "main thread" on the queue to start. Every time another thread/coroutine is started/sprouted it gets added to the queue. Whenever a delay is encountered the current thread will be at the top of the queue, so you just have to change the delay time and call some downheap() function on the heap to restore the heap property after changing the delay time in the priority queue member. (then delay the t_clock by the new top member of the queue)</div>



<div>Every time a coroutine finishes I take it off of the top of the queue. (and call another t_clock delay for the new top member, or stop if there are none left)</div>



<div><br clear="none">
</div>



<div>-Seb<br clear="none">
<br clear="none">


<div style="font-family:helvetica, arial;font-size:10pt;color:black;"><font size="2">-----Original Message-----
</font>
<div dir="ltr">Message: 2<br clear="none">
</div>



<div dir="ltr">Date: Sat, 12 Jun 2021 11:11:12 -0700<br clear="none">
</div>



<div dir="ltr">From: Iain Duncan <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:iainduncanlists@gmail.com" target="_blank" href="mailto:iainduncanlists@gmail.com">iainduncanlists@gmail.com</a>><br clear="none">
</div>



<div dir="ltr">To: pd-dev <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:pd-dev@lists.iem.at" target="_blank" href="mailto:pd-dev@lists.iem.at">pd-dev@lists.iem.at</a>><br clear="none">
</div>



<div dir="ltr">Subject: [PD-dev] More clock questions<br clear="none">
</div>



<div dir="ltr"><br clear="none">
</div>



<div dir="ltr">I have a couple more clock/schedule related questions after rereading the<br clear="none">
</div>



<div dir="ltr">comments from Miller and Seb.<br clear="none">
</div>



<div dir="ltr"><br clear="none">
</div>



<div dir="ltr">1) Is the Pd clock scheduler only accurate to the ms or does it operate at<br clear="none">
</div>



<div dir="ltr">higher than ms accuracy? (As in, if I wanted to role my own mechanism<br clear="none">
</div>



<div dir="ltr">closer to what Seb did, how frequently should my master time go off?)<br clear="none">
</div>



<div dir="ltr"><br clear="none">
</div>



<div dir="ltr">2) Miller made reference to the linked list of clocks that pd iterates<br clear="none">
</div>



<div dir="ltr">through. Is that something I could access directly? (and if so, where would<br clear="none">
</div>



<div dir="ltr">it live?<br clear="none">
</div>



<div dir="ltr"><br clear="none">
</div>



<div dir="ltr">3) if anyone knows good books or online resources for learning more about<br clear="none">
</div>



<div dir="ltr">writing scheduler related code, I'm all ears! :-)<br clear="none">
</div>



<div dir="ltr"><br clear="none">
</div>



<div dir="ltr">thanks<br clear="none">
</div>



<div dir="ltr">iain</div>

</div>

</div>

</div>

_______________________________________________<br clear="none">
Pd-dev mailing list<br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:Pd-dev@lists.iem.at" target="_blank" href="mailto:Pd-dev@lists.iem.at">Pd-dev@lists.iem.at</a><br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="https://lists.puredata.info/listinfo/pd-dev">https://lists.puredata.info/listinfo/pd-dev</a><br clear="none">
</blockquote></div>
</div>

</div>
</div>
</font></div>
</div>
</div>
</div>