peers builtin; fixed stop request issuing in starter

This commit is contained in:
Robert Bendun 2023-01-08 07:47:22 +01:00
parent 7924f7e8f8
commit dd8245996c
3 changed files with 24 additions and 2 deletions

View File

@ -1231,10 +1231,16 @@ static Result<Value> builtin_start(Interpreter &interpreter, std::span<Ast> args
{ {
return Try(interpreter.eval((Ast)ast)); return Try(interpreter.eval((Ast)ast));
}); });
// FIXME this should be in "finally" block
interpreter.starter.stop(); interpreter.starter.stop();
return result; return result;
} }
static Result<Value> builtin_peers(Interpreter &interpreter, std::vector<Value>)
{
return Number(interpreter.starter.peers());
}
void Interpreter::register_builtin_functions() void Interpreter::register_builtin_functions()
{ {
auto &global = *Env::global; auto &global = *Env::global;
@ -1264,6 +1270,7 @@ void Interpreter::register_builtin_functions()
global.force_define("oct", builtin_oct); global.force_define("oct", builtin_oct);
global.force_define("par", builtin_par); global.force_define("par", builtin_par);
global.force_define("partition", builtin_partition); global.force_define("partition", builtin_partition);
global.force_define("peers", builtin_peers);
global.force_define("permute", builtin_permute); global.force_define("permute", builtin_permute);
global.force_define("pgmchange", builtin_program_change); global.force_define("pgmchange", builtin_program_change);
global.force_define("pick", builtin_pick); global.force_define("pick", builtin_pick);

View File

@ -23,6 +23,7 @@ struct State
ableton::Link *link; ableton::Link *link;
std::mutex mutex; std::mutex mutex;
std::condition_variable condition; std::condition_variable condition;
std::stop_source source;
}; };
void Starter::start() void Starter::start()
@ -46,24 +47,28 @@ void Starter::start()
{ {
if (is_playing) { if (is_playing) {
state->condition.notify_all(); state->condition.notify_all();
state->source.request_stop();
} }
}); });
std::jthread starter([quantum = quantum, state = state](std::stop_token token) std::thread starter([quantum = quantum, state = state, token = state->source.get_token()]()
{ {
auto counter = 0; auto counter = 0;
while (!token.stop_requested()) { while (!token.stop_requested()) {
auto const time = state->link->clock().micros(); auto const time = state->link->clock().micros();
auto sessionState = state->link->captureAppSessionState(); auto sessionState = state->link->captureAppSessionState();
if (counter == 2) { if (counter == 10) {
sessionState.setIsPlaying(true, time); sessionState.setIsPlaying(true, time);
state->link->commitAppSessionState(sessionState); state->link->commitAppSessionState(sessionState);
state->condition.notify_all();
state->source.request_stop();
return; return;
} }
auto const phase = sessionState.phaseAtTime(time, quantum); auto const phase = sessionState.phaseAtTime(time, quantum);
counter += phase == 0; counter += phase == 0;
} }
}); });
starter.detach();
std::unique_lock lock(state->mutex); std::unique_lock lock(state->mutex);
state->condition.wait(lock); state->condition.wait(lock);
@ -79,3 +84,11 @@ void Starter::stop()
sessionState.setIsPlaying(false, time); sessionState.setIsPlaying(false, time);
link.commitAppSessionState(sessionState); link.commitAppSessionState(sessionState);
} }
size_t Starter::peers() const
{
ensure(impl != nullptr, "Starter wasn't initialized properly");
auto &link = impl->link;
return link.numPeers();
}

View File

@ -9,6 +9,8 @@ struct Starter
void start(); void start();
void stop(); void stop();
size_t peers() const;
struct Implementation; struct Implementation;
std::shared_ptr<Implementation> impl; std::shared_ptr<Implementation> impl;
}; };