diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/common.c | 48 | ||||
-rw-r--r-- | lib/common.h | 28 |
2 files changed, 76 insertions, 0 deletions
diff --git a/lib/common.c b/lib/common.c index 413b9f5..cf68ffa 100644 --- a/lib/common.c +++ b/lib/common.c @@ -1919,3 +1919,51 @@ int urlUnescape(char* dst, const char* src, int normalize) return (dst - org_dst) - 1; } + +//*************************************************************************** +//*************************************************************************** +// Timer Thread +//*************************************************************************** + +cTimerThread::cTimerThread(sendEventFct fct, int aEvent, time_t aTime, void* aUserData, bool aSelfDistroy) +{ + sendEvent = fct; + event = aEvent; + theTime = aTime; + userData = aUserData; + selfdetroy = aSelfDistroy; + active = no; + + Start(); +} + +//*************************************************************************** +// Action +//*************************************************************************** + +void cTimerThread::Action() +{ + cMutex mutex; + + active = yes; + + mutex.Lock(); + tell(1, "Info: Started timer thread, event (%d) scheduled for '%s'", event, l2pTime(theTime).c_str()); + + while (time(0) < theTime && Running() && active) + { + // loop every 10 seconds + + waitCondition.TimedWait(mutex, (theTime - time(0)) * 1000); + } + + if (time(0) >= theTime && sendEvent) + sendEvent(event, userData); + + tell(3, "Info: Finished timer thread"); + + active = no; + + // if (selfdetroy) + // delete this; // :o :o ;) +} diff --git a/lib/common.h b/lib/common.h index 28536ec..3d0aa95 100644 --- a/lib/common.h +++ b/lib/common.h @@ -414,6 +414,34 @@ class LogDuration }; //*************************************************************************** +// Timer Thread +//*************************************************************************** + +class cTimerThread : public cThread +{ + public: + + typedef void (*sendEventFct)(int event, void* userData); + + cTimerThread(sendEventFct fct, int aEvent, time_t aTime, void* aUserData = 0, bool aSelfDistroy = no); + + int isActive() { return active; } + + protected: + + virtual void Action(); + + int event; + time_t theTime; + void* userData; + bool selfdetroy; + cCondVar waitCondition; + int active; + + sendEventFct sendEvent; +}; + +//*************************************************************************** // Semaphore //*************************************************************************** |