Sirikata
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
Sirikata::Network::IOTimer Class Reference

A timer which handles events using an IOService. More...

#include <IOTimer.hpp>

Inheritance diagram for Sirikata::Network::IOTimer:
Collaboration diagram for Sirikata::Network::IOTimer:

List of all members.

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

DeadlineTimermTimer
IOStrandmStrand
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.

Detailed Description

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.


Constructor & Destructor Documentation

Sirikata::Network::IOTimer::IOTimer ( IOService io) [private]

Create a new timer, serviced by the specified IOService.

Parameters:
iothe IOService to service this timers events

Referenced by create().

Sirikata::Network::IOTimer::IOTimer ( IOService io,
const IOCallback cb 
) [private]

Create a new timer, serviced by the specified IOService.

Parameters:
iothe IOService to service this timers events
cbthe handler for this timer's events.

References setCallback().

Sirikata::Network::IOTimer::IOTimer ( IOStrand ios) [private]

Create a new timer, serviced within the given IOStrand.

Parameters:
iosthe IOStrand to service this timer's events
Sirikata::Network::IOTimer::IOTimer ( IOStrand ios,
const IOCallback cb 
) [private]

Create a new timer, serviced by the specified IOStrand.

Parameters:
iosthe IOStrand to service this timer's events
cbthe handler for this timer's events.

References setCallback().

Sirikata::Network::IOTimer::~IOTimer ( )

Member Function Documentation

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]

Create a new timer, serviced by the specified IOService.

Parameters:
iothe IOService to service this timers events
cbthe handler for this timer's events.

References IOTimer().

IOTimerPtr Sirikata::Network::IOTimer::create ( IOService io) [static]
IOTimerPtr Sirikata::Network::IOTimer::create ( IOStrand ios,
const IOCallback cb 
) [static]

Create a new timer, serviced by the specified IOStrand.

Parameters:
iothe IOStrand to service this timers events
cbthe handler for this timer's events.

References IOTimer().

IOTimerPtr Sirikata::Network::IOTimer::create ( IOStrand ios) [static]

Create a new timer, serviced by the specified IOStrand.

Parameters:
iosthe IOStrand to service this timers events

References IOTimer().

Duration Sirikata::Network::IOTimer::expiresFromNow ( )

References mTimer.

void Sirikata::Network::IOTimer::setCallback ( const IOCallback cb)

Set the callback which will be used by this timer.

Note that this sets the callback regardless of the current state of the timer, and will be used for timeouts currently in progress.

References mFunc.

Referenced by IOTimer(), and wait().

void Sirikata::Network::IOTimer::wait ( const Duration waitFor,
const IOCallback cb 
)

Wait for the specified duration, then call the previously set callback.

Parameters:
waitForthe amount of time to wait.
cbthe 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.

Parameters:
waitForthe 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().


Member Data Documentation

Referenced by cancel(), and ~IOTimer().

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().

Referenced by cancel(), wait(), and ~IOTimer().

Referenced by cancel(), expiresFromNow(), wait(), and ~IOTimer().


The documentation for this class was generated from the following files: