Skip to content

Commit

Permalink
tightened origin_shred semantics; reduce overall dependency between s…
Browse files Browse the repository at this point in the history
…hred and language-level object instances
  • Loading branch information
gewang committed Oct 29, 2023
1 parent 95eaa79 commit b9caca9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/core/chuck_dl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ static Chuck_DL_Api::Object CK_DLL_CALL ck_create_without_shred( Chuck_VM * vm,
if( t == NULL )
{
// print error message
EM_error2( 0, "DL_Api:object:ck_create_no_shred: NULL object reference." );
EM_error2( 0, "DL_Api:object:ck_create_without_shred: NULL object reference." );
// done
return NULL;
}
Expand Down Expand Up @@ -2114,7 +2114,7 @@ Chuck_DL_Return CK_DLL_CALL ck_invoke_mfun_immediate_mode( Chuck_Object * obj, t
// iterate over c-style array
for( t_CKUINT i = 0; i < numArgs; i++ ) args_vector.push_back(args_list[i]);
// invoke the invoker
func->invoker_mfun->invoke( obj, args_vector );
func->invoker_mfun->invoke( obj, args_vector, caller_shred );
}

// return it
Expand Down
17 changes: 6 additions & 11 deletions src/core/chuck_dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,22 @@
#include <vector>




// major version must be the same between chuck:chugin
#define CK_DLL_VERSION_MAJOR (0x0009)
#define CK_DLL_VERSION_MAJOR (9)
// minor version of chugin must be less than or equal to chuck's
#define CK_DLL_VERSION_MINOR (0x0000)
#define CK_DLL_VERSION_MINOR (0)
#define CK_DLL_VERSION_MAKE(maj,min) ((t_CKUINT)(((maj) << 16) | (min)))
#define CK_DLL_VERSION_GETMAJOR(v) (((v) >> 16) & 0xFFFF)
#define CK_DLL_VERSION_GETMINOR(v) ((v) & 0xFFFF)
#define CK_DLL_VERSION (CK_DLL_VERSION_MAKE(CK_DLL_VERSION_MAJOR, CK_DLL_VERSION_MINOR))




// string literal containing default chugin path (platform-specific)
extern char g_default_chugin_path[];
// string literal containing environment variable for chugin search path
extern char g_chugin_path_envvar[];



// DL forward references
struct Chuck_DL_Query;
struct Chuck_DL_Class;
Expand Down Expand Up @@ -888,16 +885,14 @@ struct Chuck_DL_Api
void (CK_DLL_CALL * const release)( Object object );
// get reference count
t_CKUINT (CK_DLL_CALL * const refcount)( Object object );
// instantiating and initializing a ChucK object by type, with reference to a parent shred
// instantiating and initializing a ChucK object by type
// if addRef == TRUE the newly created object will have a reference count of 1; otherwise 0
// NOTE set addRef to TRUE if you intend to keep a reference of the newly created object around (e.g., in the chugin)
// NOTE set addRef to FALSE if the created object is to be returned without keeping a reference around
Object (CK_DLL_CALL * const create)( Chuck_VM_Shred *, Type type, t_CKBOOL addRef );
// instantiating and initializing a ChucK object by type, with no reference to a parent shred
// if addRef == TRUE the newly created object will have a reference count of 1; otherwise 0
Object (CK_DLL_CALL * const create_without_shred)( Chuck_VM *, Type type, t_CKBOOL addRef );
// instantiate and initialize a ChucK string by type
// if addRef == TRUE the newly created object will have a reference count of 1; otherwise 0
// instantiate and initialize a ChucK string by type (without ref to a parent shred)
String (CK_DLL_CALL * const create_string)( Chuck_VM *, const char * value, t_CKBOOL addRef );
// get the origin shred
Chuck_VM_Shred * (CK_DLL_CALL * const get_origin_shred)( Object object );
Expand Down
29 changes: 12 additions & 17 deletions src/core/chuck_instr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4226,7 +4226,7 @@ void Chuck_Instr_Pre_Constructor::execute( Chuck_VM * vm, Chuck_VM_Shred * shred

//-----------------------------------------------------------------------------
// name: instantiate_object()
// desc: ...
// desc: instantiate Object including data and virtual table
//-----------------------------------------------------------------------------
t_CKBOOL initialize_object( Chuck_Object * object, Chuck_Type * type, Chuck_VM_Shred * shred, Chuck_VM * vm, t_CKBOOL setShredOrigin )
{
Expand All @@ -4239,20 +4239,8 @@ t_CKBOOL initialize_object( Chuck_Object * object, Chuck_Type * type, Chuck_VM_S

// REFACTOR-2017: added | 1.5.1.5 (ge & andrew) moved here from instantiate_...
object->setOriginVM( vm );
// check if origin shred is available | 1.5.1.5 (ge)
if( shred )
{
// set the origin shred only in specific cases...
// UGens: needs shred for auto-disconnect when shred is removed
// user-defined classes (that refer to global-scope variables):
// ...needs shred to access the global-scope variables across sporking
// setShredOrigin: if true, this is likely a registered callback_on_instantiate
if( type->ugen_info || type->originHint == te_originUserDefined || setShredOrigin )
{
// set origin shred | 1.5.1.5 (ge) was: ugen->shred = shred;
object->setOriginShred( shred );
}
}
// set origin shred for non-ugens | 1.5.1.5 (ge & andrew) moved here from instantiate_...
if( !type->ugen_info && setShredOrigin ) object->setOriginShred( shred );

// allocate virtual table
object->vtable = new Chuck_VTable;
Expand Down Expand Up @@ -4281,8 +4269,15 @@ t_CKBOOL initialize_object( Chuck_Object * object, Chuck_Type * type, Chuck_VM_S
{
// ugen
Chuck_UGen * ugen = (Chuck_UGen *)object;
// add ugen to shred | 1.5.1.5 (ge & andrew) moved from instantiate_and_initialize_object()
if( shred ) shred->add( ugen );
// UGens: needs shred for auto-disconnect when shred is removed
// 1.5.1.5 (ge & andrew) moved from instantiate_and_initialize_object()
if( shred )
{
// add ugen to shred (ref-counted)
shred->add( ugen );
// add shred to ugen (ref-counted) | 1.5.1.5 (ge) was: ugen->shred = shred;
object->setOriginShred( shred );
}
// set tick
if( type->ugen_info->tick ) ugen->tick = type->ugen_info->tick;
// added 1.3.0.0 -- tickf for multi-channel tick
Expand Down
52 changes: 28 additions & 24 deletions src/core/chuck_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,7 @@ t_CKBOOL Chuck_VM_Shred::add( Chuck_UGen * ugen )
return FALSE;

// increment reference count (added 1.3.0.0)
ugen->add_ref();
CK_SAFE_ADD_REF( ugen );

// RUBBISH
// cerr << "vm add ugen: 0x" << hex << (int)ugen << endl;
Expand Down Expand Up @@ -3193,7 +3193,7 @@ void Chuck_VM_Status::clear()
Chuck_VM_MFunInvoker::Chuck_VM_MFunInvoker()
{
// zero
shred = NULL;
invoker_shred = NULL;
instr_pushThis = NULL;
instr_pushReturnVar = NULL;
}
Expand All @@ -3220,7 +3220,7 @@ Chuck_VM_MFunInvoker::~Chuck_VM_MFunInvoker()
t_CKBOOL Chuck_VM_MFunInvoker::setup( Chuck_Func * func, t_CKUINT func_vt_offset, Chuck_VM * vm, Chuck_VM_Shred * caller )
{
// clean up first
if( shred ) cleanup();
if( invoker_shred ) cleanup();

// a vector of VM instructions
vector<Chuck_Instr *> instructions;
Expand Down Expand Up @@ -3387,15 +3387,15 @@ t_CKBOOL Chuck_VM_MFunInvoker::setup( Chuck_Func * func, t_CKUINT func_vt_offset
code->need_this = FALSE;

// create dedicated shred
shred = new Chuck_VM_Shred;
invoker_shred = new Chuck_VM_Shred;
// set the VM ref (needed by initialize)
shred->vm_ref = vm;
invoker_shred->vm_ref = vm;
// initialize with code + allocate stacks
shred->initialize( code );
invoker_shred->initialize( code );
// set name
shred->name = func->signature(FALSE,TRUE);
invoker_shred->name = func->signature(FALSE,TRUE);
// enter immediate mode (will throw runtime exception on any time/event ops)
shred->setImmediateMode( TRUE );
invoker_shred->setImmediateMode( TRUE );

// done
return TRUE;
Expand Down Expand Up @@ -3442,7 +3442,7 @@ static Chuck_Instr_Reg_Push_Imm2 * ckvm_next_instr_as_float( vector<Chuck_Instr
// name: invoke()
// desc: invoke the mfun
//-----------------------------------------------------------------------------
Chuck_DL_Return Chuck_VM_MFunInvoker::invoke( Chuck_Object * obj, const vector<Chuck_DL_Arg> & args )
Chuck_DL_Return Chuck_VM_MFunInvoker::invoke( Chuck_Object * obj, const vector<Chuck_DL_Arg> & args, Chuck_VM_Shred * parent_shred )
{
// the return value
Chuck_DL_Return RETURN;
Expand All @@ -3452,7 +3452,7 @@ Chuck_DL_Return Chuck_VM_MFunInvoker::invoke( Chuck_Object * obj, const vector<C
// index
t_CKUINT index = 0;
// no shred?
if( !shred ) return RETURN;
if( !invoker_shred ) return RETURN;
// verify
assert( instr_pushThis != NULL );

Expand Down Expand Up @@ -3518,32 +3518,36 @@ Chuck_DL_Return Chuck_VM_MFunInvoker::invoke( Chuck_Object * obj, const vector<C
if( instr_pushReturnVar ) instr_pushReturnVar->set( (t_CKUINT)&RETURN );

// reset shred: program counter
shred->pc = 0;
invoker_shred->pc = 0;
// next pc
shred->next_pc = 1;
invoker_shred->next_pc = 1;

// set parent
invoker_shred->parent = parent_shred;
// commented out: parent if any should have been set in setup()
// invoker_shred->parent = obj->originShred();

// set parent base_ref; in case mfun is part of a non-public class
// that can access file-global variables outside the class definition
shred->parent = obj->originShred();
if( shred->parent ) shred->base_ref = shred->parent->base_ref;
else shred->base_ref = shred->mem;
if( invoker_shred->parent ) invoker_shred->base_ref = invoker_shred->parent->base_ref;
else invoker_shred->base_ref = invoker_shred->mem;

// shred in dump (all done)
shred->is_dumped = FALSE;
invoker_shred->is_dumped = FALSE;
// shred done
shred->is_done = FALSE;
invoker_shred->is_done = FALSE;
// shred running
shred->is_running = FALSE;
invoker_shred->is_running = FALSE;
// shred abort
shred->is_abort = FALSE;
invoker_shred->is_abort = FALSE;
// set the instr
shred->instr = shred->code->instr;
invoker_shred->instr = invoker_shred->code->instr;
// zero out the id (shred is in immediate mode and cannot be shreduled)
shred->xid = 0;
invoker_shred->xid = 0;
// inherit now from vm
shred->now = shred->vm_ref->now();
invoker_shred->now = invoker_shred->vm_ref->now();
// run shred on VM
shred->run( shred->vm_ref );
invoker_shred->run( invoker_shred->vm_ref );

// done; by the point, return should have been filled with return value, if func has one
return RETURN;
Expand All @@ -3564,7 +3568,7 @@ void Chuck_VM_MFunInvoker::cleanup()
{
// release shred reference
// NB this should also cleanup the code and VM instruction we created in setup
CK_SAFE_RELEASE( shred );
CK_SAFE_RELEASE( invoker_shred );

// clear the arg instructions
instr_args.clear();
Expand Down
5 changes: 3 additions & 2 deletions src/core/chuck_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,14 @@ struct Chuck_VM_MFunInvoker
Chuck_VM * vm, Chuck_VM_Shred * caller );
// invoke the member function
Chuck_DL_Return invoke( Chuck_Object * obj,
const std::vector<Chuck_DL_Arg> & args );
const std::vector<Chuck_DL_Arg> & args,
Chuck_VM_Shred * parent_shred );
// clean up
void cleanup();

public:
// dedicated shred to call the mfun on
Chuck_VM_Shred * shred;
Chuck_VM_Shred * invoker_shred;
// instructions for args (to be filled on invoke)
std::vector<Chuck_Instr *> instr_args;
// instruction to update on invoke: pushing this pointer
Expand Down
2 changes: 1 addition & 1 deletion src/core/ugen_xxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ CK_DLL_TICK( foogen_tick )
// set input
data->input = in;
// invoke the function | 1.5.1.5 (ge) encapsulated into invoker
ret = data->invoker->invoke( SELF, args );
ret = data->invoker->invoke( SELF, args, SELF->originShred() );
// set output
data->output = ret.v_float;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/ulib_opsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ CK_DLL_MFUN( oscmsg_numArgs )

CK_DLL_MFUN( oscmsg_getInt )
{
int i = GET_NEXT_INT( ARGS );
t_CKINT i = GET_NEXT_INT( ARGS );
Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
Chuck_Object * arg_obj;

Expand All @@ -1602,7 +1602,7 @@ CK_DLL_MFUN( oscmsg_getInt )

CK_DLL_MFUN( oscmsg_getFloat )
{
int i = GET_NEXT_INT( ARGS );
t_CKINT i = GET_NEXT_INT( ARGS );
Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
Chuck_Object * arg_obj;
if( i >= 0 && i < args_obj->size() )
Expand All @@ -1616,7 +1616,7 @@ CK_DLL_MFUN( oscmsg_getFloat )

CK_DLL_MFUN( oscmsg_getString )
{
int i = GET_NEXT_INT( ARGS );
t_CKINT i = GET_NEXT_INT( ARGS );
Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
Chuck_Object * arg_obj;

Expand Down

0 comments on commit b9caca9

Please sign in to comment.