Sirikata
|
A timer which handles events using an IOService. More...
#include <IOTimer.hpp>
Classes | |
class | TimedOut |
Public Member Functions | |
~IOTimer () | |
void | setCallback (const IOCallback &cb) |
Set the callback which will be used by this timer. | |
void | wait (const Duration &waitFor) |
Wait for the specified duration, then call the previously set callback. | |
void | wait (const Duration &waitFor, const IOCallback &cb) |
Wait for the specified duration, then call the previously set callback. | |
void | cancel () |
Cancel the current timer. | |
Duration | expiresFromNow () |
Static Public Member Functions | |
static IOTimerPtr | create (IOService *io) |
Create a new timer, serviced by the specified IOService. | |
static IOTimerPtr | create (IOService &io) |
static IOTimerPtr | create (IOStrand *ios) |
Create a new timer, serviced by the specified IOStrand. | |
static IOTimerPtr | create (IOStrand &ios) |
static IOTimerPtr | create (IOService *io, const IOCallback &cb) |
Create a new timer, serviced by the specified IOService. | |
static IOTimerPtr | create (IOService &io, const IOCallback &cb) |
static IOTimerPtr | create (IOStrand *ios, const IOCallback &cb) |
Create a new timer, serviced by the specified IOStrand. | |
static IOTimerPtr | create (IOStrand &ios, const IOCallback &cb) |
Private Member Functions | |
IOTimer (IOService &io) | |
Create a new timer, serviced by the specified IOService. | |
IOTimer (IOService &io, const IOCallback &cb) | |
Create a new timer, serviced by the specified IOService. | |
IOTimer (IOStrand *ios) | |
Create a new timer, serviced within the given IOStrand. | |
IOTimer (IOStrand *ios, const IOCallback &cb) | |
Create a new timer, serviced by the specified IOStrand. | |
Private Attributes | |
DeadlineTimer * | mTimer |
IOStrand * | mStrand |
IOCallback | mFunc |
SerializationCheck | chk |
AtomicValue< uint32 > | mCanceled |
Since the cancel does not always stop the boost callback this adds a level of safety to prevent the callback: This value gets incremented when call cancel. |
A timer which handles events using an IOService.
The user specifies a timeout and a callback and that callback is triggered after at least the specified duration has passed. Repeated, periodic callbacks are supported by specifying a callback up front which will be called at each timeout.
Because this works with the multithreaded event loop you should be careful of how use use IOTimers: the handler may be queued to run or running on a different thread, meaning it can actually run *after* you cancel it. If you need single-threaded-style semantics, provide an IOStrand for the IOTimer to use and *only operate on it from within that strand*. When you do this, you don't have to deal with wrapping your own callbacks: they are guaranteed to be invoked from the strand you pass in.
Note: Instances of this class should not be stored directly, instead they must be stored using a shared_ptr<IOTimer> (which is available as IOTimerPtr). In order to enforce this, you cannot allocate one directly -- instead you must use the static IOTimer::create() methods.
Sirikata::Network::IOTimer::IOTimer | ( | IOService & | io | ) | [private] |
Sirikata::Network::IOTimer::IOTimer | ( | IOService & | io, |
const IOCallback & | cb | ||
) | [private] |
Create a new timer, serviced by the specified IOService.
io | the IOService to service this timers events |
cb | the handler for this timer's events. |
References setCallback().
Sirikata::Network::IOTimer::IOTimer | ( | IOStrand * | ios | ) | [private] |
Sirikata::Network::IOTimer::IOTimer | ( | IOStrand * | ios, |
const IOCallback & | cb | ||
) | [private] |
Create a new timer, serviced by the specified IOStrand.
ios | the IOStrand to service this timer's events |
cb | the handler for this timer's events. |
References setCallback().
Sirikata::Network::IOTimer::~IOTimer | ( | ) |
void Sirikata::Network::IOTimer::cancel | ( | ) |
Cancel the current timer.
This will cancel the callback that would have resulted when the timer expired.
References Sirikata::Network::DeadlineTimer::cancel(), chk, mCanceled, mStrand, mTimer, Sirikata::SerializationCheck::serializedEnter(), and Sirikata::SerializationCheck::serializedExit().
Referenced by ~IOTimer().
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOService & | io | ) | [static] |
References IOTimer().
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOService & | io, |
const IOCallback & | cb | ||
) | [static] |
References IOTimer().
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOStrand & | ios | ) | [static] |
References IOTimer().
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOStrand & | ios, |
const IOCallback & | cb | ||
) | [static] |
References IOTimer().
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOService * | io, |
const IOCallback & | cb | ||
) | [static] |
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOService * | io | ) | [static] |
Create a new timer, serviced by the specified IOService.
io | the IOService to service this timers events |
References IOTimer().
Referenced by Sirikata::TCPSpaceNetwork::handleConnectedStream(), Sirikata::Graphics::ProxyEntity::ProxyEntity(), Sirikata::OH::SQLiteStorage::start(), Sirikata::OH::CassandraStorage::start(), and Sirikata::Context::startForceQuitTimer().
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOStrand * | ios, |
const IOCallback & | cb | ||
) | [static] |
IOTimerPtr Sirikata::Network::IOTimer::create | ( | IOStrand * | ios | ) | [static] |
void Sirikata::Network::IOTimer::setCallback | ( | const IOCallback & | cb | ) |
void Sirikata::Network::IOTimer::wait | ( | const Duration & | waitFor, |
const IOCallback & | cb | ||
) |
Wait for the specified duration, then call the previously set callback.
waitFor | the amount of time to wait. |
cb | the callback to set for this, and future, timeout events. |
References setCallback(), and wait().
void Sirikata::Network::IOTimer::wait | ( | const Duration & | waitFor | ) |
Wait for the specified duration, then call the previously set callback.
waitFor | the amount of time to wait. |
References Sirikata::Logging::error, mCanceled, mStrand, mTimer, Sirikata::AtomicValue< T >::read(), Sirikata::Network::IOTimer::TimedOut::timedOut(), and Sirikata::Network::IOStrand::wrap().
Referenced by wait().
Referenced by cancel(), and ~IOTimer().
AtomicValue<uint32> Sirikata::Network::IOTimer::mCanceled [private] |
Since the cancel does not always stop the boost callback this adds a level of safety to prevent the callback: This value gets incremented when call cancel.
When creating callback, bind it with the value of callbackToken when at this point. When executing callback compare the bound value passed through with current value of callbackToken. If tokens aren't equal, programmer called cancel in intervening time.
Note that this is only guaranteed effective if you provide a strand to work with. Otherwise it just reduces the likelihood of executing the callback after the cancel is finished (or of executing it *while* executing the cancel, in separate threads).
Referenced by cancel(), Sirikata::Network::IOTimer::TimedOut::timedOut(), and wait().
IOCallback Sirikata::Network::IOTimer::mFunc [private] |
Referenced by setCallback(), and Sirikata::Network::IOTimer::TimedOut::timedOut().
IOStrand* Sirikata::Network::IOTimer::mStrand [private] |
Referenced by cancel(), wait(), and ~IOTimer().
DeadlineTimer* Sirikata::Network::IOTimer::mTimer [private] |
Referenced by cancel(), expiresFromNow(), wait(), and ~IOTimer().