xref: /haiku/src/build/libroot/misc.cpp (revision 77c8afb47f226476057898672cde7331d4a2e20a)
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
debugger(const char * message)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
_debuggerAssert(const char * file,int line,const char * expression)27cfb5f18dSRene 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 
35*77c8afb4SJessica Hamilton #ifndef HAIKU_HOST_PLATFORM_HAIKU
36338b8dc3SIngo Weinhold // system_time
37338b8dc3SIngo Weinhold bigtime_t
system_time(void)38338b8dc3SIngo Weinhold system_time(void)
39338b8dc3SIngo Weinhold {
40338b8dc3SIngo Weinhold 	struct timeval tm;
41338b8dc3SIngo Weinhold 	gettimeofday(&tm, NULL);
42338b8dc3SIngo Weinhold 	return (int64)tm.tv_sec * 1000000LL + (int64)tm.tv_usec;
43338b8dc3SIngo Weinhold }
44*77c8afb4SJessica Hamilton #endif
45338b8dc3SIngo Weinhold 
46338b8dc3SIngo Weinhold // snooze
47338b8dc3SIngo Weinhold status_t
snooze(bigtime_t amount)48338b8dc3SIngo Weinhold snooze(bigtime_t amount)
49338b8dc3SIngo Weinhold {
50338b8dc3SIngo Weinhold 	if (amount <= 0)
51338b8dc3SIngo Weinhold 		return B_OK;
52338b8dc3SIngo Weinhold 
53338b8dc3SIngo Weinhold 	int64 secs = amount / 1000000LL;
54338b8dc3SIngo Weinhold 	int64 usecs = amount % 1000000LL;
55338b8dc3SIngo Weinhold 	if (secs > 0) {
56338b8dc3SIngo Weinhold 		if (sleep((unsigned)secs) < 0)
57338b8dc3SIngo Weinhold 			return errno;
58338b8dc3SIngo Weinhold 	}
59338b8dc3SIngo Weinhold 
60338b8dc3SIngo Weinhold 	if (usecs > 0) {
61338b8dc3SIngo Weinhold 		if (usleep((useconds_t)usecs) < 0)
62338b8dc3SIngo Weinhold 			return errno;
63338b8dc3SIngo Weinhold 	}
64338b8dc3SIngo Weinhold 
65338b8dc3SIngo Weinhold 	return B_OK;
66338b8dc3SIngo Weinhold }
67338b8dc3SIngo Weinhold 
68338b8dc3SIngo Weinhold // snooze_until
69338b8dc3SIngo Weinhold status_t
snooze_until(bigtime_t time,int timeBase)70338b8dc3SIngo Weinhold snooze_until(bigtime_t time, int timeBase)
71338b8dc3SIngo Weinhold {
72338b8dc3SIngo Weinhold 	return snooze(time - system_time());
73338b8dc3SIngo Weinhold }
74