linux createevent 信号量实现
event.h
#ifndef EVENT_H
#define EVENT_H
#include <fcntl.h>
#include <atomic>
#include <time.h>
#include <semaphore.h>
#include <string>
using std::string;
using std::atomic_bool;
class CEvent
{
private:
CEvent(long lpEventAttributes, bool bManualReset, bool bInitialState, const char* lpName);
~CEvent();
bool Init();
timespec sem_get_time_millsecs(long msecs);
public:
enum
{
WAIT_OBJECT_0 = 0,
WAIT_TIMEOUT = 0x00000102L,
WAIT_FAILED = 0xFFFFFFFF
};
//S_IRWXU
static CEvent* CreateEvent(long lpEventAttributes, bool bManualReset, bool bInitialState, const char* lpName);
static CEvent* CreateEvent(bool bManualReset, bool bInitialState, const char* lpName);
static void CloseHandle(CEvent* &p_event);
static unsigned long WaitForSingleObject(CEvent* p_event, long ms);
/// 触发事件
static bool SetEvent(CEvent* p_event);
/// 复位事件
static bool ResetEvent(CEvent* p_event);
private:
bool _b_manual_reset;
atomic_bool _b_initial_state;
sem_t* _p_named_sem = SEM_FAILED;
sem_t _sem;
string _sem_name;
long _lpEventAttributes;
int _sem_count = 0;
};
#endif
event.cpp
#include "event.h"
#include "unistd.h"
CEvent::CEvent(long lpEventAttributes, bool bManualReset, bool bInitialState, const char* lpName)
{
_lpEventAttributes = lpEventAttributes;
_b_manual_reset = bManualReset;
_b_initial_state = bInitialState;
if (lpName != nullptr) _sem_name = lpName;
}
CEvent::~CEvent()
{
if (_p_named_sem != SEM_FAILED)
{
if (_sem_name.empty())
{
sem_destroy(&_sem);
_p_named_sem = SEM_FAILED;
}
else
{
sem_close(_p_named_sem);
sem_unlink(_sem_name.c_str());
_sem_name.clear();
}
}
}
bool CEvent::Init()
{
if (_b_initial_state)
_sem_count = 1;
else
_sem_count = 0;
if (_sem_name.empty())
{
if (0 != sem_init(&_sem, 0, _sem_count))
{
_p_named_sem = SEM_FAILED;
return false;
}
_p_named_sem = &_sem;
}
else
{
_p_named_sem = sem_open(_sem_name.c_str(), O_CREAT, _lpEventAttributes, _sem_count);
if (_p_named_sem == SEM_FAILED)
{
sem_unlink(_sem_name.c_str());
_sem_name.clear();
return false;
}
}
return true;
}
timespec CEvent::sem_get_time_millsecs(long msecs)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long secs = msecs / 1000;
msecs = msecs % 1000;
long add = 0;
msecs = msecs * 1000 * 1000 + ts.tv_nsec;
add = msecs / (1000 * 1000 * 1000);
ts.tv_sec += (add + secs);
ts.tv_nsec = msecs % (1000 * 1000 * 1000);
return ts;
}
CEvent* CEvent::CreateEvent(long lpEventAttributes, bool bManualReset, bool bInitialState, const char* lpName)
{
CEvent* p_event = new CEvent(lpEventAttributes, bManualReset, bInitialState, lpName);
if (!p_event->Init())
{
delete p_event;
return nullptr;
}
return p_event;
}
CEvent* CEvent::CreateEvent(bool bManualReset, bool bInitialState, const char* lpName)
{
CEvent* p_event = new CEvent(S_IRWXU, bManualReset, bInitialState, lpName);
if (!p_event->Init())
{
delete p_event;
return nullptr;
}
return p_event;
}
void CEvent::CloseHandle(CEvent* &p_event)
{
if (p_event != nullptr)
delete p_event;
p_event = nullptr;
}
unsigned long CEvent::WaitForSingleObject(CEvent* p_event, long ms)
{
if (p_event == nullptr)
return WAIT_FAILED;
if (p_event->_b_initial_state)
{
if (!p_event->_b_manual_reset)
{
p_event->_b_initial_state = false;
while (sem_getvalue(p_event->_p_named_sem, &p_event->_sem_count) == 0 && p_event->_sem_count > 0)
sem_wait(p_event->_p_named_sem);
}
return WAIT_OBJECT_0;
}
if (ms == 0)
{
int n_ret = sem_wait(p_event->_p_named_sem);
if (n_ret != 0)
return WAIT_FAILED;
}
else
{
int n_ret = 0;
timespec ts = p_event->sem_get_time_millsecs(ms);
while ((n_ret = sem_timedwait(p_event->_p_named_sem, &ts)) == -1 && errno == EINTR)
continue;
if (n_ret == -1)
{
if (errno == ETIMEDOUT)
return WAIT_TIMEOUT;
else
return WAIT_FAILED;
}
}
if (p_event->_b_manual_reset)
p_event->_b_initial_state = true;
return WAIT_OBJECT_0;
}
bool CEvent::SetEvent(CEvent* p_event)
{
if (p_event == nullptr) return false;
int n_ret = sem_post(p_event->_p_named_sem);
if (n_ret != 0)
return false;
return true;
}
bool CEvent::ResetEvent(CEvent* p_event)
{
if (p_event == nullptr) return false;
p_event->_b_initial_state = false;
while (sem_getvalue(p_event->_p_named_sem, &p_event->_sem_count) == 0 && p_event->_sem_count > 0)
sem_wait(p_event->_p_named_sem);
return true;
}
#ifdef EVENT_DEMO
/// g++ event.cpp -DEVENT_DEMO -o event-demo -lpthread
void* thread_func(void* arg)
{
CEvent *p = (CEvent*)arg;
int i = 0;
while(1)
{
i++;
sleep(1);
if(i >= 5)CEvent::SetEvent(p);
printf("sleep %d s\n", i);
}
return nullptr;
}
int main(int argc, char* argv[])
{
printf("event-demo\n");
CEvent *event = CEvent::CreateEvent(true, false, "sec1");
pthread_t tid;
pthread_create(&tid, NULL, thread_func, event);
pthread_detach(tid);
if(CEvent::WaitForSingleObject(event, 10*1000)==CEvent::WAIT_OBJECT_0)
{
printf("wait event ok\n");
CEvent::ResetEvent(event);
}
CEvent::CloseHandle(event);
return 0;
}
#endif
编译命令
g++ event.cpp -DEVENT_DEMO -o event-demo -lpthread
运行结果:
./event-demo
event-demo
sleep 1 s
sleep 2 s
sleep 3 s
sleep 4 s
sleep 5 s
wait event ok
本文链接地址:https://const.net.cn/500.html