
#include <errno.h>
#include <time.h>
#include <Spectre/System/System.h>

namespace sp {

unsigned long system::getMilliseconds()
{
	struct timespec tv;
	clock_gettime(CLOCK_REALTIME, &tv);
	return (tv.tv_sec * 1000ul) + (tv.tv_nsec / 1000000ul);

}

void system::sleep(int milliseconds)
{
	struct timespec req, rem;
	req.tv_sec = milliseconds / 1000ul;
	req.tv_nsec = (milliseconds - (req.tv_sec * 1000ul)) * 1000000ul;

_start:
	// Try again if we get interrupted.
	if (clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem) == EINTR) {
		// Update req with remaining time.
		req = rem;
		goto _start;
	}
}

} // namespace sp
