]> git.tdb.fi Git - libs/core.git/blob - source/time/unix/utils.cpp
Move non-oneliner functions out of RefPtr class declaration
[libs/core.git] / source / time / unix / utils.cpp
1 #include <sys/resource.h>
2 #include <sys/time.h>
3 #include <cerrno>
4 #include <msp/core/systemerror.h>
5 #include "rawtime_private.h"
6 #include "timedelta.h"
7 #include "timestamp.h"
8 #include "utils.h"
9
10 namespace Msp {
11 namespace Time {
12
13 TimeStamp now()
14 {
15         timeval tv;
16         gettimeofday(&tv, 0);
17         return TimeStamp(timeval_to_rawtime(tv));
18 }
19
20 TimeDelta get_cpu_time()
21 {
22         rusage ru;
23         getrusage(RUSAGE_SELF, &ru);
24         return (ru.ru_utime.tv_sec+ru.ru_stime.tv_sec)*sec + (ru.ru_utime.tv_usec+ru.ru_stime.tv_usec)*usec;
25 }
26
27 void sleep(const TimeDelta &d)
28 {
29         timespec ts = rawtime_to_timespec(d.raw());
30         while(nanosleep(&ts, 0)==-1)
31                 if(errno!=EINTR)
32                         throw system_error("nanosleep");
33 }
34
35 } // namespace Time
36 } // namespace Msp