Skip to content

Commit

Permalink
add chugin API for callback_on_shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed May 10, 2024
1 parent b9b41d6 commit 11322f4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/chuck_dl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,16 @@ Chuck_DL_MainThreadHook * CK_DLL_CALL ck_create_main_thread_hook( Chuck_DL_Query
return new Chuck_DL_MainThreadHook( hook, quit, bindle, query->carrier() );
}

//-----------------------------------------------------------------------------
// name: ck_register_callback_on_shutdown() | 1.5.2.5 (ge) added
// desc: register a callback function to be called on host exit
// (both natural or SIGINT)
//-----------------------------------------------------------------------------
void CK_DLL_CALL ck_register_callback_on_shutdown( Chuck_DL_Query * query, f_callback_on_shutdown cb, void * bindle )
{
// register
query->vm()->register_callback_on_shutdown( cb, bindle );
}

//-----------------------------------------------------------------------------
// name: ck_register_shreds_watcher()
Expand Down Expand Up @@ -1351,6 +1361,7 @@ Chuck_DL_Query::Chuck_DL_Query( Chuck_Carrier * carrier, Chuck_DLL * dll )
create_main_thread_hook = ck_create_main_thread_hook;
register_shreds_watcher = ck_register_shreds_watcher; // 1.5.1.5 (ge & andrew)
unregister_shreds_watcher = ck_unregister_shreds_watcher; // 1.5.1.5 (ge & andrew)
register_callback_on_shutdown = ck_register_callback_on_shutdown; // 1.5.2.5 (ge)
m_carrier = carrier;
dll_ref = dll; // 1.5.1.3 (ge) added

