-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventFile.cpp
More file actions
executable file
·102 lines (86 loc) · 1.45 KB
/
EventFile.cpp
File metadata and controls
executable file
·102 lines (86 loc) · 1.45 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* EventFile.cpp
*
* Created on: 5 Sep 2013
* Author: lester
*/
#ifndef _WIN32
#include "EventFile.h"
#include <sys/eventfd.h>
#include <unistd.h>
#include "poll.h"
#include "errno.h"
#include "log.h"
EventFile::EventFile()
{
fd = -1;
}
EventFile::~EventFile()
{
release();
}
void EventFile::release()
{
if (fd != -1)
{
close(fd);
fd = -1;
}
}
void EventFile::init()
{
if (fd == -1)
{
// create a simple event file, read will put value to zero
fd = eventfd(0, EFD_NONBLOCK);
if (fd == -1)
{
throw uexception::error_api(ERROR_INFO());
}
}
}
void EventFile::set()
{
uint64_t u = 1;
if (write(fd, &u, sizeof(u)) == -1)
{
// blocking operation will be considerate as already reset event
if (errno != EAGAIN)
{
throw uexception::error_api(ERROR_INFO());
}
}
}
/*
Read data from event to reset status.
if operation would block
*/
void EventFile::reset()
{
unsigned char ui64[8];
if (read(fd, &ui64, sizeof(ui64)) == -1)
{
// blocking operation will be considerate as already reset event
if (errno != EAGAIN)
{
throw uexception::error_api(ERROR_INFO());
}
}
}
/*
wait for data available for read on file
or a numbers of milliseconds pass
*/
bool EventFile::waitRead(int mstimeout)
{
struct pollfd pfd[1];
pfd[0].fd = fd;
pfd[0].events = POLLIN | POLLPRI;
if (poll(pfd, 1, mstimeout) < 0)
{
throw uexception::error_api(ERROR_INFO());
}
if (!pfd[0].revents) return false;
return true;
}
#endif