forked from hackndev/0verkill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.c
75 lines (63 loc) · 1.14 KB
/
time.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifdef __EMX__
#include <stdlib.h>
#endif
#ifndef WIN32
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#else
#include <time.h>
#endif
#include <string.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include "cfg.h"
/* gets current time in microseconds */
unsigned long_long get_time(void)
{
#ifndef WIN32
struct timeval tv;
struct timezone tz;
memset(&tz,0,sizeof(tz));
gettimeofday(&tv,&tz);
return ((unsigned long_long)tv.tv_sec)*1000000+(unsigned long_long)tv.tv_usec;
#else
return ((unsigned long_long)GetTickCount())*1000UL;
#endif
}
/* waits until time t passes */
void sleep_until(unsigned long_long t)
{
unsigned long_long u=get_time();
if (u>=t) return;
t-=u;
#ifdef WIN32
Sleep((DWORD)(t/1000));
#elif defined(__EMX__)
_sleep2(t/1000);
#else
{
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=t;
select(0,NULL,NULL,NULL,&tv);
}
#endif
}
/* waits time t microseconds */
void my_sleep(unsigned long_long t)
{
#ifdef WIN32
Sleep((DWORD)(t/1000));
#elif defined(__EMX__)
_sleep2(t/1000);
#else
{
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=t;
select(0,NULL,NULL,NULL,&tv);
}
#endif
}