1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| void
g_usleep (gulong microseconds)
{
#ifdef G_OS_WIN32
Sleep (microseconds / 1000);
#else /* !G_OS_WIN32 */
# ifdef HAVE_NANOSLEEP
struct timespec request, remaining;
request.tv_sec = microseconds / G_USEC_PER_SEC;
request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
request = remaining;
# else /* !HAVE_NANOSLEEP */
if (g_thread_supported ())
{
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
static GCond* cond = NULL;
GTimeVal end_time;
g_get_current_time (&end_time);
if (microseconds > G_MAXLONG)
{
microseconds -= G_MAXLONG;
g_time_val_add (&end_time, G_MAXLONG);
}
g_time_val_add (&end_time, microseconds);
g_static_mutex_lock (&mutex);
if (!cond)
cond = g_cond_new ();
while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
&end_time))
/* do nothing */;
g_static_mutex_unlock (&mutex);
}
else
{
struct timeval tv;
tv.tv_sec = microseconds / G_USEC_PER_SEC;
tv.tv_usec = microseconds % G_USEC_PER_SEC;
select(0, NULL, NULL, NULL, &tv);
}
# endif /* !HAVE_NANOSLEEP */
#endif /* !G_OS_WIN32 */
} |
Partager