Expand Down
12 changes: 12 additions & 0 deletions src/core/chuck_dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ typedef t_CKBOOL (CK_DLL_CALL * f_tock)( Chuck_Object * SELF, Chuck_UAna * UANA,
typedef t_CKBOOL (CK_DLL_CALL * f_mainthreadhook)( void * bindle );
// "main thread" quit (stop running hook)
typedef t_CKBOOL (CK_DLL_CALL * f_mainthreadquit)( void * bindle );
// callback function, called on host shutdown
typedef void (CK_DLL_CALL * f_callback_on_shutdown)( void * bindle );
// shreds watcher callback
typedef void (CK_DLL_CALL * f_shreds_watcher)( Chuck_VM_Shred * SHRED, t_CKINT CODE, t_CKINT PARAM, Chuck_VM * VM, void * BINDLE );
// type instantiation callback
Expand Down Expand Up @@ -414,6 +416,8 @@ typedef void (CK_DLL_CALL * f_add_ugen_funcf_auto_num_channels)( Chuck_DL_Query
typedef t_CKBOOL (CK_DLL_CALL * f_end_class)( Chuck_DL_Query * query );
// create main thread hook- used for executing a "hook" function in the main thread of a primary chuck instance
typedef Chuck_DL_MainThreadHook * (CK_DLL_CALL * f_create_main_thread_hook)( Chuck_DL_Query * query, f_mainthreadhook hook, f_mainthreadquit quit, void * bindle );
// register a callback to be called on host shutdown, e.g., for chugin cleanup
typedef void (CK_DLL_CALL * f_register_callback_on_shutdown)( Chuck_DL_Query * query, f_callback_on_shutdown cb, void * bindle );
// register a callback function to receive notification from the VM about shreds (add, remove, etc.)
typedef void (CK_DLL_CALL * f_register_shreds_watcher)( Chuck_DL_Query * query, f_shreds_watcher cb, t_CKUINT options, void * bindle );
// unregister a shreds notification callback
Expand Down Expand Up @@ -589,6 +593,14 @@ struct Chuck_DL_Query
// -------------
f_create_main_thread_hook create_main_thread_hook;

public:
// -------------
// register a function to be run on host shutdown; this can be used
// for chugin cleanup when the host (chuck, miniAudicle, etc.) exits
// including on SIGINT (ctrl-c termination) | added 1.5.2.5 (ge)
// -------------
f_register_callback_on_shutdown register_callback_on_shutdown;

public:
// -------------
// register callback to be invoked by chuck host at various
Expand Down
49 changes: 49 additions & 0 deletions src/core/chuck_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ t_CKBOOL Chuck_VM::shutdown()
// set state
m_init = FALSE;

// notify registered callbacks we are shutting down | 1.5.2.5 (ge) added
notify_callbacks_on_shutdown();

// relockdown (added 1.3.6.0)
// REFACTOR-2017: TODO -- remove once made per-VM
Chuck_VM_Object::lock_all();
Expand Down Expand Up @@ -1431,6 +1434,52 @@ Chuck_VM_Shred * Chuck_VM::get_current_shred() const



//-----------------------------------------------------------------------------
// name: register_callback_on_shutdown()
// cesc: register a callback to be called on VM shutdown | 1.5.2.5 (ge) added
//-----------------------------------------------------------------------------
void Chuck_VM::register_callback_on_shutdown( f_callback_on_shutdown cb, void * bindle )
{
// check
if( !cb ) return;

// ensure no duplicates
list<Chuck_VM_Callback_On_Shutdown>::iterator it = std::find( m_callbacks_on_shutdown.begin(),
m_callbacks_on_shutdown.end(), cb );
// add if not already preset
if( it == m_callbacks_on_shutdown.end() )
{
// append
m_callbacks_on_shutdown.push_back( Chuck_VM_Callback_On_Shutdown(cb, bindle) );
}
}




//-----------------------------------------------------------------------------
// name: notify_callbacks_on_shutdown()
// desc: notify callbacks on VM shutdown | 1.5.2.5 (ge) added
//-----------------------------------------------------------------------------
void Chuck_VM::notify_callbacks_on_shutdown()
{
// the function to call eventually
f_callback_on_shutdown f = NULL;
// iterator
list<Chuck_VM_Callback_On_Shutdown>::iterator it;
// iterate
for( it = m_callbacks_on_shutdown.begin(); it != m_callbacks_on_shutdown.end(); it++ )
{
// the function
f = (*it).cb;
// call it with user data
f( (*it).userdata );
}
}




//-----------------------------------------------------------------------------
// name: notify_watchers()
// desc: notify watchers for a particular subscription | 1.5.1.5
Expand Down
39 changes: 39 additions & 0 deletions src/core/chuck_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,33 @@ struct Chuck_VM_Status : public Chuck_Object



//-----------------------------------------------------------------------------
// name: struct Chuck_VM_Callback_On_Shutdown
// desc: an entry for a callback to be called on VM shutdown
//-----------------------------------------------------------------------------
struct Chuck_VM_Callback_On_Shutdown
{
// function pointer to call
f_callback_on_shutdown cb;
// user data
void * userdata;

// constructor
Chuck_VM_Callback_On_Shutdown( f_callback_on_shutdown f = NULL, void * data = NULL )
: cb(f), userdata(data) { }

// copy constructor
Chuck_VM_Callback_On_Shutdown( const Chuck_VM_Callback_On_Shutdown & other )
: cb(other.cb), userdata(other.userdata) { }

// ==
bool operator ==( const Chuck_VM_Callback_On_Shutdown & other )
{ return this->cb == other.cb; }
};




//-----------------------------------------------------------------------------
// name: struct Chuck_VM_Shreduler
// desc: a ChucK shreduler shredules shreds
Expand Down Expand Up @@ -657,6 +684,14 @@ struct Chuck_VM : public Chuck_Object
// remove shreds watcher callback | 1.5.1.5
void remove_watcher( f_shreds_watcher cb );

public:
// register a callback to be called on VM shutdown | 1.5.2.5 (ge) added
void register_callback_on_shutdown( f_callback_on_shutdown cb, void * bindle );

protected:
// notify callbacks on VM shutdown | 1.5.2.5 (ge) added
void notify_callbacks_on_shutdown();

//-----------------------------------------------------------------------------
// data
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -733,6 +768,10 @@ struct Chuck_VM : public Chuck_Object
std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_remove;
std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_suspend;
std::list<Chuck_VM_Shreds_Watcher> m_shreds_watchers_activate;

protected:
// 1.5.2.5 (ge) on major VM events callbacks
std::list<Chuck_VM_Callback_On_Shutdown> m_callbacks_on_shutdown;
};


Expand Down

0 comments on commit 11322f4

Please sign in to comment.