Sirikata
|
#include <JSTimerStruct.hpp>
Public Member Functions | |
JSTimerStruct (EmersonScript *eobj, Duration dur, v8::Persistent< v8::Function > &callback, JSContextStruct *jscont, uint32 contID, double timeRemaining, bool isSuspended, bool isCleared, JSCtx *mCtx) | |
~JSTimerStruct () | |
v8::Handle< v8::Value > | struct_resetTimer (double timeInSecondsToRefire) |
virtual v8::Handle< v8::Value > | suspend () |
virtual v8::Handle< v8::Value > | resume () |
virtual v8::Handle< v8::Value > | clear () |
v8::Handle< v8::Value > | struct_getAllData () |
virtual void | fixSuspendableToContext (JSContextStruct *toAttachTo) |
void | noReference (const Liveness::Token &token) |
When no longer have a reference to the Emerson object holding this timer and garbage collection is occurring, this function gets called. | |
void | setPersistentObject (v8::Persistent< v8::Object >) |
Every time a timer struct is created, the next line should set its persistent object. | |
Static Public Member Functions | |
static JSTimerStruct * | decodeTimerStruct (v8::Handle< v8::Value > toDecode, String &errorMessage) |
static void | timerWeakReferenceCleanup (v8::Persistent< v8::Value > containsTimer, void *otherArg) |
When the only reference to a timer are weak (ie, no emerson objects directly point at it, this function gets called. | |
Public Attributes | |
EmersonScript * | emerScript |
v8::Persistent< v8::Function > | cb |
JSContextStruct * | jsContStruct |
Duration | timeUntil |
double | mTimeRemaining |
Private Member Functions | |
void | evaluateCallback (Liveness::Token isAlive) |
void | iEvaluateCallback (Liveness::Token token) |
Private Attributes | |
JSCtx * | mCtx |
Sirikata::Network::IOTimerPtr | mDeadlineTimer |
bool | amExecuting |
Means that we're currently processing the callback of a timer. | |
bool | killAfterFire |
Need to know when to clean up timer struct. | |
bool | noTimerWaiting |
True if have no callback waiting to be executed. | |
v8::Persistent< v8::Object > | mPersistentHandle |
On clear, should set mPersistentHandle's internal fields to NULL. |
Sirikata::JS::JSTimerStruct::JSTimerStruct | ( | EmersonScript * | eobj, |
Duration | dur, | ||
v8::Persistent< v8::Function > & | callback, | ||
JSContextStruct * | jscont, | ||
uint32 | contID, | ||
double | timeRemaining, | ||
bool | isSuspended, | ||
bool | isCleared, | ||
JSCtx * | mCtx | ||
) |
Sirikata::JS::JSTimerStruct::~JSTimerStruct | ( | ) |
References clear(), and Sirikata::Liveness::letDie().
v8::Handle< v8::Value > Sirikata::JS::JSTimerStruct::clear | ( | ) | [virtual] |
Reimplemented from Sirikata::JS::JSSuspendable.
References amExecuting, cb, CHECK_INTERNAL_FIELD_COUNT, DEL_TYPEID_AND_CHECK, Sirikata::JS::JSSuspendable::getIsCleared(), Sirikata::Logging::insane, jsContStruct, JSLOG, Sirikata::Liveness::livenessToken(), mCtx, mDeadlineTimer, mPersistentHandle, Sirikata::JS::JSCtx::objStrand, Sirikata::JS::JSContextStruct::struct_asyncDeregisterSuspendable(), Sirikata::JS::JSContextStruct::struct_deregisterSuspendable(), TIMER_JSTIMER_TEMPLATE_FIELD_COUNT, TIMER_JSTIMERSTRUCT_FIELD, and TIMER_TYPEID_STRING.
Referenced by JSTimerStruct(), noReference(), struct_resetTimer(), and ~JSTimerStruct().
JSTimerStruct * Sirikata::JS::JSTimerStruct::decodeTimerStruct | ( | v8::Handle< v8::Value > | toDecode, |
String & | errorMessage | ||
) | [static] |
References TIMER_JSTIMER_TEMPLATE_FIELD_COUNT, and TIMER_JSTIMERSTRUCT_FIELD.
Referenced by Sirikata::JS::JSTimer::getAllData(), Sirikata::JS::JSTimer::isSuspended(), Sirikata::JS::JSTimer::resetTimer(), Sirikata::JS::JSTimer::resume(), Sirikata::JS::JSTimer::suspend(), and timerWeakReferenceCleanup().
void Sirikata::JS::JSTimerStruct::evaluateCallback | ( | Liveness::Token | isAlive | ) | [private] |
References iEvaluateCallback(), mCtx, Sirikata::JS::JSCtx::objStrand, and Sirikata::JS::JSCtx::stopped().
Referenced by fixSuspendableToContext(), JSTimerStruct(), resume(), and struct_resetTimer().
void Sirikata::JS::JSTimerStruct::fixSuspendableToContext | ( | JSContextStruct * | toAttachTo | ) | [virtual] |
void Sirikata::JS::JSTimerStruct::iEvaluateCallback | ( | Liveness::Token | token | ) | [private] |
References amExecuting, cb, emerScript, Sirikata::JS::JSCtx::initialized(), Sirikata::JS::EmersonScript::invokeCallbackInContext(), jsContStruct, JSLOG, killAfterFire, Sirikata::Liveness::livenessToken(), mCtx, noReference(), noTimerWaiting, Sirikata::JS::JSCtx::objStrand, Sirikata::JS::JSCtx::stopped(), and Sirikata::Logging::warn.
Referenced by evaluateCallback().
void Sirikata::JS::JSTimerStruct::noReference | ( | const Liveness::Token & | alive | ) |
When no longer have a reference to the Emerson object holding this timer and garbage collection is occurring, this function gets called.
Called by timerWeakReferenceCleanup and after firing timer when killAfterFire is true.
If the timer has no pending events (and won't have any pending events in the future [eg, it isn't suspended and its parent context isn't suspended]), then delete the timer right then and there. Otherwise, delete it after it fires. (Mark killAfterFire to be true.)
iotimer | is a keepalive token to the IOTimer. If the struct is GC'd then this will no longer resolve to a shared reference and this class is dead. |
Means that no Emerson objects hold references to this timer any longer and that this object should either: 1) Free itself if there's no way that its timer will re-fire. 2) Schedule itself to be freed after its timer fires.
For case 1: the timer has already fired, and (the timer isn't suspended and its context isn't suspended). For case 2: just sets killAfterFire to be true. Will re-evaluate these conditions after timer actually fires.
References clear(), Sirikata::JS::JSSuspendable::getIsSuspended(), jsContStruct, killAfterFire, mCtx, Sirikata::JS::JSCtx::mIsolate, and noTimerWaiting.
Referenced by iEvaluateCallback(), and timerWeakReferenceCleanup().
v8::Handle< v8::Value > Sirikata::JS::JSTimerStruct::resume | ( | ) | [virtual] |
Reimplemented from Sirikata::JS::JSSuspendable.
References evaluateCallback(), Sirikata::JS::JSSuspendable::getIsCleared(), Sirikata::JS::JSSuspendable::getIsSuspendedV8(), Sirikata::Logging::info, JSLOG, Sirikata::Liveness::livenessToken(), mDeadlineTimer, noTimerWaiting, and timeUntil.
Referenced by Sirikata::JS::JSTimer::resume(), and struct_resetTimer().
void Sirikata::JS::JSTimerStruct::setPersistentObject | ( | v8::Persistent< v8::Object > | pers | ) |
Every time a timer struct is created, the next line should set its persistent object.
References mPersistentHandle.
Referenced by Sirikata::JS::EmersonScript::create_timeout().
v8::Handle< v8::Value > Sirikata::JS::JSTimerStruct::struct_getAllData | ( | ) |
v8::Handle< v8::Value > Sirikata::JS::JSTimerStruct::struct_resetTimer | ( | double | timeInSecondsToRefire | ) |
v8::Handle< v8::Value > Sirikata::JS::JSTimerStruct::suspend | ( | ) | [virtual] |
Reimplemented from Sirikata::JS::JSSuspendable.
References Sirikata::JS::JSSuspendable::getIsCleared(), Sirikata::Logging::info, Sirikata::Logging::insane, JSLOG, mDeadlineTimer, and noTimerWaiting.
Referenced by JSTimerStruct(), and Sirikata::JS::JSTimer::suspend().
void Sirikata::JS::JSTimerStruct::timerWeakReferenceCleanup | ( | v8::Persistent< v8::Value > | containsTimer, |
void * | otherArg | ||
) | [static] |
When the only reference to a timer are weak (ie, no emerson objects directly point at it, this function gets called.
The first argument contains the persistent object pointing to the timer. The second should just be null.
References CHECK_INTERNAL_FIELD_COUNT, decodeTimerStruct(), Sirikata::Logging::error, Sirikata::Logging::insane, JSLOG, Sirikata::Liveness::livenessToken(), Sirikata::JS::JSTimerLivenessHolder::lt, mCtx, noReference(), Sirikata::JS::JSCtx::objStrand, and TIMER_JSTIMER_TEMPLATE_FIELD_COUNT.
Referenced by Sirikata::JS::EmersonScript::create_timeout().
bool Sirikata::JS::JSTimerStruct::amExecuting [private] |
Means that we're currently processing the callback of a timer.
Should be used to prevent invalid memory access if try to clear a timer from within its own callback. (Ie, won't delete the timer if amExecuting is true.)
Referenced by clear(), and iEvaluateCallback().
v8::Persistent<v8::Function> Sirikata::JS::JSTimerStruct::cb |
Referenced by clear(), iEvaluateCallback(), and struct_getAllData().
Referenced by iEvaluateCallback().
Referenced by clear(), fixSuspendableToContext(), iEvaluateCallback(), noReference(), and struct_getAllData().
bool Sirikata::JS::JSTimerStruct::killAfterFire [private] |
Need to know when to clean up timer struct.
Have made Emerson objects that are timers weak. This means that as soon as the objects holding them go out of scope, we receive a callback. If the timer has not yet fired, we do not want to delete JSTimerStruct. Therefore, we wait until the timer has fired before freeing the memory associated with this timer struct. Setting killAfterFire to true instructs this struct to clear and free itself after it fires and executes its callback.
Referenced by iEvaluateCallback(), and noReference().
JSCtx* Sirikata::JS::JSTimerStruct::mCtx [private] |
Referenced by clear(), evaluateCallback(), iEvaluateCallback(), noReference(), and timerWeakReferenceCleanup().
Referenced by clear(), fixSuspendableToContext(), JSTimerStruct(), resume(), struct_getAllData(), struct_resetTimer(), and suspend().
v8::Persistent<v8::Object> Sirikata::JS::JSTimerStruct::mPersistentHandle [private] |
On clear, should set mPersistentHandle's internal fields to NULL.
That way. This accounts for the case where a user explicitly called clear on a timer, which deletes the JSTimerStruct pointer and then the v8 garbage collector tries to delete the internal field of mPersistentHandle.
Referenced by clear(), and setPersistentObject().
Referenced by fixSuspendableToContext().
bool Sirikata::JS::JSTimerStruct::noTimerWaiting [private] |
True if have no callback waiting to be executed.
False otherwise.
Referenced by fixSuspendableToContext(), iEvaluateCallback(), JSTimerStruct(), noReference(), resume(), struct_resetTimer(), and suspend().
Referenced by fixSuspendableToContext(), JSTimerStruct(), resume(), and struct_getAllData().