ptimedsem.cxx

来自「PTypes是一个扩充了多线程和网络功能的STL库」· CXX 代码 · 共 130 行

CXX
130
字号
/* * *  C++ Portable Types Library (PTypes) *  Version 2.0.2  Released 17-May-2004 * *  Copyright (C) 2001-2004 Hovik Melikyan * *  http://www.melikyan.com/ptypes/ * */#ifdef WIN32#  include <windows.h>#else#  include <sys/time.h>#  include <pthread.h>#  include <errno.h>#endif#include "pasync.h"PTYPES_BEGINstatic void tsem_fail(){    fatal(CRIT_FIRST + 41, "Timed semaphore failed");}#ifdef WIN32timedsem::timedsem(int initvalue){    handle = CreateSemaphore(nil, initvalue, 65535, nil);    if (handle == 0)        tsem_fail();}timedsem::~timedsem() {    CloseHandle(handle);}bool timedsem::wait(int timeout){    uint r = WaitForSingleObject(handle, timeout);    if (r == WAIT_FAILED)        tsem_fail();    return r != WAIT_TIMEOUT;}void timedsem::post(){    if (ReleaseSemaphore(handle, 1, nil) == 0)        tsem_fail();}#elseinline void tsem_syscheck(int r){    if (r != 0)        tsem_fail();}timedsem::timedsem(int initvalue)    : unknown(), count(initvalue){    tsem_syscheck(pthread_mutex_init(&mtx, 0));    tsem_syscheck(pthread_cond_init(&cond, 0));}timedsem::~timedsem(){    pthread_cond_destroy(&cond);    pthread_mutex_destroy(&mtx);}bool timedsem::wait(int timeout){    pthread_mutex_lock(&mtx);    while (count <= 0) {         if (timeout >= 0)        {            timespec abs_ts;             timeval cur_tv;            gettimeofday(&cur_tv, NULL);            abs_ts.tv_sec = cur_tv.tv_sec + timeout / 1000;             abs_ts.tv_nsec = cur_tv.tv_usec * 1000                + (timeout % 1000) * 1000000;            int rc = pthread_cond_timedwait(&cond, &mtx, &abs_ts);            if (rc == ETIMEDOUT) {                 pthread_mutex_unlock(&mtx);                return false;            }        }        else            pthread_cond_wait(&cond, &mtx);    }     count--;    pthread_mutex_unlock(&mtx);    return true;} void timedsem::post(){    pthread_mutex_lock(&mtx);    count++;     pthread_cond_signal(&cond);    pthread_mutex_unlock(&mtx);}#endifPTYPES_END

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?