xref: /haiku/src/build/libroot/misc.cpp (revision cfb5f18de4e9368dc4c7ecd79c89f9387873e8fc)
1338b8dc3SIngo Weinhold 
2677aaae1SIngo Weinhold #include <BeOSBuildCompatibility.h>
3338b8dc3SIngo Weinhold 
4338b8dc3SIngo Weinhold #include <errno.h>
5338b8dc3SIngo Weinhold #include <stdio.h>
6338b8dc3SIngo Weinhold #include <stdlib.h>
7338b8dc3SIngo Weinhold #include <unistd.h>
8338b8dc3SIngo Weinhold #include <sys/stat.h>
9338b8dc3SIngo Weinhold #include <sys/time.h>
10338b8dc3SIngo Weinhold 
1187c9daaeSIngo Weinhold #include <Debug.h>
12338b8dc3SIngo Weinhold #include <image.h>
13338b8dc3SIngo Weinhold #include <OS.h>
14338b8dc3SIngo Weinhold 
15338b8dc3SIngo Weinhold mode_t __gUmask = 022;
16338b8dc3SIngo Weinhold 
17338b8dc3SIngo Weinhold // debugger
18338b8dc3SIngo Weinhold void
19338b8dc3SIngo Weinhold debugger(const char *message)
20338b8dc3SIngo Weinhold {
21338b8dc3SIngo Weinhold 	fprintf(stderr, "debugger() called: %s\n", message);
22338b8dc3SIngo Weinhold 	exit(1);
23338b8dc3SIngo Weinhold }
24338b8dc3SIngo Weinhold 
2587c9daaeSIngo Weinhold // _debuggerAssert
2687c9daaeSIngo Weinhold int
27*cfb5f18dSRene Gollent _debuggerAssert(const char *file, int line, const char *expression)
2887c9daaeSIngo Weinhold {
2987c9daaeSIngo Weinhold 	char buffer[2048];
3087c9daaeSIngo Weinhold 	snprintf(buffer, sizeof(buffer), "%s:%d: %s\n", file, line, expression);
3187c9daaeSIngo Weinhold 	debugger(buffer);
3287c9daaeSIngo Weinhold 	return 0;
3387c9daaeSIngo Weinhold }
3487c9daaeSIngo Weinhold 
35338b8dc3SIngo Weinhold // system_time
36338b8dc3SIngo Weinhold bigtime_t
37338b8dc3SIngo Weinhold system_time(void)
38338b8dc3SIngo Weinhold {
39338b8dc3SIngo Weinhold 	struct timeval tm;
40338b8dc3SIngo Weinhold 	gettimeofday(&tm, NULL);
41338b8dc3SIngo Weinhold 	return (int64)tm.tv_sec * 1000000LL + (int64)tm.tv_usec;
42338b8dc3SIngo Weinhold }
43338b8dc3SIngo Weinhold 
44338b8dc3SIngo Weinhold // snooze
45338b8dc3SIngo Weinhold status_t
46338b8dc3SIngo Weinhold snooze(bigtime_t amount)
47338b8dc3SIngo Weinhold {
48338b8dc3SIngo Weinhold 	if (amount <= 0)
49338b8dc3SIngo Weinhold 		return B_OK;
50338b8dc3SIngo Weinhold 
51338b8dc3SIngo Weinhold 	int64 secs = amount / 1000000LL;
52338b8dc3SIngo Weinhold 	int64 usecs = amount % 1000000LL;
53338b8dc3SIngo Weinhold 	if (secs > 0) {
54338b8dc3SIngo Weinhold 		if (sleep((unsigned)secs) < 0)
55338b8dc3SIngo Weinhold 			return errno;
56338b8dc3SIngo Weinhold 	}
57338b8dc3SIngo Weinhold 
58338b8dc3SIngo Weinhold 	if (usecs > 0) {
59338b8dc3SIngo Weinhold 		if (usleep((useconds_t)usecs) < 0)
60338b8dc3SIngo Weinhold 			return errno;
61338b8dc3SIngo Weinhold 	}
62338b8dc3SIngo Weinhold 
63338b8dc3SIngo Weinhold 	return B_OK;
64338b8dc3SIngo Weinhold }
65338b8dc3SIngo Weinhold 
66338b8dc3SIngo Weinhold // snooze_until
67338b8dc3SIngo Weinhold status_t
68338b8dc3SIngo Weinhold snooze_until(bigtime_t time, int timeBase)
69338b8dc3SIngo Weinhold {
70338b8dc3SIngo Weinhold 	return snooze(time - system_time());
71338b8dc3SIngo Weinhold }
72