Overnight, a couple of other strategies occurred to me.
On-demand ProtoDefs
A ProtoDef is made from a function. You could store a library of functions to initialize different types of ProtoDef:
e.g., protodefs.scd:
Library.put(\protoDefs, \busManager, {
~server = Server.default;
~otherVariables ...
~initDef = { |self|
...
}
});
Library.put(\protoDefs, \bufferManager, {
~server = Server.default;
~otherVariables ...
~initDef = { |self|
...
}
});
This can be done anytime (before or after booting the server), because it’s not doing any of the actual work yet.
Then, after the server is booted, create ProtoDefs as needed:
ProtoDef(\buses, Library.at(\protoDefs, \busManager))
So the functions are like class definitions, and the ProtoDef calls are like making instances of those classes… loose analogy but apt. In the main class library, you don’t expect that every class is going to instantiate during language startup – they are definitions to use when you need them.
Again, the idea is to separate loading the files away from the server boot process.
CondVar how to
CondVar would allow you to create ProtoDefs at any time, but defer their initialization until after server starts.
The catch is that something needs to signal the CondVar – meaning that there has to be a shared CondVar, or collection of them. The server boot process can’t signal CondVars that it doesn’t know about.
So I’d do maybe like this:
(
~serverBootCond = CondVar.new;
~bootSignal = {
s.waitForBoot {
~serverBootCond.signalAll
}
};
ProtoDef(\demo, {
~inited = false;
~initDef = { |self|
fork {
~serverBootCond.wait { s.serverRunning };
"ok, server is up, ready to init stuff".postln;
self.inited = true;
}
}
});
)
ProtoDef(\demo).inited // false
~bootSignal.value;
... snip...
Shared memory server interface initialized
ok, server is up, ready to init stuff
// wait... then:
ProtoDef(\demo).inited // true
The advantage of this approach is: if you add a ProtoDef after booting the server, it will go ahead with the initialization (because the condition s.serverRunning
is already met).
So, three approaches in the last couple of messages:
- Load ProtoDefs first, then boot the server, and use
defOnServerBoot
.
- Load functions first, then boot the server, then create ProtoDefs from the functions (with no need to sync because the server will already be up).
- Load ProtoDefs at any time, with reference to a global server-boot CondVar.
Use only one of these – mixing and matching will only cause you trouble.
hjh