Linux sem_trywait实现sem_timedwait

  • sem_timedwait说明

          int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);

          其中第2个参数 struct timespec *abs_timeout要求为绝对时间,如果手动将系统时间往后修改会导致sem_timedwait长时间阻塞

  • sem_trywait说明

         int sem_trywait(sem_t *sem);

         sem_trywait与 sem_wait() 类似,若信号不可用,则返回错误(errno 设置为EAGAIN)而不是阻塞

  • sem_timedwait自定义实现
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <semaphore.h>
#include <errno.h>


unsigned long long GetClockTimeMS( void )
{
    unsigned long long time;
    struct timespec curTime;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curTime);
    time = curTime.tv_sec;
    time *= 1000;
    time += curTime.tv_nsec/1000000;
    return time;
}

unsigned long long GetClockTimeUS( void )
{
    unsigned long long time;
    struct timespec curTime;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curTime);
    time = curTime.tv_sec;
    time *= 1000000;
    time += curTime.tv_nsec/1000;
    return time;
}

unsigned long long GetClockTimeNS(void)
{
    unsigned long long time;
    struct timespec curTime;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curTime);
    time = curTime.tv_sec;
    time *= 1000000000;
    time += curTime.tv_nsec;
    return time;
}

int SleepEx(int ms)
{
    struct timeval timeout;

    timeout.tv_sec = ms/1000;
    timeout.tv_usec = (ms%1000)*1000;
    if(-1 == select(0, NULL, NULL, NULL, &timeout))
    {
        return errno;
    }
    return 0;
}

int WaitEvent(sem_t *hEvent, unsigned int milliseconds)
{
    int ret;
#if 0
        struct timespec timeout = { 0 };
#ifdef _DEBUG
        ret = clock_gettime(CLOCK_REALTIME, &timeout);
#else
        clock_gettime(CLOCK_REALTIME, &timeout);
#endif//_DEBUG
        timeout.tv_sec += (milliseconds / 1000);
        timeout.tv_nsec += ((milliseconds % 1000) * 1000000);
        if (0 == sem_timedwait((sem_t *)hEvent, &timeout))
        {
            return 0;
        }
        return errno;
#else
        unsigned long long timeout = milliseconds;//微秒
        unsigned long long beg = GetClockTimeMS();
        do 
        {
            if (0 == sem_trywait((sem_t *)hEvent))
            {
                return 0;
            }
            ret = errno;
            if (EAGAIN != ret)
            {
                return ret;
            }
            if (GetClockTimeMS() - beg >= timeout)
            {
                break;
            }
            SleepEx(1);
        } while (1);
        return ETIMEDOUT;
#endif
}

int main(void)
{
    sem_t event;
    sem_init(&event, 0, 0);
    unsigned long long begMS = GetClockTimeMS();
    WaitEvent(&event, 2000);
    printf("end %llu..\n", GetClockTimeMS() - begMS);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值