cumbia 1.x
general purpose multi threaded library
|
The CuTimerService class provides access to cumbia timers. It is normally used internally by the library. More...
#include <cutimerservice.h>
Public Member Functions | |
CuTimerService () | |
Class constructor Initializes timerMaxCount to 25. More... | |
~CuTimerService () | |
CuTimer * | registerListener (CuTimerListener *timer_listener, int timeout, CuEventLoopService *loop_service=nullptr, const std::string &tmrname=std::string()) |
Register a new CuTimerListener with a given timeout. Reuse existing timers if possible. Listeners shall be notified in the event loop thread if the method is called with a non null reference to CuEventLoopService, in the CuTimer's thread otherwise. More... | |
void | unregisterListener (CuTimerListener *th, int timeout) |
Unregister a listener with the given timeout from the service. More... | |
CuTimer * | changeTimeout (CuTimerListener *th, int from_timeo, int to_timeo) |
Changes the timeout of the timer that serves the given listener. More... | |
void | restart (CuTimer *t) |
restart the timer t with interval millis More... | |
void | start (CuTimer *t) |
start the timer More... | |
std::string | getName () const |
Returns the name of the service. More... | |
CuServices::Type | getType () const |
CuTimerService::getType returns the service type. More... | |
Public Member Functions inherited from CuServiceI | |
virtual | ~CuServiceI () |
virtual std::string | getName () const =0 |
return the name of the service More... | |
virtual CuServices::Type | getType () const =0 |
return the type of the service More... | |
The CuTimerService class provides access to cumbia timers. It is normally used internally by the library.
CuTimerService implements CuServiceI in order to provide access to CuTimer objects. They are not directly available to clients. Use registerListener to either get a timer. Timers are shared across all listeners registered with the same timeout. unregisterListener is called when a timer is no more needed. CuTimer is single shot: CuTimerService::restart shall be called to restart the timer. It is possible to get the list of CuTimerListener for a given timer and the list of all timers.
CuTimerService::CuTimerService | ( | ) |
Class constructor Initializes timerMaxCount to 25.
References CuTimerServicePrivate::timer_max_count, and CuTimerServicePrivate::tmrcnt.
CuTimerService::~CuTimerService | ( | ) |
References CuTimerServicePrivate::ti_cache, and CuTimerServicePrivate::ti_map.
CuTimer * CuTimerService::changeTimeout | ( | CuTimerListener * | tl, |
int | from_timeo, | ||
int | to_timeo | ||
) |
Changes the timeout of the timer that serves the given listener.
tl | the CuTimerListener requiring a change in timeout |
timeout | the new timeout, in milliseconds |
References perr, registerListener(), CuTimerServicePrivate::shmu, CuTimer::timeout(), and unregisterListener().
|
virtual |
|
virtual |
CuTimerService::getType returns the service type.
Implements CuServiceI.
References CuServices::Timer.
CuTimer * CuTimerService::registerListener | ( | CuTimerListener * | tl, |
int | timeout, | ||
CuEventLoopService * | loo_s = nullptr , |
||
const std::string & | tmrname = std::string() |
||
) |
Register a new CuTimerListener with a given timeout. Reuse existing timers if possible. Listeners shall be notified in the event loop thread if the method is called with a non null reference to CuEventLoopService, in the CuTimer's thread otherwise.
timer_listener | a CuTimerListener (e.g. CuThread) |
timeout | the desired timeout |
loop_service | if specified (not null) then the timeout callback is executed in the same thread as the event loop thread (usually, the main thread). |
tmrname | an optional name for the timer to be created. If the timer is reused, passing a non empty tmrname will overwrite the timer name. |
A timer with the same timeout is reused and the listener and loop service are added to the instance.
References CuTimerPrivate::m_id, CuTimerPrivate::m_name, pgreen, CuTimer::setTimeout(), CuTimerServicePrivate::shmu, CuTimerServicePrivate::ti_cache, CuTimerServicePrivate::ti_map, CuTimerServicePrivate::timer_max_count, and CuTimerServicePrivate::tmrcnt.
Referenced by changeTimeout().
void CuTimerService::restart | ( | CuTimer * | t | ) |
restart the timer t with interval millis
t | the timer to restart |
millis | the desired interval |
The timer is rescheduled if a timeout is already pending
void CuTimerService::start | ( | CuTimer * | t | ) |
start the timer
t | which timer |
millis | timeout in milliseconds |
if the timer is pending, nothing is done. Timer is started otherwise.
void CuTimerService::unregisterListener | ( | CuTimerListener * | tl, |
int | timeout | ||
) |
Unregister a listener with the given timeout from the service.
tl | the CuTimerListner to unregister |
timeout | the to remove listener's timeout |
If the timer associated to tl has no more listeners after unregistration, it will be stopped and deleted.
References pfatal, and CuTimerServicePrivate::shmu.
Referenced by changeTimeout().