Open Lighting Architecture
0.9.4
|
A single threaded I/O event management system.
SelectServer is the core of the event driven system. It's responsible for invoking Callbacks when certain events occur.
The SelectServer has a number of different implementations depending on the platform. On systems with epoll, the flag –use-epoll will use epoll() rather than select(). The PollerInterface defines the contract between the SelectServer and the lower level, platform dependant Poller classes.
All methods except Execute() and Terminate() must be called from the thread that Run() was called in.
Public Member Functions | |
SelectServer (ola::ExportMap *export_map=NULL, Clock *clock=NULL) | |
Create a new SelectServer. | |
~SelectServer () | |
Clean up. | |
bool | IsRunning () const |
Checks if the SelectServer is running. | |
const TimeStamp * | WakeUpTime () const |
The time when this SelectServer was woken up. | |
void | Terminate () |
Exit from the Run() loop. | |
void | SetDefaultInterval (const TimeInterval &block_interval) |
Set the duration to block for. | |
void | Run () |
Enter the event loop. | |
void | RunOnce () |
Do a single pass through the event loop. Does not block. | |
void | RunOnce (const TimeInterval &block_interval) |
Do a single pass through the event loop. | |
bool | AddReadDescriptor (ReadFileDescriptor *descriptor) |
Register a ReadFileDescriptor for read-events. | |
bool | AddReadDescriptor (ConnectedDescriptor *descriptor, bool delete_on_close=false) |
Register a ConnectedDescriptor for read-events. | |
void | RemoveReadDescriptor (ReadFileDescriptor *descriptor) |
Remove a RemoveReadDescriptor for read-events. | |
void | RemoveReadDescriptor (ConnectedDescriptor *descriptor) |
Remove a ConnectedDescriptor for read-events. | |
bool | AddWriteDescriptor (WriteFileDescriptor *descriptor) |
Register a WriteFileDescriptor for write-events. | |
void | RemoveWriteDescriptor (WriteFileDescriptor *descriptor) |
Remove a WriteFileDescriptor for write-events. | |
ola::thread::timeout_id | RegisterRepeatingTimeout (unsigned int ms, ola::Callback0< bool > *callback) |
Execute a callback periodically. | |
ola::thread::timeout_id | RegisterRepeatingTimeout (const ola::TimeInterval &interval, ola::Callback0< bool > *callback) |
Execute a callback periodically. | |
ola::thread::timeout_id | RegisterSingleTimeout (unsigned int ms, ola::SingleUseCallback0< void > *callback) |
Execute a callback after a certain time interval. | |
ola::thread::timeout_id | RegisterSingleTimeout (const ola::TimeInterval &interval, ola::SingleUseCallback0< void > *callback) |
Execute a callback after a certain time interval. | |
void | RemoveTimeout (ola::thread::timeout_id id) |
Cancel an existing timeout. | |
void | RunInLoop (ola::Callback0< void > *callback) |
Execute a callback on every event loop. | |
void | Execute (ola::BaseCallback0< void > *callback) |
Execute the supplied callback at some point in the future. | |
void | DrainCallbacks () |
Run all callbacks until there are none left. |
Friends | |
class | ::SelectServerTest |
ola::io::SelectServer::SelectServer | ( | ola::ExportMap * | export_map = NULL , |
Clock * | clock = NULL |
||
) |
Create a new SelectServer.
ola::io::SelectServer::~SelectServer | ( | ) |
Clean up.
The SelectServer should be terminated before it's deleted.
|
virtual |
Register a ReadFileDescriptor for read-events.
descriptor | the ReadFileDescriptor to add. |
When the descriptor is ready for reading, PerformRead() will be called.
Implements ola::io::SelectServerInterface.
|
virtual |
Register a ConnectedDescriptor for read-events.
descriptor | the ConnectedDescriptor to add. |
delete_on_close | if true, ownership of the ConnectedDescriptor is transferred to the SelectServer. |
When the descriptor is ready for reading, PerformRead() will be called. Prior to PerformRead(), IsClosed() is called. If this returns true, and delete_on_close was set, the descriptor will be deleted.
Implements ola::io::SelectServerInterface.
|
virtual |
Register a WriteFileDescriptor for write-events.
descriptor | the WriteFileDescriptor to add. |
When the descriptor is writeable, PerformWrite() is called.
Implements ola::io::SelectServerInterface.
|
virtual |
Execute the supplied callback at some point in the future.
callback | the callback to run. |
This method provides the following guarantees:
When queuing callbacks, you ether need to ensure that either:
To achieve the latter it's common to keep track of the number of outstanding callbacks and then call DrainCallbacks() in the destructor if the number of outstanding callbacks is non-0.
Implements ola::thread::ExecutorInterface.
|
inline |
Checks if the SelectServer is running.
|
virtual |
Execute a callback periodically.
period | the number of milliseconds between each execution of the callback. |
callback | the callback to run. Ownership is transferred. |
Returning false from the callback will cause it to be cancelled.
Implements ola::io::SelectServerInterface.
|
virtual |
Execute a callback periodically.
period | the time interval between each execution of the callback. |
callback | the callback to run. Ownership is transferred. |
Returning false from the callback will cause it to be cancelled.
Implements ola::io::SelectServerInterface.
|
virtual |
Execute a callback after a certain time interval.
delay | the number of milliseconds before the callback is executed. |
callback | the callback to run. Ownership is transferred. |
Implements ola::io::SelectServerInterface.
|
virtual |
Execute a callback after a certain time interval.
delay | the time interval to wait before the callback is executed. |
callback | the callback to run. Ownership is transferred. |
Implements ola::io::SelectServerInterface.
|
virtual |
Remove a RemoveReadDescriptor for read-events.
descriptor | the descriptor to remove. |
Implements ola::io::SelectServerInterface.
|
virtual |
Remove a ConnectedDescriptor for read-events.
descriptor | the descriptor to remove. |
Implements ola::io::SelectServerInterface.
|
virtual |
Cancel an existing timeout.
id | the timeout_id returned by a call to RegisterRepeatingTimeout or RegisterSingleTimeout. |
Implements ola::io::SelectServerInterface.
|
virtual |
Remove a WriteFileDescriptor for write-events.
descriptor | the descriptor to remove. |
Implements ola::io::SelectServerInterface.
void ola::io::SelectServer::Run | ( | ) |
Enter the event loop.
Run() will return once Terminate() has been called.
void ola::io::SelectServer::RunInLoop | ( | ola::Callback0< void > * | callback | ) |
Execute a callback on every event loop.
callback | the Callback to execute. Ownership is transferrred to the SelectServer. |
Be very cautious about using this, it's almost certainly not what you want.
There is no way to remove a Callback added with this method.
void ola::io::SelectServer::RunOnce | ( | const TimeInterval & | block_interval | ) |
Do a single pass through the event loop.
block_interval | The maximum time to block if there is no I/O or timer events. |
void ola::io::SelectServer::SetDefaultInterval | ( | const TimeInterval & | block_interval | ) |
Set the duration to block for.
block_interval | the interval to block for. |
This controls the upper bound on the duration between callbacks added with RunInLoop().
void ola::io::SelectServer::Terminate | ( | ) |
Exit from the Run() loop.
Terminate() may be called from any thread.
|
virtual |
The time when this SelectServer was woken up.
If running within the same thread as the SelectServer, this is a efficient way to get the current time.
Implements ola::io::SelectServerInterface.