summaryrefslogtreecommitdiff
path: root/src/base/myTimer.c
blob: 2fbee7b69e22abd3ae37ff131e89caf3fdeb3db7 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

#include <time.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>

#include "main.h"
#include "list.h"
#include "myTimer.h"

#ifndef PUBLIC_SERVER
#include "interface.h"
#endif
#ifdef __WIN32__
#include <windows.h>
#include <time.h>
#endif
/* // uncoment when having issues with gettimeofday in Windows
#ifdef __WIN32__
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL

struct timezone 
{
  int  tz_minuteswest;
  int  tz_dsttime;
};
 
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
	FILETIME ft;
	unsigned __int64 tmpres = 0;
	static int tzflag;
	if (NULL != tv) {
		GetSystemTimeAsFileTime(&ft);
	    tmpres |= ft.dwHighDateTime;
	    tmpres <<= 32;
	    tmpres |= ft.dwLowDateTime;
	    tmpres /= 10;
	    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
	    tv->tv_sec = (long)(tmpres / 1000000UL);
	    tv->tv_usec = (long)(tmpres % 1000000UL);
	}
	if (NULL != tz) {
	    if (!tzflag) {
	      _tzset();
	      tzflag++;
	    }
	    tz->tz_minuteswest = _timezone / 60;
	    tz->tz_dsttime = _daylight;
	}
	return 0;
}
#endif
*/
static struct timeval start = { .tv_sec = 0, .tv_usec = 0 };

list_t* newTimer()
{
	return newList();
}

void restartTimer()
{
	gettimeofday(&start, NULL);
}


my_time_t getMyTime()
{
	struct timeval now;
	my_time_t ticks;

	if(  start.tv_sec == 0 && start.tv_usec == 0 )
	{
		restartTimer();
	}

	gettimeofday(&now, NULL);

	ticks = (now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;

	//printf("-> %d\n", ticks);
	return ticks;
}

my_time_t getMyTimeMicro()
{
	static struct timeval start = { .tv_sec = 0, .tv_usec = 0 };
	struct timeval now;
	my_time_t ticks;

	if(  start.tv_sec == 0 && start.tv_usec == 0 )
	{
		gettimeofday(&start, NULL);
	}

	gettimeofday(&now, NULL);

	ticks = (now.tv_sec-start.tv_sec)*1000*1000+(now.tv_usec-start.tv_usec);

	//printf("-> %d\n", ticks);
	return ticks;
}

my_timer_t* newTimerItem(int type, void (*fce)(void *p), void *arg, my_time_t my_time)
{
	static int new_id = 0;
	my_timer_t *new;
	
	assert( fce != NULL );

	new = malloc( sizeof(my_timer_t) );
	memset(new, 0, sizeof(my_timer_t));

	new->id = new_id++;
	new->type = type;
	new->fce = fce;
	new->arg = arg;

	new->createTime = getMyTime();
	new->time = my_time;

	return new;
}

static void destroyTimerItem(my_timer_t *p)
{
	assert( p != NULL );
	free(p);
}

int addTaskToTimer(list_t *listTimer, int type, void (*fce)(void *p), void *arg, my_time_t my_time)
{
	my_timer_t *new;

	new = newTimerItem(type, fce, arg, my_time);
	addList(listTimer, new);

	return new->id;
}

void eventTimer(list_t *listTimer)
{
	int i;
	my_timer_t *thisTimer;
	my_time_t currentTime;

 	currentTime = getMyTime();

	for( i = 0 ; i < listTimer->count ; i++ )
	{
		thisTimer = (my_timer_t *)listTimer->list[i];
		assert( thisTimer != NULL );

		switch( thisTimer->type )
		{
			case TIMER_ONE:
				if( currentTime >= thisTimer->createTime + thisTimer->time )
				{
					thisTimer->fce(thisTimer->arg);
		
					delListItem(listTimer, i, free);
					i--;
				}
			break;
			case TIMER_PERIODIC:
				if( currentTime >= thisTimer->createTime + thisTimer->time )
				{
					thisTimer->fce(thisTimer->arg);
					thisTimer->createTime = getMyTime();
				}
			break;
			default :
				assert( ! "bad timer type !" );
			break;
		}

	}
}

void delTimer(list_t *listTimer, int id)
{
	int i;
	my_timer_t *thisTimer;

	for( i = 0 ; i < listTimer->count ; i++ )
	{
		thisTimer = (my_timer_t *)listTimer->list[i];
		assert( thisTimer != NULL );

		if( thisTimer->id == id )
		{
			delListItem(listTimer, i, free);
			return;
		}
	}

	assert( ! "Uloha s id nenajdena !" );
}


void destroyTimer(list_t *listTimer)
{
	destroyListItem(listTimer, destroyTimerItem);
}