Merge branch 'thread' of https://git.shs.tools/SHS/pip into thread

This commit is contained in:
Бычков Андрей
2022-11-08 10:53:43 +03:00
4 changed files with 29 additions and 14 deletions

View File

@@ -28,7 +28,6 @@
# include <fcntl.h> # include <fcntl.h>
# include <sys/ioctl.h> # include <sys/ioctl.h>
#endif #endif
#include "piincludes_p.h"
#include "pistring.h" #include "pistring.h"
@@ -45,7 +44,7 @@ void PIWaitEvent::create() {
piCout << "Error with CreateEventA:" << errorString(); piCout << "Error with CreateEventA:" << errorString();
} }
#else #else
for (int i = 0; i < sizeof(fds); ++i) memset(&(fds[i]), 0, sizeof(fds[i])); for (int i = 0; i < 3; ++i) memset(&(fds[i]), 0, sizeof(fds[i]));
if (::pipe(pipe_fd) < 0) { if (::pipe(pipe_fd) < 0) {
piCout << "Error with pipe:" << errorString(); piCout << "Error with pipe:" << errorString();
} else { } else {
@@ -62,7 +61,7 @@ void PIWaitEvent::destroy() {
event = NULL; event = NULL;
} }
#else #else
for (int i = 0; i < sizeof(pipe_fd); ++i) { for (int i = 0; i < 2; ++i) {
if (pipe_fd[i] != 0) { if (pipe_fd[i] != 0) {
::close(pipe_fd[i]); ::close(pipe_fd[i]);
pipe_fd[i] = 0; pipe_fd[i] = 0;
@@ -82,7 +81,7 @@ bool PIWaitEvent::wait(int fd, CheckRole role) {
if (fd == -1) return false; if (fd == -1) return false;
int nfds = piMaxi(pipe_fd[ReadEnd], fd) + 1; int nfds = piMaxi(pipe_fd[ReadEnd], fd) + 1;
int fd_index = role; int fd_index = role;
for (int i = 0; i < sizeof(fds); ++i) FD_ZERO(&(fds[i])); for (int i = 0; i < 3; ++i) FD_ZERO(&(fds[i]));
FD_SET(pipe_fd[ReadEnd], &(fds[CheckRead])); FD_SET(pipe_fd[ReadEnd], &(fds[CheckRead]));
FD_SET(fd, &(fds[CheckExeption])); FD_SET(fd, &(fds[CheckExeption]));
if (fd_index != CheckExeption) FD_SET(fd, &(fds[fd_index])); if (fd_index != CheckExeption) FD_SET(fd, &(fds[fd_index]));
@@ -116,5 +115,9 @@ bool PIWaitEvent::isCreate() const {
void * PIWaitEvent::getEvent() const { void * PIWaitEvent::getEvent() const {
#ifdef WINDOWS
return event; return event;
#else
return nullptr;
#endif
} }

View File

@@ -35,19 +35,17 @@ class PIWaitEvent {
public: public:
~PIWaitEvent(); ~PIWaitEvent();
void create();
void destroy();
enum CheckRole { // UNIX only enum CheckRole { // UNIX only
CheckRead, CheckRead,
CheckWrite, CheckWrite,
CheckExeption CheckExeption
}; };
bool wait(int fd = -1, CheckRole role = CheckRead);
void create();
void destroy();
bool wait(int fd = -1, CheckRole role = CheckRead);
void interrupt(); void interrupt();
bool isCreate() const; bool isCreate() const;
void * getEvent() const; // WINDOWS only void * getEvent() const; // WINDOWS only
private: private:

View File

@@ -18,7 +18,7 @@
*/ */
#include "pican.h" #include "pican.h"
#include "pipropertystorage.h" #include "pipropertystorage.h"
#include "piincludes_p.h" #include "piwaitevent_p.h"
#if !defined(WINDOWS) && !defined(MAC_OS) && !defined(MICRO_PIP) #if !defined(WINDOWS) && !defined(MAC_OS) && !defined(MICRO_PIP)
# define PIP_CAN # define PIP_CAN
#endif #endif
@@ -39,17 +39,24 @@
REGISTER_DEVICE(PICAN) REGISTER_DEVICE(PICAN)
PRIVATE_DEFINITION_START(PICAN)
PIWaitEvent event;
PRIVATE_DEFINITION_END(PICAN)
PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) { PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
setThreadedReadBufferSize(256); setThreadedReadBufferSize(256);
setPath(path); setPath(path);
can_id = 0; can_id = 0;
sock = 0; sock = 0;
PRIVATE->event.create();
} }
PICAN::~PICAN() { PICAN::~PICAN() {
stop(); stopAndWait();
close(); close();
PRIVATE->event.destroy();
} }
@@ -92,6 +99,7 @@ bool PICAN::openDevice() {
bool PICAN::closeDevice() { bool PICAN::closeDevice() {
#ifdef PIP_CAN #ifdef PIP_CAN
interrupt();
if (sock > 0) ::close(sock); if (sock > 0) ::close(sock);
#endif #endif
return true; return true;
@@ -103,9 +111,8 @@ ssize_t PICAN::readDevice(void * read_to, ssize_t max_size) {
//piCout << "PICAN read"; //piCout << "PICAN read";
can_frame frame; can_frame frame;
ssize_t ret = 0; ssize_t ret = 0;
reading_now = true; if (PRIVATE->event.wait(sock))
ret = ::read(sock, &frame, sizeof(can_frame)); ret = ::read(sock, &frame, sizeof(can_frame));
reading_now = false;
if (ret < 0) {/*piCoutObj << "Error while read CAN frame " << ret;*/ return -1;} if (ret < 0) {/*piCoutObj << "Error while read CAN frame " << ret;*/ return -1;}
//piCoutObj << "receive CAN frame Id =" << frame.can_id; //piCoutObj << "receive CAN frame Id =" << frame.can_id;
memcpy(read_to, frame.data, piMini(frame.can_dlc, max_size)); memcpy(read_to, frame.data, piMini(frame.can_dlc, max_size));
@@ -148,6 +155,11 @@ int PICAN::readedCANID() const {
} }
void PICAN::interrupt() {
PRIVATE->event.interrupt();
}
PIString PICAN::constructFullPathDevice() const { PIString PICAN::constructFullPathDevice() const {
PIString ret; PIString ret;
ret += path() + ":" + PIString::fromNumber(CANID(), 16); ret += path() + ":" + PIString::fromNumber(CANID(), 16);

View File

@@ -39,6 +39,7 @@ public:
void setCANID(int id); void setCANID(int id);
int CANID() const; int CANID() const;
int readedCANID() const; int readedCANID() const;
void interrupt() override;
protected: protected:
bool openDevice() override; bool openDevice() override;
@@ -52,6 +53,7 @@ protected:
DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;}
private: private:
PRIVATE_DECLARATION(PIP_EXPORT)
int sock; int sock;
int can_id, readed_id; int can_id, readed_id;
}; };