[Tuxnes-devel] usleep() and gettimeofday() aren't portable
Brought to you by:
tmmm
From: Jason D. S. <jd...@us...> - 2004-05-11 06:00:59
|
The usleep() function is not portable. It's not available on win32; we should use Sleep() instead. Freeciv has code like /*************************************************************** Suspend execution for the specified number of microseconds. ***************************************************************/ void myusleep(unsigned long usec) { #ifdef HAVE_USLEEP usleep(usec); #else #ifdef HAVE_SNOOZE /* BeOS */ snooze(usec); #else #ifdef GENERATING_MAC EventRecord the_event; /* dummy - always be a null event */ usec /= 16666; /* microseconds to 1/60th seconds */ if (usec < 1) usec = 1; /* suposed to give other application processor time for the mac */ WaitNextEvent(0, &the_event, usec, 0L); #else #ifdef WIN32_NATIVE Sleep(usec / 1000); #else struct timeval tv; tv.tv_sec=0; tv.tv_usec=usec; select(0, NULL, NULL, NULL, &tv); #endif #endif #endif #endif } where HAVE_USLEEP, HAVE_SNOOZE, GENERATING_MAC, and WIN32_NATIVE are all checked at configure time. There's a similar problem with gettimeofday. ftime is more portable and is reported to have 10ms resolution on win32 (barely adequate for tuxnes purposes). So there is code like #ifdef HAVE_GETTIMEOFDAY int ret = gettimeofday(&t->start.tv, NULL); if (ret == -1) { report_gettimeofday_failed(t); return; } #elif defined HAVE_FTIME ftime(&t->start.tp); #else t->start.t = time(NULL); if (t->start.t == (time_t) -1) { report_time_failed(t); return; } #endif I think it would be convenient to have wrappers that take times in seconds, as float or double values. Internally these can convert to whatever integer form is needed by the backend. Incidentally there is no good way to check for Sleep and other win32-api functions because linking is done differently. I have no idea why this is but it does seem to be the case. The solution is to check for the win32 api in one go and define WIN32_NATIVE or WIN32_API or some such. Then this value can be used to cover all win32-specific function calls (which will be consistent across all windows platforms). jason |