Skip to content

Commit

Permalink
pass vm and shred to native dtors; streamline VM and compiler shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Oct 28, 2023
1 parent dbbf3a6 commit 5d723ae
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 41 deletions.
13 changes: 7 additions & 6 deletions src/core/chuck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ t_CKBOOL ChucK::initVM()

// instantiate VM
m_carrier->vm = new Chuck_VM();
// add reference (this will be released on shtudwon
// add reference (this will be released on shutdown)
CK_SAFE_ADD_REF( m_carrier->vm );
// reference back to carrier
m_carrier->vm->setCarrier( m_carrier );
Expand Down Expand Up @@ -1036,12 +1036,13 @@ t_CKBOOL ChucK::shutdown()
// ensure we have a carrier
if( m_carrier != NULL )
{
// clean up vm, compiler
CK_SAFE_DELETE( m_carrier->compiler );
// release VM (which is itself a Chuck_Obj)
// release VM (itself an Object)
CK_SAFE_RELEASE( m_carrier->vm );
// zero the env out (cleaned up in compiler)
m_carrier->env = NULL;
// delete compiler, including type system (m_carrier->env)
// do this after VM cleanup, as shreds could have objects with type refs
CK_SAFE_DELETE( m_carrier->compiler );
// verify the env pointer is NULL
assert( m_carrier->env == NULL );
}

// clear flag
Expand Down
24 changes: 13 additions & 11 deletions src/core/chuck_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,6 @@ void Chuck_Compiler::shutdown()
// push indent
EM_pushlog();

// need an actual carrier
if( m_carrier != NULL )
{
// free type engine env
type_engine_shutdown( m_carrier );
// check the pointer is now NULL
assert( m_carrier->env == NULL );
// zero out the carrier reference | 1.5.1.1
m_carrier = NULL;
}

// free emitter (and set emitter to NULL)
emit_engine_shutdown( emitter );
// check the pointer is now NULL
Expand All @@ -206,8 +195,21 @@ void Chuck_Compiler::shutdown()
// clear the list
m_dlls.clear();

// log | 1.5.1.8
EM_log( CK_LOG_SYSTEM, "compiler shutown complete." ) ;
// pop indent
EM_poplog();

// if we have carrier
if( m_carrier != NULL )
{
// free type engine env within carrier
type_engine_shutdown( m_carrier );
// check the pointer is now NULL
assert( m_carrier->env == NULL );
// zero out the carrier reference | 1.5.1.1
m_carrier = NULL;
}
}


Expand Down
41 changes: 28 additions & 13 deletions src/core/chuck_oo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,36 +265,49 @@ Chuck_Object::Chuck_Object()


//-----------------------------------------------------------------------------
// name: Chuck_Object()
// desc: ...
// name: ~Chuck_Object()
// desc: chuck object internal destructor
//-----------------------------------------------------------------------------
Chuck_Object::~Chuck_Object()
{
// added 1.3.0.0:
// call destructors, from latest descended child to oldest parent
// get this object's type
Chuck_Type * type = this->type_ref;

// vm ref from origin vm | 1.5.1.8
Chuck_VM * vm = origin_vm;
// no origin vm? get it from type
if( !vm ) vm = type ? type->vm() : NULL;

// shred ref from origin shred | 1.5.1.8
Chuck_VM_Shred * shred = origin_shred;
// if no origin shred, get it from vm | can only be non-NULL during VM compute cycle
if( !shred ) shred = vm ? vm->get_current_shred() : NULL;

// call destructors, from latest descended child to oldest parent | 1.3.0.0
while( type != NULL )
{
// SPENCER TODO: HACK! is there a better way to call the dtor?
if( type->info && type->has_destructor ) // 1.5.0.0 (ge) added type->info check
{
// sanity check
// verify: for now, can only be native func
assert( type->info->dtor && type->info->dtor->native_func );
// REFACTOR-2017: do we know which VM to pass in? (currently NULL)
((f_dtor)(type->info->dtor->native_func))( this, NULL, NULL, Chuck_DL_Api::instance() );
// REFACTOR-2017: do we know which VM to pass in? (diff main/sub instance?)
// pass in type-associated vm and current shred | 1.5.1.8
((f_dtor)(type->info->dtor->native_func))( this, vm, shred, Chuck_DL_Api::instance() );
}

// go up the inheritance
type = type->parent;
}

// release
CK_SAFE_RELEASE( origin_shred );
CK_SAFE_RELEASE( origin_vm );
CK_SAFE_RELEASE( type_ref );
// CK_SAFE_RELEASE( origin_vm );
// just zero out | 1.5.1.8 (ge)
origin_vm = NULL;

// free
CK_SAFE_DELETE( vtable );
CK_SAFE_RELEASE( type_ref );
CK_SAFE_DELETE_ARRAY( data );
}

Expand Down Expand Up @@ -348,12 +361,14 @@ void Chuck_Object::setOriginShred( Chuck_VM_Shred * shred )

//-----------------------------------------------------------------------------
// name: setOriginVM()
// desc: set origin shred
// desc: set origin VM
//-----------------------------------------------------------------------------
void Chuck_Object::setOriginVM( Chuck_VM * vm )
{
// assign
CK_SAFE_REF_ASSIGN( this->origin_vm, vm );
// copy only; avoid refcounting VM on a per-object basis | 1.5.1.8
this->origin_vm = vm;
// reference count assign
// CK_SAFE_REF_ASSIGN( this->origin_vm, vm );
}


