xref: /haiku/headers/private/libroot/time_private.h (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
1 /*
2  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _LIBROOT_TIME_PRIVATE_H
6 #define _LIBROOT_TIME_PRIVATE_H
7 
8 
9 #include <errno.h>
10 #include <sys/cdefs.h>
11 #include <sys/time.h>
12 #include <time.h>
13 
14 #include <SupportDefs.h>
15 
16 #include <new>
17 
18 #define CLOCKS_PER_SEC_BEOS					1000
19 #define CLK_TCK_BEOS						CLOCKS_PER_SEC_BEOS
20 
21 #define MICROSECONDS_PER_CLOCK_TICK			(1000000 / CLOCKS_PER_SEC)
22 #define MICROSECONDS_PER_CLOCK_TICK_BEOS	(1000000 / CLOCKS_PER_SEC_BEOS)
23 
24 
25 struct __timer_t {
26 	int32		id;
27 	thread_id	thread;
28 
29 	void SetTo(int32 id, thread_id thread)
30 	{
31 		this->id = id;
32 		this->thread = thread;
33 	}
34 };
35 
36 
37 static inline void
38 bigtime_to_timespec(bigtime_t time, timespec& spec)
39 {
40 	spec.tv_sec = time / 1000000;
41 	spec.tv_nsec = (time % 1000000) * 1000;
42 }
43 
44 
45 static inline bool
46 timespec_to_bigtime(const timespec& spec, bigtime_t& _time)
47 {
48 	if (spec.tv_sec < 0 || spec.tv_nsec < 0 || spec.tv_nsec >= 1000000000)
49 		return false;
50 
51 	_time = (bigtime_t)spec.tv_sec * 1000000 + (spec.tv_nsec + 999) / 1000;
52 
53 	return true;
54 }
55 
56 
57 static inline bool
58 timeval_to_timespec(const timeval& val, timespec& spec)
59 {
60 	if (val.tv_sec < 0 || val.tv_usec < 0 || val.tv_usec >= 1000000)
61 		return false;
62 
63 	spec.tv_sec = val.tv_sec;
64 	spec.tv_nsec = val.tv_usec * 1000;
65 
66 	return true;
67 }
68 
69 
70 static inline bool
71 timeval_to_bigtime(const timeval& val, bigtime_t& _time)
72 {
73 	 timespec spec;
74 	 return timeval_to_timespec(val, spec) && timespec_to_bigtime(spec, _time);
75 }
76 
77 
78 static inline void
79 timespec_to_timeval(const timespec& spec, timeval& val)
80 {
81 	val.tv_sec = spec.tv_sec;
82 	val.tv_usec = spec.tv_nsec / 1000;
83 }
84 
85 
86 __BEGIN_DECLS
87 
88 
89 clock_t	__clock_beos(void);
90 clock_t	__clock(void);
91 
92 
93 __END_DECLS
94 
95 
96 #endif	// _LIBROOT_TIME_PRIVATE_H
97