-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_tools.c
47 lines (37 loc) · 1.05 KB
/
common_tools.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
// Copyright 2022 <Maros Varchola - mvarchdev>
#include "common_tools.h"
#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
/// @brief Will return random integer based on min max definitions
/// @param min Minimum number that should return
/// @param max Maximum number that should return
/// @return Random number based on min max definition
unsigned int rand_gen(int min, int max) {
return min + (rand() % ((max + 1) - min));
}
/// @brief Sleep for the requested number of milliseconds
/// @param msec how long it should block
/// @return Error code
int msleep(long msec) {
struct timespec ts;
int res;
if (msec < 0) {
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
/// @brief Get actual time in ms
/// @return Actual time in miliseconds
long long timeInMilliseconds() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (((long long)tv.tv_sec) * 1000) + (tv.tv_usec / 1000);
}