Files
pip/libs/main/core/piwaitevent_p.cpp

121 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 < sizeof(fds); ++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 < sizeof(pipe_fd); ++i) {
if (pipe_fd[i] != 0) {
::close(pipe_fd[i]);
pipe_fd[i] = 0;
}
}
#endif
}
bool PIWaitEvent::wait(int fd, CheckRole role) {
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
if (fd == -1) return false;
int nfds = piMaxi(pipe_fd[ReadEnd], fd) + 1;
int fd_index = role;
for (int i = 0; i < sizeof(fds); ++i) FD_ZERO(&(fds[i]));
FD_SET(pipe_fd[ReadEnd], &(fds[CheckRead]));
FD_SET(fd, &(fds[CheckExeption]));
if (fd_index != CheckExeption) FD_SET(fd, &(fds[fd_index]));
::select(nfds, &(fds[CheckRead]), &(fds[CheckWrite]), &(fds[CheckExeption]), nullptr);
int buf = 0;
while (::read(pipe_fd[ReadEnd], &buf, sizeof(buf)) > 0);
if (FD_ISSET(fd, &(fds[CheckExeption]))) 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
}
void * PIWaitEvent::getEvent() const {
return event;
}