687 |
|
static void *xpram_func(void *arg) |
688 |
|
{ |
689 |
|
while (!xpram_thread_cancel) { |
690 |
< |
for (int i=0; i<60 && !xpram_thread_cancel; i++) { |
691 |
< |
#ifdef HAVE_NANOSLEEP |
692 |
< |
struct timespec req = {1, 0}; |
693 |
< |
nanosleep(&req, NULL); |
694 |
< |
#else |
695 |
< |
usleep(1000000); |
696 |
< |
#endif |
697 |
< |
} |
690 |
> |
for (int i=0; i<60 && !xpram_thread_cancel; i++) |
691 |
> |
Delay_usec(1000000); |
692 |
|
xpram_watchdog(); |
693 |
|
} |
694 |
|
return NULL; |
740 |
|
#ifdef HAVE_PTHREADS |
741 |
|
static void *tick_func(void *arg) |
742 |
|
{ |
743 |
+ |
uint64 next = GetTicks_usec(); |
744 |
|
while (!tick_thread_cancel) { |
750 |
– |
|
751 |
– |
// Wait |
752 |
– |
#ifdef HAVE_NANOSLEEP |
753 |
– |
struct timespec req = {0, 16625000}; |
754 |
– |
nanosleep(&req, NULL); |
755 |
– |
#else |
756 |
– |
usleep(16625); |
757 |
– |
#endif |
758 |
– |
|
759 |
– |
// Action |
745 |
|
one_tick(); |
746 |
+ |
next += 16625; |
747 |
+ |
int64 delay = next - GetTicks_usec(); |
748 |
+ |
if (delay > 0) |
749 |
+ |
Delay_usec(delay); |
750 |
+ |
else if (delay < -16625) |
751 |
+ |
next = GetTicks_usec(); |
752 |
|
} |
753 |
|
return NULL; |
754 |
|
} |
755 |
|
#endif |
756 |
|
|
757 |
|
|
758 |
+ |
/* |
759 |
+ |
* Get current value of microsecond timer |
760 |
+ |
*/ |
761 |
+ |
|
762 |
+ |
uint64 GetTicks_usec(void) |
763 |
+ |
{ |
764 |
+ |
#ifdef HAVE_CLOCK_GETTIME |
765 |
+ |
struct timespec t; |
766 |
+ |
clock_gettime(CLOCK_REALTIME, &t); |
767 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
768 |
+ |
#else |
769 |
+ |
struct timeval t; |
770 |
+ |
gettimeofday(&t, NULL); |
771 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_usec; |
772 |
+ |
#endif |
773 |
+ |
} |
774 |
+ |
|
775 |
+ |
|
776 |
+ |
/* |
777 |
+ |
* Delay by specified number of microseconds (<1 second) |
778 |
+ |
* (adapted from SDL_Delay() source) |
779 |
+ |
*/ |
780 |
+ |
|
781 |
+ |
void Delay_usec(uint32 usec) |
782 |
+ |
{ |
783 |
+ |
int was_error; |
784 |
+ |
#ifndef __linux__ // Non-Linux implementations need to calculate time left |
785 |
+ |
uint64 then, now, elapsed; |
786 |
+ |
#endif |
787 |
+ |
struct timeval tv; |
788 |
+ |
|
789 |
+ |
// Set the timeout interval - Linux only needs to do this once |
790 |
+ |
#ifdef __linux__ |
791 |
+ |
tv.tv_sec = 0; |
792 |
+ |
tv.tv_usec = usec; |
793 |
+ |
#else |
794 |
+ |
then = GetTicks_usec(); |
795 |
+ |
#endif |
796 |
+ |
do { |
797 |
+ |
errno = 0; |
798 |
+ |
#ifndef __linux__ |
799 |
+ |
/* Calculate the time interval left (in case of interrupt) */ |
800 |
+ |
now = GetTicks_usec(); |
801 |
+ |
elapsed = now - then; |
802 |
+ |
then = now; |
803 |
+ |
if (elapsed >= usec) |
804 |
+ |
break; |
805 |
+ |
usec -= elapsed; |
806 |
+ |
tv.tv_sec = 0; |
807 |
+ |
tv.tv_usec = usec; |
808 |
+ |
#endif |
809 |
+ |
was_error = select(0, NULL, NULL, NULL, &tv); |
810 |
+ |
} while (was_error && (errno == EINTR)); |
811 |
+ |
} |
812 |
+ |
|
813 |
+ |
|
814 |
|
#if !EMULATED_68K |
815 |
|
/* |
816 |
|
* Virtual 68k interrupt handler |