File: time.c

package info (click to toggle)
overkill 0.16-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,964 kB
  • ctags: 1,245
  • sloc: ansic: 11,650; makefile: 193; sh: 39
file content (75 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (4)
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
#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;
#if defined(WIN32)
	Sleep(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)
{
#if defined(WIN32)
	Sleep(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
}