49#define INVALID_EVENT_ID (-10 & 1 << 29)
92 assert(timer != NULL);
114 assert(timer !=
nullptr);
130 time_t currentTime = 0;
131 time_t nextEventTime = 0;
135 assert(timer !=
nullptr);
137 pthread_mutex_lock(&timer->
mutex);
144 pthread_mutex_unlock(&timer->
mutex);
152 pthread_mutex_unlock(&timer->
mutex);
156 nextEventTime = nextEvent->eventTime;
158 currentTime = time(NULL);
160 if (nextEvent && currentTime >= nextEventTime) {
161 if (nextEvent->persistent) {
164 if (nextEvent->job.arg != NULL &&
165 nextEvent->job.free_func != NULL) {
166 nextEvent->job.free_func(nextEvent->job.arg);
171 if (nextEvent->job.arg != NULL &&
172 nextEvent->job.free_func != NULL) {
173 nextEvent->job.free_func(nextEvent->job.arg);
182 timeToWait.tv_nsec = 0;
183 timeToWait.tv_sec = (long)nextEvent->eventTime;
205 assert(timeout !=
nullptr);
227 assert(timer != NULL);
230 if ((timer == NULL) || (tp == NULL)) {
234 rc += pthread_mutex_init(&timer->
mutex, NULL);
238 rc += pthread_mutex_lock(&timer->
mutex);
241 rc += pthread_cond_init(&timer->
condition, NULL);
264 pthread_mutex_unlock(&timer->
mutex);
268 pthread_mutex_destroy(&timer->
mutex);
283 TimerEvent* temp = NULL;
284 TimerEvent* newEvent = NULL;
286 assert(timer != NULL);
289 if ((timer == NULL) || (job == NULL)) {
294 pthread_mutex_lock(&timer->
mutex);
304 if (newEvent == NULL) {
305 pthread_mutex_unlock(&timer->
mutex);
312 while (tempNode != NULL) {
313 temp = (TimerEvent*)tempNode->item;
314 if (temp->eventTime >= timeout) {
334 pthread_mutex_unlock(&timer->
mutex);
342 TimerEvent* temp = NULL;
344 assert(timer != NULL);
350 pthread_mutex_lock(&timer->
mutex);
354 while (tempNode != NULL) {
355 temp = (TimerEvent*)tempNode->item;
356 if (temp->id ==
id) {
368 pthread_mutex_unlock(&timer->
mutex);
376 assert(timer != NULL);
382 pthread_mutex_lock(&timer->
mutex);
388 while (tempNode != NULL) {
389 TimerEvent* temp = (TimerEvent*)tempNode->item;
393 if (temp->job.free_func) {
394 temp->job.free_func(temp->job.arg);
397 tempNode = tempNode2;
403 pthread_cond_broadcast(&timer->
condition);
409 pthread_mutex_unlock(&timer->
mutex);
412 while (pthread_cond_destroy(&timer->
condition) != 0) {
415 while (pthread_mutex_destroy(&timer->
mutex) != 0) {
int FreeListInit(FreeList *free_list, size_t elementSize, int maxFreeListLength)
Initializes Free List.
int FreeListFree(FreeList *free_list, void *element)
Returns an item to the Free List.
void * FreeListAlloc(FreeList *free_list)
Allocates chunk of set size.
int FreeListDestroy(FreeList *free_list)
Releases the resources stored with the free list.
int ListDestroy(LinkedList *list, int freeItem)
Removes all memory associated with list nodes. Does not free LinkedList *list.
void * ListDelNode(LinkedList *list, ListNode *dnode, int freeItem)
Removes a node from the list. The memory for the node is freed.
ListNode * ListAddBefore(LinkedList *list, void *item, ListNode *anode)
Adds a node before the specified node. Node gets added immediately before anode.
ListNode * ListNext(LinkedList *list, ListNode *node)
Returns the next item in the list.
ListNode * ListHead(LinkedList *list)
Returns the head of the list.
ListNode * ListAddTail(LinkedList *list, void *item)
Adds a node to the tail of the list. Node gets added immediately before list.tail.
int ListInit(LinkedList *list, cmp_routine cmp_func, free_function free_func)
Initializes LinkedList. Must be called first and only once for List.
#define EOUTOFMEM
Error condition for "out of memory".
Linked list node. Stores generic item and pointers to next and prev.
int ThreadPoolAdd(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a job to the thread pool.
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the priority of the threadpool job.
int TPJobInit(ThreadPoolJob *job, UPnPsdk::start_routine func, void *arg)
Initializes thread pool job.
int ThreadPoolAddPersistent(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a persistent job to the thread pool.
#define INVALID_EVENT_ID
Invalid event ID.
int TimerThreadShutdown(TimerThread *timer)
Shutdown the timer thread.
int TimerThreadInit(TimerThread *timer, ThreadPool *tp)
Initializes and starts timer thread.
int TimerThreadSchedule(TimerThread *timer, time_t timeout, TimeoutType type, ThreadPoolJob *job, Duration duration, int *id)
Schedules an event to run at a specified time.
int TimerThreadRemove(TimerThread *timer, int id, ThreadPoolJob *out)
Removes an event from the timer Q.
Manage threads that start at a given time (for internal use only).
pthread_mutex_t mutex
[in]
pthread_cond_t condition
[in]
TimeoutType
Timeout Types.
@ ABS_SEC
seconds from Jan 1, 1970.
A timer thread that allows the scheduling of a job to run at a specified time in the future.
void TimerThreadWorker(void *arg)
Implements timer thread.
TimerEvent * CreateTimerEvent(TimerThread *timer, ThreadPoolJob *job, Duration persistent, time_t eventTime, int id)
Creates a Timer Event.
void FreeTimerEvent(TimerThread *timer, TimerEvent *event)
Deallocates a dynamically allocated TimerEvent.
int CalculateEventTime(time_t *timeout, TimeoutType type)
Calculates the appropriate timeout in absolute seconds since Jan 1, 1970.
Structure to contain information for a timer event.