xref: /haiku/src/build/libroot/misc.cpp (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
1 
2 #include <BeOSBuildCompatibility.h>
3 
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <sys/time.h>
10 
11 #include <Debug.h>
12 #include <image.h>
13 #include <OS.h>
14 
15 mode_t __gUmask = 022;
16 
17 // debugger
18 void
19 debugger(const char *message)
20 {
21 	fprintf(stderr, "debugger() called: %s\n", message);
22 	exit(1);
23 }
24 
25 // _debuggerAssert
26 int
27 _debuggerAssert(const char *file, int line, const char *expression)
28 {
29 	char buffer[2048];
30 	snprintf(buffer, sizeof(buffer), "%s:%d: %s\n", file, line, expression);
31 	debugger(buffer);
32 	return 0;
33 }
34 
35 // system_time
36 bigtime_t
37 system_time(void)
38 {
39 	struct timeval tm;
40 	gettimeofday(&tm, NULL);
41 	return (int64)tm.tv_sec * 1000000LL + (int64)tm.tv_usec;
42 }
43 
44 // snooze
45 status_t
46 snooze(bigtime_t amount)
47 {
48 	if (amount <= 0)
49 		return B_OK;
50 
51 	int64 secs = amount / 1000000LL;
52 	int64 usecs = amount % 1000000LL;
53 	if (secs > 0) {
54 		if (sleep((unsigned)secs) < 0)
55 			return errno;
56 	}
57 
58 	if (usecs > 0) {
59 		if (usleep((useconds_t)usecs) < 0)
60 			return errno;
61 	}
62 
63 	return B_OK;
64 }
65 
66 // snooze_until
67 status_t
68 snooze_until(bigtime_t time, int timeBase)
69 {
70 	return snooze(time - system_time());
71 }
72