Trying to understand the scheduler

    Aug 18 2015 | 11:37 am
    Hi all,
    Due to my geek attitude, I want to come up with a complete picture of how scheduling works in Max. I have one concept in mind and I'd like to know what do you guys think about it.
    So, my guess is that internally the scheduler is implemented alike Java's priority queue, and when you want to do something, you basically add a task and a time, when it should be executed, to the queue. All tasks in this queue are ordered according to execution time, and the scheduler just pops the tasks from the top of the queue and checks, whether it is time to execute the task or not. If it is, the task is executed, if not—the scheduler does nothing until the next notification (controlled by “Scheduler Interval” setting).
    So, is it far from the reality? :)
    Best, E.

    • Aug 19 2015 | 9:14 pm
      You might want to take a look at the source code to Max's cousin, Pure Data (Pd). The code is freely available, so you can dig around to your heart's content.
      At this point in history, it's quite possible that Cycling '74 has evolved their version of the scheduler from the common ancestor (Max 2.4 or something). In fact, the two may even be entirely different now. Only someone from the company can say.
      I would say that studying the Pd code was a highly educative experience for me. Also interesting to see what Miller did to get a low-latency memory allocation system working (particularly when you look at that code next to a standard implementation of malloc() & Co.)
    • Aug 19 2015 | 11:24 pm
      Hi Peter,
      Thanks for your response. I always hesitated to look into the source as I don't have much C/C++ experience except some small educational stuff. But now I see, that things are not so bad as I expected—it took me about 15 minutes to confirm my hypothesis from the first post.
      Regarding my question, I think I was right about the idea in general. Scheduler is implemented as a linked list (type _clock). Function clock_set is responsible for adding new tasks to the scheduler—the body of this function clearly shows that the new event added to the list at the place according to event's execution time (i.e. how far from now the event should be triggered).
      And the scheduler's main loop is in the function sched_tick. Basically, it just traverses through the linked list, removes task from it and then executes it.
      So, I hope this info may be useful for some other curious minds as mine :)
      Best, E.
      P.S. And, of course, I could've easily went wrong here due to my little programming experience, so trust me with care.