Expand Down
37 changes: 34 additions & 3 deletions src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,17 @@ t_CKBOOL type_engine_init( Chuck_Carrier * carrier )
void type_engine_shutdown( Chuck_Carrier * carrier )
{
// log
EM_log( CK_LOG_SEVERE, "shutting down type checker..." );
EM_log( CK_LOG_SEVERE, "shutting down type system..." );
// push
EM_pushlog();

// shut it down; this is system cleanup -- delete instead of release
CK_SAFE_DELETE( carrier->env );

// log
EM_log( CK_LOG_SEVERE, "type checker shutdown complete." );
EM_log( CK_LOG_SEVERE, "type system shutdown complete." );
// pop
EM_poplog();
}


Expand Down Expand Up @@ -5457,7 +5461,7 @@ t_CKBOOL operator <=( const Chuck_Type & lhs, const Chuck_Type & rhs )
}

// if lhs is null and rhs is a object | removed 1.5.1.7?
if( (lhs == *(lhs.env_ref->ckt_null)) && (rhs <= *(rhs.env_ref->ckt_object)) ) return TRUE;
if( (lhs == *(lhs.env()->ckt_null)) && (rhs <= *(rhs.env()->ckt_object)) ) return TRUE;

return FALSE;
}
Expand Down Expand Up @@ -9060,6 +9064,33 @@ Chuck_Type * Chuck_Type::copy( Chuck_Env * env, Chuck_Context * context ) const



//-----------------------------------------------------------------------------
// name: env() | 1.5.1.8 (ge) added method, avoid direct variable access
// desc: get env reference that contains this type
//-----------------------------------------------------------------------------
Chuck_Env * Chuck_Type::env() const
{
return env_ref;
}




//-----------------------------------------------------------------------------
// name: vm() | 1.5.1.8 (ge) added method, avoid direct variable access
// desc: get VM reference associated with this type (via the env)
//-----------------------------------------------------------------------------
Chuck_VM * Chuck_Type::vm() const
{
// no env ref, no vm ref
if( !env_ref ) return NULL;
// return env vm ref
return env_ref->vm();
}




//-----------------------------------------------------------------------------
// name: name()
// desc: get the full name of this type, e.g., "UGen" or "int[][]"
Expand Down
11 changes: 8 additions & 3 deletions src/core/chuck_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,6 @@ struct Chuck_Type : public Chuck_Object
// origin hint
te_Origin originHint;

// reference to environment RE-FACTOR 2017
Chuck_Env * env_ref;

// (within-context, e.g., a ck file) dependency tracking | 1.5.0.8
Chuck_Value_Dependency_Graph depends;

Expand All @@ -982,13 +979,21 @@ struct Chuck_Type : public Chuck_Object
// make a copy of this type struct
Chuck_Type * copy( Chuck_Env * env, Chuck_Context * context ) const;

public:
// get env reference that contains this type | 1.5.1.8
Chuck_Env * env() const;
// get VM reference associated with this type (via the env) | 1.5.1.8
Chuck_VM * vm() const;

public:
// to string: the full name of this type, e.g., "UGen" or "int[][]"
const std::string & name();
// to c string
const char * c_name();

protected:
// reference to environment RE-FACTOR 2017 | get using env()
Chuck_Env * env_ref;
// this for str() and c_name() use only
std::string ret;

Expand Down
26 changes: 21 additions & 5 deletions src/core/chuck_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,6 @@ t_CKBOOL Chuck_VM::shutdown()
// removal all | 1.5.0.8 (ge) updated from previously non-functioning code
this->removeAll();

// log
EM_log( CK_LOG_SYSTEM, "freeing shreduler..." );
// free the shreduler
CK_SAFE_DELETE( m_shreduler );

#ifndef __DISABLE_HID__
// log
EM_log( CK_LOG_SYSTEM, "unregistering VM from HID manager..." );
Expand Down Expand Up @@ -422,6 +417,11 @@ t_CKBOOL Chuck_VM::shutdown()
this->release_dump();
EM_poplog();

// log
EM_log( CK_LOG_SYSTEM, "freeing shreduler..." );
// free the shreduler; do this later as dumped shreds may refer to shreduler
CK_SAFE_DELETE( m_shreduler );

// log
EM_log( CK_LOG_SYSTEM, "freeing special ugens..." );
// go
Expand Down Expand Up @@ -1377,6 +1377,22 @@ t_CKBOOL Chuck_VM::abort_current_shred()



//-----------------------------------------------------------------------------
// name: get_current_shred()
// desc: get currently executing shred
// NOTE this can only be non-NULL during a Chuck_VM::compute() cycle
//-----------------------------------------------------------------------------
Chuck_VM_Shred * Chuck_VM::get_current_shred() const
{
// no shreduler
if( !m_shreduler ) return NULL;
// return shreduler's current shred
return m_shreduler->get_current_shred();
}




//-----------------------------------------------------------------------------
// name: notify_watchers()
// desc: notify watchers for a particular subscription | 1.5.1.5
Expand Down
3 changes: 3 additions & 0 deletions src/core/chuck_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ struct Chuck_VM : public Chuck_Object
t_CKBOOL compute();
// abort current running shred
t_CKBOOL abort_current_shred();
// 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;

public: // invoke functions
t_CKBOOL invoke_static( Chuck_VM_Shred * shred );
Expand Down

0 comments on commit 5d723ae

Please sign in to comment.