Пример #1
0
void std_fuse::wait_out() {
	waitTick = clock();

	int stime = fuseLength - ((waitTick - startTick) / (CLOCKS_PER_MS));

	if ( stime >= 1000) {
		SDL_Delay(stime);
		return;
	}

	if ( stime < 0 ) return;
    std_sleep(stime);
}
Пример #2
0
/* The implementation is not exactly "waitByTime" since the mac os
 * doesn't support sem_timedWait currently */
extern int std_take_proc_sem_by_time(std_proc_sem_id_t semId, int timeout)
{
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
    struct timespec tm;
    struct timeval  tv;
    struct timezone tz;

    gettimeofday(&tv, &tz);
    tm.tv_sec = tv.tv_sec + timeout / 1000;
    tm.tv_nsec = tv.tv_usec * 1000 + (timeout % 1000) * 1000000;
    if (tm.tv_nsec >= 1000000000)
    {
        tm.tv_nsec -= 1000000000;
        tm.tv_sec++;
    }

    do
    {
        if (sem_timedwait((sem_t *) semId, &tm) == 0)
            return 1;

        switch (std_get_error())
        {
        case EINTR:
            /* Try again */
            break;

        default:
        case ETIMEDOUT:
            return 0;
        }
    } while (1);
#else

#warning sem_timedwait is not supported, use sem_trywait + sleep

    std_tick_t tick = std_get_current_tick();
    for (;;)
    {
        int period = (int) (std_get_current_tick() - tick);
        if (period < 0 || period >= timeout)
            // timeout
            return 0;

        if (sem_trywait((sem_t *) semId) == 0)
            return 1;

        std_sleep(1);
    }
#endif
}
Пример #3
0
extern int std_get_mutex_by_time(std_mutex_id_t mutexId, int timeout)
{
    int toTick;
    struct timespec tm;
    struct timeval  tv;
    struct timezone tz;
    int   rc;

    if (mutexId == STD_NO_MUTEX)
        /* Return failed for operating empty object */
        return 0;

    if (timeout == STD_WAIT_FOREVER)
        /* Wait forever */
        return std_get_mutex(mutexId);

    gettimeofday(&tv, &tz);
    tm.tv_sec = tv.tv_sec + timeout / 1000;
    tm.tv_nsec = tv.tv_usec * 1000 + (timeout % 1000) * 1000000;
    if (tm.tv_nsec >= 1000000000)
    {
        tm.tv_nsec -= 1000000000;
        tm.tv_sec++;
    }

    toTick = std_get_current_tick() + timeout;
    for (;;)
    {
#if 1
        if ((rc = pthread_mutex_trylock(mutexId)) == 0)
            return 1;

        std_sleep(1);
        if (std_get_current_tick() >= toTick)
            return 0;
#else
        if ((rc = pthread_mutex_timedlock(mutexId, &tm)) == 0)
            /* Got mutex */
            return 1;

        if (rc != EINTR)
            return 0;
#endif
    }
}