From 8f6e7d2cae370c3cfe20092ec0132105e59584a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Wed, 23 Nov 2016 10:59:53 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@282 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src/containers/pideque.h | 6 ++-- src/containers/pivector.h | 6 ++-- src/core/pitime.cpp | 5 +++ src/core/pitime.h | 1 + src/io/piserial.cpp | 8 ++--- src/io/piserial.h | 6 ++-- src/thread/pimutex.cpp | 59 ++++++++++++++++++++++++----------- src/thread/pimutex.h | 9 +++++- src/thread/pipipelinethread.h | 9 ++++-- 9 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/containers/pideque.h b/src/containers/pideque.h index c7791d75..23b3ccfc 100755 --- a/src/containers/pideque.h +++ b/src/containers/pideque.h @@ -419,9 +419,9 @@ private: if (as != pid_rsize) { //printf("(%p) realloc %d -> %d (%p)\n", this, pid_rsize, as, pid_data); PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T)) - T * p_d = (T*)(realloc(pid_data, as*sizeof(T))); - if (p_d) - pid_data = p_d; + T * p_d = (T*)(realloc(pid_data, as*sizeof(T))); + assert(p_d); + pid_data = p_d; pid_rsize = as; //printf("(%p) realloc done (%p)\n", this, pid_data); } diff --git a/src/containers/pivector.h b/src/containers/pivector.h index 881cf4fe..7f3ef7d1 100755 --- a/src/containers/pivector.h +++ b/src/containers/pivector.h @@ -356,9 +356,9 @@ private: if (as == piv_rsize) return; //cout << std::hex << " ![("< (new 0x" << (llong)piv_data << ") ok]!" << endl; diff --git a/src/core/pitime.cpp b/src/core/pitime.cpp index 8ee6cb3e..fd0173b9 100755 --- a/src/core/pitime.cpp +++ b/src/core/pitime.cpp @@ -148,6 +148,11 @@ bool operator >(const PIDateTime & t0, const PIDateTime & t1) { } +PISystemTime PITime::toSystemTime() const { + return PISystemTime((hours * 60. + minutes) * 60. + seconds, milliseconds * 1000.); +} + + PITime PITime::current() { time_t rt = ::time(0); tm * pt = localtime(&rt); diff --git a/src/core/pitime.h b/src/core/pitime.h index 6d80694c..8176227b 100755 --- a/src/core/pitime.h +++ b/src/core/pitime.h @@ -192,6 +192,7 @@ struct PIP_EXPORT PITime { int seconds; int milliseconds; PIString toString(const PIString & format = "h:mm:ss") const; + PISystemTime toSystemTime() const; static PITime current(); static PITime fromSystemTime(const PISystemTime & st); }; diff --git a/src/io/piserial.cpp b/src/io/piserial.cpp index 13f4ab5c..cc5af26c 100755 --- a/src/io/piserial.cpp +++ b/src/io/piserial.cpp @@ -541,7 +541,7 @@ void PISerial::applySettings() { } PRIVATE->desc.StopBits = params[PISerial::TwoStopBits] ? TWOSTOPBITS : ONESTOPBIT; - PRIVATE->desc.fOutxCtsFlow = 0; + /*PRIVATE->desc.fOutxCtsFlow = 0; PRIVATE->desc.fOutxDsrFlow = 0; PRIVATE->desc.fDtrControl = 0; PRIVATE->desc.fRtsControl = 0; @@ -549,7 +549,7 @@ void PISerial::applySettings() { PRIVATE->desc.fOutX = 0; PRIVATE->desc.fBinary = 1; PRIVATE->desc.fAbortOnError = 0; - PRIVATE->desc.fNull = 0; + PRIVATE->desc.fNull = 0;*/ if (SetCommState(PRIVATE->hCom, &PRIVATE->desc) == -1) { piCoutObj << "Unable to set comm state for \"" << path() << "\""; return; @@ -629,9 +629,9 @@ int PISerial::read(void * read_to, int max_size) { if (sending) return -1; // piCoutObj << "com event ..."; //WaitCommEvent(PRIVATE->hCom, 0, 0); -// piCoutObj << "read ..."; + //piCoutObj << "read ..." << PRIVATE->hCom; ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0); -// piCoutObj << "read ok"; + //piCoutObj << "read ok" << PRIVATE->readed; return PRIVATE->readed; #else if (!canRead()) return -1; diff --git a/src/io/piserial.h b/src/io/piserial.h index eb8a7c03..41bd9949 100755 --- a/src/io/piserial.h +++ b/src/io/piserial.h @@ -177,9 +177,9 @@ public: bool send(const void * data, int size) {return (write(data, size) == size);} /// NOTE: no reason to use this function, use PIString::toUtf8() or PIString::dataAscii(),lengthAscii() instead -// //! \brief Write to device string "data" and wait for data written if "wait" is \b true. -// //! \returns \b true if sended bytes count = size of string -// bool send(const PIString & data, bool wait = false) {return (write(data.data(), data.lengthAscii(), wait) == data.size_s());} + //! \brief Write to device string "data" and wait for data written if "wait" is \b true. + //! \returns \b true if sended bytes count = size of string + //bool send(const PIString & data, bool wait = false) {return (write(data.data(), data.lengthAscii(), wait) == data.size_s());} //! \brief Write to device byte array "data" and wait for data written if "wait" is \b true. //! \returns \b true if sended bytes count = size of string diff --git a/src/thread/pimutex.cpp b/src/thread/pimutex.cpp index f5903d63..ff9a7287 100755 --- a/src/thread/pimutex.cpp +++ b/src/thread/pimutex.cpp @@ -38,29 +38,19 @@ * */ -PIMutex::PIMutex() { -#ifdef WINDOWS - mutex = CreateMutex(0, false, 0); -#else - pthread_mutexattr_t attr; - memset(&attr, 0, sizeof(attr)); - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); - memset(&mutex, 0, sizeof(mutex)); - pthread_mutex_init(&mutex, &attr); - pthread_mutexattr_destroy(&attr); -#endif - locked = false; +PIMutex::PIMutex(): inited_(false) { + init(); } PIMutex::~PIMutex() { -#ifdef WINDOWS - if (mutex != 0) CloseHandle(mutex); -#else - pthread_mutex_destroy(&mutex); -#endif - locked = false; + destroy(); +} + + +PIMutex & PIMutex::operator =(const PIMutex & other) { + init(); + return *this; } @@ -99,3 +89,34 @@ bool PIMutex::tryLock() { bool PIMutex::isLocked() const { return locked; } + + +void PIMutex::init() { + if (inited_) destroy(); +#ifdef WINDOWS + mutex = CreateMutex(0, false, 0); +#else + pthread_mutexattr_t attr; + memset(&attr, 0, sizeof(attr)); + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); + memset(&mutex, 0, sizeof(mutex)); + pthread_mutex_init(&mutex, &attr); + pthread_mutexattr_destroy(&attr); +#endif + locked = false; + inited_ = true; +} + + +void PIMutex::destroy() { + if (inited_) { +#ifdef WINDOWS + if (mutex) CloseHandle(mutex); + mutex = 0; +#else + pthread_mutex_destroy(&mutex); +#endif + } + locked = inited_ = false; +} diff --git a/src/thread/pimutex.h b/src/thread/pimutex.h index 60ac3796..e32e8cdd 100755 --- a/src/thread/pimutex.h +++ b/src/thread/pimutex.h @@ -33,9 +33,12 @@ class PIP_EXPORT PIMutex public: //! Constructs unlocked mutex - PIMutex(); + explicit PIMutex(); ~PIMutex(); + + + PIMutex & operator =(const PIMutex & other); //! \brief Lock mutex @@ -56,12 +59,16 @@ public: bool isLocked() const; private: + void init(); + void destroy(); + #ifdef WINDOWS void * #else pthread_mutex_t #endif mutex; + bool inited_; volatile bool locked; }; diff --git a/src/thread/pipipelinethread.h b/src/thread/pipipelinethread.h index 111df4cc..add23a61 100644 --- a/src/thread/pipipelinethread.h +++ b/src/thread/pipipelinethread.h @@ -48,7 +48,7 @@ public: void enqueue(const Tin &v) {enqueue(v, 0);} EVENT_HANDLER2(void, enqueue, const Tin &, v, bool *, overload) { mutex.lock(); -// piCout << "enque" << max_size << in.size(); + //piCoutObj << "enque" << overload;//max_size << in.size(); if (max_size == 0 || in.size() < max_size) { in.enqueue(v); if (overload) *overload = false; @@ -119,15 +119,17 @@ protected: private: void begin() {cnt = 0;} void run() { + //piCoutObj << "run ..."; mutex.lock(); if (in.isEmpty()) { mutex.unlock(); piMSleep(10); + //piCoutObj << "run in empty"; return; } if (next_overload && wait_next_pipe) { mutex.unlock(); - piCoutObj << &next_overload; + //piCoutObj << "wait" << &next_overload; calculated(last, &next_overload); piMSleep(10); } else { @@ -141,10 +143,11 @@ private: mutex_l.unlock(); cnt++; // next_overload = true; - piCoutObj << &next_overload; + //piCoutObj << "calc ok" << &next_overload; calculated(r, &next_overload); } } + //piCoutObj << "run ok"; } PIMutex mutex; PIMutex mutex_l;