xref: /haiku/headers/private/libroot/time_private.h (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
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 <OS.h>
15 
16 
17 #define CLOCKS_PER_SEC_BEOS					1000
18 #define CLK_TCK_BEOS						CLOCKS_PER_SEC_BEOS
19 
20 #define MICROSECONDS_PER_CLOCK_TICK			(1000000 / CLOCKS_PER_SEC)
21 #define MICROSECONDS_PER_CLOCK_TICK_BEOS	(1000000 / CLOCKS_PER_SEC_BEOS)
22 
23 
24 struct __timer_t {
25 	int32		id;
26 	thread_id	thread;
27 
28 	void SetTo(int32 id, thread_id thread)
29 	{
30 		this->id = id;
31 		this->thread = thread;
32 	}
33 };
34 
35 
36 static inline void
37 bigtime_to_timespec(bigtime_t time, timespec& spec)
38 {
39 	spec.tv_sec = time / 1000000;
40 	spec.tv_nsec = (time % 1000000) * 1000;
41 }
42 
43 
44 static inline bool
45 timespec_to_bigtime(const timespec& spec, bigtime_t& _time)
46 {
47 	if (spec.tv_sec < 0 || spec.tv_nsec < 0 || spec.tv_nsec >= 1000000000)
48 		return false;
49 
50 	_time = (bigtime_t)spec.tv_sec * 1000000 + (spec.tv_nsec + 999) / 1000;
51 
52 	return true;
53 }
54 
55 
56 static inline bool
57 timeval_to_timespec(const timeval& val, timespec& spec)
58 {
59 	if (val.tv_sec < 0 || val.tv_usec < 0 || val.tv_usec >= 1000000)
60 		return false;
61 
62 	spec.tv_sec = val.tv_sec;
63 	spec.tv_nsec = val.tv_usec * 1000;
64 
65 	return true;
66 }
67 
68 
69 static inline bool
70 timeval_to_bigtime(const timeval& val, bigtime_t& _time)
71 {
72 	 timespec spec;
73 	 return timeval_to_timespec(val, spec) && timespec_to_bigtime(spec, _time);
74 }
75 
76 
77 static inline void
78 timespec_to_timeval(const timespec& spec, timeval& val)
79 {
80 	val.tv_sec = spec.tv_sec;
81 	val.tv_usec = spec.tv_nsec / 1000;
82 }
83 
84 
85 __BEGIN_DECLS
86 
87 
88 clock_t	__clock_beos(void);
89 clock_t	__clock(void);
90 
91 
92 __END_DECLS
93 
94 
95 #endif	// _LIBROOT_TIME_PRIVATE_H
96