Skip to content

Commit 3116bbb

Browse files
committed
Use poll instead of fcntl
1 parent 66ddd81 commit 3116bbb

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

modules/yup_core/native/yup_Watchdog_linux.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class Watchdog::Impl final
3535
if (fd < 0)
3636
return;
3737

38-
const int flags = fcntl (fd, F_GETFL, 0);
39-
fcntl (fd, F_SETFL, flags | O_NONBLOCK);
40-
4138
addPaths (folder);
4239

4340
thread = std::thread ([this]
@@ -156,27 +153,30 @@ class Watchdog::Impl final
156153
void threadCallback()
157154
{
158155
auto lastRenamedPath = std::optional<File> {};
159-
char buffer[bufferSize] = { 0 };
156+
char buffer[bufferSize + 1] = { 0 };
157+
158+
struct pollfd pfd;
159+
pfd.fd = fd;
160+
pfd.events = POLLIN;
160161

161162
while (! threadShouldExit)
162163
{
164+
const int pollResult = poll (&pfd, 1, 200);
165+
166+
if (threadShouldExit)
167+
break;
168+
169+
if (pollResult <= 0 || pfd.revents & POLLIN == 0)
170+
continue;
171+
163172
const ssize_t numRead = read (fd, buffer, bufferSize);
164-
if (numRead < 0)
165-
{
166-
if (errno == EAGAIN || errno == EWOULDBLOCK)
167-
{
168-
std::this_thread::sleep_for (std::chrono::milliseconds (50));
169-
continue;
170-
}
171-
else
172-
{
173-
break;
174-
}
175-
}
176173

177174
if (threadShouldExit)
178175
break;
179176

177+
if (numRead <= 0)
178+
continue;
179+
180180
const inotify_event* notifyEvent = nullptr;
181181
for (const char* ptr = buffer; ptr < buffer + numRead; ptr += sizeof (struct inotify_event) + notifyEvent ? notifyEvent->len : 0)
182182
{

0 commit comments

Comments
 (0)