-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
56 lines (47 loc) · 1.52 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* File : timer.c
*
* Author : Eleftheriadis Charalampos
*
* Date : 23 July 2020
*/
#include "timer.h"
void timerInit(Timer *timer, int period, int tasksToExecute, void *(*stopFcn)(void *arg), void *(*timerFcn)(void *arg),
void *(*errorFcn)(), Queue *queue, void *(*producer)(void *arg), int *tJobIn, int *tDrift,
pthread_mutex_t *tMut) {
timer->period = period;
timer->tasksToExecute = tasksToExecute;
timer->startDelay = 0;
timer->startFcn = NULL;
timer->stopFcn = stopFcn;
timer->timerFcn = timerFcn;
timer->errorFcn = errorFcn;
timer->userData = NULL;
timer->queue = queue;
timer->producer = producer;
timer->tJobIn = tJobIn;
timer->tDrift = tDrift;
timer->tMut = tMut;
}
void start(Timer *timer) {
pthread_create(&timer->tid, NULL, timer->producer, timer);
}
void startat(Timer *timer, int year, int month, int day, int hour, int minute, int second) {
int delay = 0;
// Gets current time.
time_t now = time(NULL);
// Creates a time struct of the desired time to start the timer.
struct tm startAt;
startAt.tm_year = year;
startAt.tm_mon = month;
startAt.tm_mday = day;
startAt.tm_hour = hour;
startAt.tm_min = minute;
startAt.tm_sec = second;
// Calculates difference of the desired time to start the timer and the current time.
delay = (int) difftime(now, mktime(&startAt));
if (delay<0)
delay = 0;
timer->startDelay = delay;
pthread_create(&timer->tid, NULL, timer->producer, timer);
}