Skip to content

Commit

Permalink
add Event.waiting_on()
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Sep 24, 2023
1 parent ba9a1d4 commit 2d8ce62
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/core/chuck_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,12 @@ t_CKBOOL init_class_event( Chuck_Env * env, Chuck_Type * type )

// add can_wait()
func = make_new_mfun( "int", "can_wait", event_can_wait );
func->doc = "can the event can be waited on? Internally used by virtual machine for synthronization.";
func->doc = "can the event can be waited on? (internal) used by virtual machine for synchronization.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add waiting_on()
func = make_new_mfun( "void", "waiting_on", event_waiting_on );
func->doc = "(internal) used by virtual machine to be notified when a shred starts waiting on this Event.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add examples
Expand Down Expand Up @@ -441,6 +446,13 @@ t_CKBOOL init_class_event( Chuck_Env * env, Chuck_Type * type )
// remember it
Chuck_Event::our_can_wait = value->func_ref->vt_index;

// find the offset for waiting_on
value = type_engine_find_value( type, "waiting_on" );
assert( value != NULL );
assert( value->func_ref != NULL );
// remember it
Chuck_Event::our_waiting_on = value->func_ref->vt_index;

return TRUE;

error:
Expand Down Expand Up @@ -2051,11 +2063,19 @@ CK_DLL_MFUN( event_wait )
assert( FALSE );
}

// default implementation
CK_DLL_MFUN( event_can_wait )
{
RETURN->v_int = TRUE;
}

// 1.5.1.4 (ge/andrew) added; default implementation
CK_DLL_MFUN( event_waiting_on )
{
// do nothing here; this function could be overridden as needed
// FYI get the calling shred with 'SHRED' parameter to this function
}


//-----------------------------------------------------------------------------
// vec3 API
Expand Down
1 change: 1 addition & 0 deletions src/core/chuck_lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ CK_DLL_MFUN( event_signal );
CK_DLL_MFUN( event_broadcast );
CK_DLL_MFUN( event_wait );
CK_DLL_MFUN( event_can_wait );
CK_DLL_MFUN( event_waiting_on );


//-----------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions src/core/chuck_oo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,7 @@ void Chuck_Array32::sort()

// Chuck_Event static instantiation
t_CKUINT Chuck_Event::our_can_wait = 0;
t_CKUINT Chuck_Event::our_waiting_on = 0;

//-----------------------------------------------------------------------------
// name: signal_local()
Expand Down Expand Up @@ -3442,6 +3443,11 @@ void Chuck_Event::wait( Chuck_VM_Shred * shred, Chuck_VM * vm )

// add shred to shreduler
vm->shreduler()->add_blocked( shred );

// get the virtual function for waiting_on()
f_mfun waiting_on = (f_mfun)this->vtable->funcs[our_waiting_on]->code->native_func;
// call to nofify event that a shred has starting waiting on it
waiting_on( this, NULL, &RETURN, vm, shred, Chuck_DL_Api::Api::instance() );
}
else // can't wait
{
Expand Down
3 changes: 3 additions & 0 deletions src/core/chuck_oo.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,10 @@ struct Chuck_Event : public Chuck_Object
void queue_broadcast( CBufferSimple * event_buffer = NULL );

public:
// virtual table offset for can_wait()
static t_CKUINT our_can_wait;
// virtual table offset for waiting_on() | 1.5.1.4 (ge/andrew) added
static t_CKUINT our_waiting_on;

protected:
std::queue<Chuck_VM_Shred *> m_queue;
Expand Down

0 comments on commit 2d8ce62

Please sign in to comment.