I’m trying to implement a library that will allow me to “schedule” function calls that will get called in another thread where they will have access to the scheduling context and be able to schedule additional function calls.
I have a non generic functioning example in this develop branch, the
tests/integration.rs shows some usage.
The basic structure essentially clones channel so that you get a sender and receiver where the sender can queue objects to schedule from one thread and the receiver can get those, update its schedule queue, and execute those, with the ability to queue more from or reschedule the called functions.
In that code I have a struct called MidiCache which has a pop method which simply allocates an item I can schedule. I want to have no allocation in the schedule execution thread as I want to support running this is a “realtime” audio callback.
Which brings me to my problem, and a branch:
In the develop branch I created a queue of pre-allocated “nodes” to use for my schedule, I use a sync_channel that I can grab nodes from in the execution thread and that I fill up in a separate, non real-time, thread. My goal is to make this operation generic so that I can create my scheduler with a variety of different cached items that are accessible in the “execution” thread. The sender side of the cache must implement a trait that fills up these sync_channels, and the receiver side of the cache must at least implement the node allocation that I had in the develop branch, but then it should also be able to implement additional “pop” methods that get other types of boxed items for scheduling in the execution thread, replacing the
MidiCache::pop() that allocates.
I’m hoping for help to get this working. I feel I’m I’m just on the edge of it.
First of all, its looking like my generics aren’t set up quite right, but I’m not sure how. The compiler wants the receiver trait implemented for the sender and visa versa and I’m not sure why. Also, it wants me to give my Cache a
'static lifetime, which I’m not going to want I don’t think, and is probably related to my second question below.
Second, maybe I’m wrong about this but I’m wondering if I can embed the allocation of the cache send/receive into the creation of my “sequencer” so I have less to pass around and could box these objects so I could dynamically allocate a new scheduler pair down the line?
Thanks for any guidance you can provide!
BTW, I have a non-updated
midi.rs module that can be ignored for now, I want to eventually flesh that out and make it another crate with its own cache trait so that I can allow users to require midi object caching and be able to schedule midi objects in their calls.