Skip to content

Commit

Permalink
add top-level thread-safe remove-all-shreds mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Dec 10, 2024
1 parent 77ecbda commit 3118d7c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/core/chuck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,8 +1384,8 @@ t_CKBOOL ChucK::start()
//-----------------------------------------------------------------------------
void ChucK::run( const SAMPLE * input, SAMPLE * output, t_CKINT numFrames )
{
// make sure we started...
if( !m_started ) this->start();
// make sure we started
if( !m_started && !this->start() ) return;

// call the callback
m_carrier->vm->run( numFrames, input, output );
Expand All @@ -1394,6 +1394,24 @@ void ChucK::run( const SAMPLE * input, SAMPLE * output, t_CKINT numFrames )



//-----------------------------------------------------------------------------
// name: removeAllShreds() | 1.5.4.4 (ge) added
// desc: remove all shreds currently in the VM
// |- (NOTE: not synchronous or truly immediate but is thread-safe;
// |- this will happen at the top of the next VM compute() call)
//-----------------------------------------------------------------------------
void ChucK::removeAllShreds()
{
// check
if( !m_carrier->vm ) return;

// request VM to remove all shreds asap (thread-safe)
m_carrier->vm->remove_all_shreds();
}




//-----------------------------------------------------------------------------
// name: globals()
// desc: returns VM Globals Manager
Expand Down
6 changes: 6 additions & 0 deletions src/core/chuck.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ class ChucK
// |- each audio frame corresponds to one point in time, and contains values for every audio channel
void run( const SAMPLE * input, SAMPLE * output, t_CKINT numFrames );

public:
// remove all shreds currently in the VM | 1.5.4.4 (ge) added
// |- (NOTE: not synchronous or truly immediate, but is thread-safe;
// |- this will happen at the top of the next VM compute() call)
void removeAllShreds();

public:
// get globals (needed to access Globals Manager)
// |- useful for communication between C++ and ChucK using chuck global variables
Expand Down
23 changes: 23 additions & 0 deletions src/core/chuck_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ Chuck_VM::Chuck_VM()
m_event_buffer = NULL;
m_shred_id = 0;
m_shred_check4dupes = FALSE; // 1.5.1.5 (ge)
m_asap_remove_all_shreds = FALSE; // 1.5.4.4 (ge) added

// audio hookups
m_dac = NULL;
Expand Down Expand Up @@ -567,6 +568,15 @@ t_CKBOOL Chuck_VM::compute()
Chuck_Event * event = NULL;
t_CKBOOL iterate = TRUE;

// check if request to remove all shreds | 1.5.4.4
if( m_asap_remove_all_shreds )
{
// call remove all
this->removeAll();
// reset the flag
m_asap_remove_all_shreds = FALSE;
}

// REFACTOR-2017: spork queued shreds, handle global messages
// this is called once per chuck time / sample / "tick"
// global manager added 1.4.1.0 (jack)
Expand Down Expand Up @@ -1454,6 +1464,19 @@ Chuck_VM_Shred * Chuck_VM::get_current_shred() const



//-----------------------------------------------------------------------------
// name: remove_all_shreds() | 1.5.4.4 (ge) added
// desc: remove all shreds asap (thread-safe) will enact atop next compute()
//-----------------------------------------------------------------------------
void Chuck_VM::remove_all_shreds()
{
// set the flag
m_asap_remove_all_shreds = TRUE;
}




//-----------------------------------------------------------------------------
// name: register_callback_on_shutdown()
// cesc: register a callback to be called on VM shutdown | 1.5.2.5 (ge) added
Expand Down
6 changes: 6 additions & 0 deletions src/core/chuck_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ struct Chuck_VM : public Chuck_Object
// get currently executing shred | 1.5.1.8 (ge) now in VM, in addition to shreduler
// NOTE this can only be non-NULL during a Chuck_VM::compute() cycle
Chuck_VM_Shred * get_current_shred() const;
// remove all shreds asap (thread-safe) | 1.5.4.4 (ge) added
void remove_all_shreds();

public: // invoke functions
t_CKBOOL invoke_static( Chuck_VM_Shred * shred );
Expand Down Expand Up @@ -770,6 +772,10 @@ struct Chuck_VM : public Chuck_Object
void dump_shred( Chuck_VM_Shred * shred );
void release_dump();

// special flag to remove all shreds in the VM...
// as soon as safely possible | 1.5.4.4 (ge) added
t_CKBOOL m_asap_remove_all_shreds;

protected:
t_CKBOOL m_init;
std::string m_last_error;
Expand Down

0 comments on commit 3118d7c

Please sign in to comment.