|
44 | 44 | #include "sb_util.h"
|
45 | 45 | #include "ck_spinlock.h"
|
46 | 46 |
|
| 47 | +#define NS_PER_SEC 1000000000 |
| 48 | +#define US_PER_SEC 1000000 |
| 49 | +#define MS_PER_SEC 1000 |
| 50 | +#define NS_PER_MS (NS_PER_SEC / MS_PER_SEC) |
| 51 | + |
47 | 52 | /* Convert nanoseconds to seconds and vice versa */
|
48 |
| -#define NS2SEC(nsec) ((nsec)/1000000000.) |
49 |
| -#define SEC2NS(sec) ((uint64_t)(sec) * 1000000000) |
| 53 | +#define NS2SEC(nsec) ((nsec) / (double) NS_PER_SEC) |
| 54 | +#define SEC2NS(sec) ((uint64_t) (sec) * NS_PER_SEC) |
50 | 55 |
|
51 | 56 | /* Convert nanoseconds to milliseconds and vice versa */
|
52 |
| -#define NS2MS(nsec) ((nsec)/1000000.) |
53 |
| -#define MS2NS(sec) ((sec)*1000000ULL) |
| 57 | +#define NS2MS(nsec) ((nsec) / (double) NS_PER_MS) |
| 58 | +#define MS2NS(sec) ((sec) * (uint64_t) NS_PER_MS) |
54 | 59 |
|
55 | 60 | /* Convert milliseconds to seconds and vice versa */
|
56 |
| -#define MS2SEC(msec) ((msec)/1000.) |
57 |
| -#define SEC2MS(sec) ((sec)*1000) |
| 61 | +#define MS2SEC(msec) ((msec) / (double) MS_PER_SEC) |
| 62 | +#define SEC2MS(sec) ((sec) * MS_PER_SEC) |
58 | 63 |
|
59 | 64 | /* Difference between two 'timespec' values in nanoseconds */
|
60 | 65 | #define TIMESPEC_DIFF(a,b) (SEC2NS(a.tv_sec - b.tv_sec) + \
|
@@ -95,6 +100,12 @@ typedef struct
|
95 | 100 | } sb_timer_t;
|
96 | 101 |
|
97 | 102 |
|
| 103 | +static inline int sb_nanosleep(uint64_t ns) |
| 104 | +{ |
| 105 | + struct timespec ts = { ns / NS_PER_SEC, ns % NS_PER_SEC }; |
| 106 | + return nanosleep(&ts, NULL); |
| 107 | +} |
| 108 | + |
98 | 109 | /* timer control functions */
|
99 | 110 |
|
100 | 111 | /* Initialize timer */
|
|
0 commit comments