860 |
|
|
861 |
|
/* |
862 |
|
* Delay by specified number of microseconds (<1 second) |
863 |
< |
* (adapted from SDL_Delay() source) |
863 |
> |
* (adapted from SDL_Delay() source; this function is designed to provide |
864 |
> |
* the highest accuracy possible) |
865 |
|
*/ |
866 |
|
|
867 |
|
void Delay_usec(uint32 usec) |
868 |
|
{ |
869 |
|
int was_error; |
870 |
< |
#ifndef __linux__ // Non-Linux implementations need to calculate time left |
870 |
> |
|
871 |
> |
#if defined(linux) |
872 |
> |
struct timeval tv; |
873 |
> |
#elif defined(__FreeBSD__) || defined(sgi) |
874 |
> |
struct timespec elapsed, tv; |
875 |
> |
#else // Non-Linux implementations need to calculate time left |
876 |
|
uint64 then, now, elapsed; |
877 |
|
#endif |
872 |
– |
struct timeval tv; |
878 |
|
|
879 |
|
// Set the timeout interval - Linux only needs to do this once |
880 |
< |
#ifdef __linux__ |
880 |
> |
#if defined(linux) |
881 |
|
tv.tv_sec = 0; |
882 |
|
tv.tv_usec = usec; |
883 |
+ |
#elif defined(__FreeBSD__) |
884 |
+ |
elapsed.tv_sec = 0; |
885 |
+ |
elapsed.tv_nsec = usec * 1000; |
886 |
|
#else |
887 |
|
then = GetTicks_usec(); |
888 |
|
#endif |
889 |
|
do { |
890 |
|
errno = 0; |
891 |
< |
#ifndef __linux__ |
891 |
> |
#if !defined(linux) && !defined(__FreeBSD__) && !defined(sgi) |
892 |
|
/* Calculate the time interval left (in case of interrupt) */ |
893 |
|
now = GetTicks_usec(); |
894 |
|
elapsed = now - then; |
899 |
|
tv.tv_sec = 0; |
900 |
|
tv.tv_usec = usec; |
901 |
|
#endif |
902 |
+ |
#if defined(__FreeBSD__) || defined(sgi) |
903 |
+ |
tv.tv_sec = elapsed.tv_sec; |
904 |
+ |
tv.tv_nsec = elapsed.tv_nsec; |
905 |
+ |
was_error = nanosleep(&tv, &elapsed); |
906 |
+ |
#else |
907 |
|
was_error = select(0, NULL, NULL, NULL, &tv); |
908 |
+ |
#endif |
909 |
|
} while (was_error && (errno == EINTR)); |
910 |
|
} |
911 |
|
|