forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinux.cpp
More file actions
203 lines (182 loc) · 4.38 KB
/
Copy pathlinux.cpp
File metadata and controls
203 lines (182 loc) · 4.38 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/detail/linux.hpp>
#include <system_error>
#include <unistd.h>
#include <cstring>
#include <cassert>
namespace cppcoro
{
namespace detail
{
namespace linux
{
message_queue::message_queue()
{
if (pipe2(m_pipefd, O_NONBLOCK) == -1) {
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error creating io_service: failed creating pipe"
};
}
m_epollfd = safe_fd{create_epoll_fd()};
m_ev.data.fd = m_pipefd[0];
m_ev.events = EPOLLIN;
add_fd_watch(m_pipefd[0], reinterpret_cast<void*>(m_pipefd[0]), EPOLLIN);
}
message_queue::~message_queue()
{
assert(close(m_pipefd[0]) == 0);
assert(close(m_pipefd[1]) == 0);
}
void message_queue::add_fd_watch(int fd, void* cb, uint32_t events){
struct epoll_event ev = {0};
ev.data.ptr = cb;
ev.events = events;
if(epoll_ctl(m_epollfd.fd(), EPOLL_CTL_ADD, fd, &ev) == -1)
{
if (errno == EPERM) {
// epoll returns EPERM on regular files because they are
// always ready for read/write, we can just queue the callback to run
enqueue_message(cb, CALLBACK_TYPE);
} else {
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"message_queue: add_fd_watch failed"
};
}
}
}
void message_queue::remove_fd_watch(int fd){
if(epoll_ctl(m_epollfd.fd(), EPOLL_CTL_DEL, fd, NULL) == -1)
{
if (errno != EPERM) {
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"message_queue: remove_fd_watch failed"
};
}
}
}
bool message_queue::enqueue_message(void* msg, message_type type)
{
message qmsg;
qmsg.m_type = type;
qmsg.m_ptr = msg;
int status = write(m_pipefd[1], (const char*)&qmsg, sizeof(message));
return status==-1?false:true;
}
bool message_queue::dequeue_message(void*& msg, message_type& type, bool wait)
{
struct epoll_event ev = {0};
int nfds = epoll_wait(m_epollfd.fd(), &ev, 1, wait?-1:0);
if(nfds == -1)
{
if (errno == EINTR || errno == EAGAIN) {
return false;
}
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error in epoll_wait run loop"
};
}
if(nfds == 0 && !wait)
{
return false;
}
if(nfds == 0 && wait)
{
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error in epoll_wait run loop"
};
}
if (ev.data.fd == m_pipefd[0]) {
message qmsg;
ssize_t status = read(m_pipefd[0], (char*)&qmsg, sizeof(message));
if(status == -1)
{
if (errno == EINTR || errno == EAGAIN) {
return false;
}
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error retrieving message from message queue: mq_receive"
};
}
msg = qmsg.m_ptr;
type = qmsg.m_type;
return true;
} else {
msg = ev.data.ptr;
type = CALLBACK_TYPE;
return true;
}
}
safe_fd create_event_fd()
{
int fd = eventfd(0, EFD_SEMAPHORE | EFD_NONBLOCK | EFD_CLOEXEC);
if(fd == -1)
{
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error creating io_service: event fd create"
};
}
return safe_fd{fd};
}
safe_fd create_timer_fd()
{
int fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if(fd == -1)
{
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error creating io_service: timer fd create"
};
}
return safe_fd{fd};
}
safe_fd create_epoll_fd()
{
int fd = epoll_create1(EPOLL_CLOEXEC);
if(fd == -1)
{
throw std::system_error
{
static_cast<int>(errno),
std::system_category(),
"Error creating timer thread: epoll create"
};
}
return safe_fd{fd};
}
void safe_fd::close() noexcept
{
if(m_fd != -1)
{
::close(m_fd);
m_fd = -1;
}
}
}
}
}