Files
pip/libs/main/core/piwaitevent_p.cpp
peri4 8a5e72c723 migrate to async IO model
new PIIODevice::interrupt() virtual method
new PIWaitEvent private class
PIEthernet and PISerial basically tested on Windows and Linux
2022-11-05 23:43:07 +03:00

127 lines
2.8 KiB
C++

/*
PIP - Platform Independent Primitives
Private PIP wait object
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piwaitevent_p.h"
#ifdef WINDOWS
//# ifdef _WIN32_WINNT
//# undef _WIN32_WINNT
//# define _WIN32_WINNT 0x0600
//# endif
# include <synchapi.h>
#else
# include <fcntl.h>
# include <sys/ioctl.h>
#endif
#include "piincludes_p.h"
#include "pistring.h"
PIWaitEvent::~PIWaitEvent() {
destroy();
}
void PIWaitEvent::create() {
destroy();
#ifdef WINDOWS
event = CreateEventA(NULL, TRUE, FALSE, NULL);
if (!event) {
piCout << "Error with CreateEventA:" << errorString();
}
#else
for (int i = 0; i < 3; ++i) memset(&(fds[i]), 0, sizeof(fds[i]));
if (::pipe(pipe_fd) < 0) {
piCout << "Error with pipe:" << errorString();
} else {
fcntl(pipe_fd[ReadEnd], F_SETFL, O_NONBLOCK);
}
#endif
}
void PIWaitEvent::destroy() {
#ifdef WINDOWS
if (event) {
CloseHandle(event);
event = NULL;
}
#else
for (int i = 0; i < 2; ++i) {
if (pipe_fd[i] != 0) {
::close(pipe_fd[i]);
pipe_fd[i] = 0;
}
}
#endif
}
#ifdef WINDOWS
bool PIWaitEvent::wait() {
#else
bool PIWaitEvent::wait(int fd, PIWaitEvent::SelectRole role) {
#endif
if (!isCreate()) return false;
#ifdef WINDOWS
DWORD ret = WaitForSingleObjectEx(event, INFINITE, TRUE);
ResetEvent(event);
if (ret == WAIT_IO_COMPLETION || ret == WAIT_FAILED) return false;
#else
int nfds = piMaxi(pipe_fd[ReadEnd], fd) + 1;
int fd_index = 0;
switch (role) {
case CheckRead:
fd_index = 0;
break;
case CheckWrite:
fd_index = 1;
break;
}
for (int i = 0; i < 3; ++i) FD_ZERO(&(fds[i]));
FD_SET(pipe_fd[ReadEnd], &(fds[0]));
FD_SET(fd, &(fds[2]));
FD_SET(fd, &(fds[fd_index]));
::select(nfds, &(fds[0]), &(fds[1]), &(fds[2]), nullptr);
int buf = 0;
while (::read(pipe_fd[ReadEnd], &buf, sizeof(buf)) > 0);
if (FD_ISSET(fd, &(fds[2]))) return false;
return FD_ISSET(fd, &(fds[fd_index]));
#endif
return true;
}
void PIWaitEvent::interrupt() {
if (!isCreate()) return;
#ifdef WINDOWS
SetEvent(event);
#else
::write(pipe_fd[WriteEnd], "", 1);
#endif
}
bool PIWaitEvent::isCreate() const {
#ifdef WINDOWS
return event;
#else
return pipe_fd[ReadEnd] != 0;
#endif
}