git-svn-id: svn://db.shs.com.ru/pip@282 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-11-23 10:59:53 +00:00
parent d3166f8fa5
commit 8f6e7d2cae
9 changed files with 73 additions and 36 deletions

View File

@@ -420,7 +420,7 @@ private:
//printf("(%p) realloc %d -> %d (%p)\n", this, pid_rsize, as, pid_data); //printf("(%p) realloc %d -> %d (%p)\n", this, pid_rsize, as, pid_data);
PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T)) PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T))
T * p_d = (T*)(realloc(pid_data, as*sizeof(T))); T * p_d = (T*)(realloc(pid_data, as*sizeof(T)));
if (p_d) assert(p_d);
pid_data = p_d; pid_data = p_d;
pid_rsize = as; pid_rsize = as;
//printf("(%p) realloc done (%p)\n", this, pid_data); //printf("(%p) realloc done (%p)\n", this, pid_data);

View File

@@ -357,7 +357,7 @@ private:
//cout << std::hex << " ![("<<this<<")realloc " << piv_data << " data ... <\n" << endl; //cout << std::hex << " ![("<<this<<")realloc " << piv_data << " data ... <\n" << endl;
T * p_d = (T*)(realloc(piv_data, as*sizeof(T))); T * p_d = (T*)(realloc(piv_data, as*sizeof(T)));
if (p_d) assert(p_d);
piv_data = p_d; piv_data = p_d;
//zeroRaw(&(piv_data[os]), as - os); //zeroRaw(&(piv_data[os]), as - os);
piv_rsize = as; piv_rsize = as;

View File

@@ -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() { PITime PITime::current() {
time_t rt = ::time(0); time_t rt = ::time(0);
tm * pt = localtime(&rt); tm * pt = localtime(&rt);

View File

@@ -192,6 +192,7 @@ struct PIP_EXPORT PITime {
int seconds; int seconds;
int milliseconds; int milliseconds;
PIString toString(const PIString & format = "h:mm:ss") const; PIString toString(const PIString & format = "h:mm:ss") const;
PISystemTime toSystemTime() const;
static PITime current(); static PITime current();
static PITime fromSystemTime(const PISystemTime & st); static PITime fromSystemTime(const PISystemTime & st);
}; };

View File

@@ -541,7 +541,7 @@ void PISerial::applySettings() {
} }
PRIVATE->desc.StopBits = params[PISerial::TwoStopBits] ? TWOSTOPBITS : ONESTOPBIT; PRIVATE->desc.StopBits = params[PISerial::TwoStopBits] ? TWOSTOPBITS : ONESTOPBIT;
PRIVATE->desc.fOutxCtsFlow = 0; /*PRIVATE->desc.fOutxCtsFlow = 0;
PRIVATE->desc.fOutxDsrFlow = 0; PRIVATE->desc.fOutxDsrFlow = 0;
PRIVATE->desc.fDtrControl = 0; PRIVATE->desc.fDtrControl = 0;
PRIVATE->desc.fRtsControl = 0; PRIVATE->desc.fRtsControl = 0;
@@ -549,7 +549,7 @@ void PISerial::applySettings() {
PRIVATE->desc.fOutX = 0; PRIVATE->desc.fOutX = 0;
PRIVATE->desc.fBinary = 1; PRIVATE->desc.fBinary = 1;
PRIVATE->desc.fAbortOnError = 0; PRIVATE->desc.fAbortOnError = 0;
PRIVATE->desc.fNull = 0; PRIVATE->desc.fNull = 0;*/
if (SetCommState(PRIVATE->hCom, &PRIVATE->desc) == -1) { if (SetCommState(PRIVATE->hCom, &PRIVATE->desc) == -1) {
piCoutObj << "Unable to set comm state for \"" << path() << "\""; piCoutObj << "Unable to set comm state for \"" << path() << "\"";
return; return;
@@ -629,9 +629,9 @@ int PISerial::read(void * read_to, int max_size) {
if (sending) return -1; if (sending) return -1;
// piCoutObj << "com event ..."; // piCoutObj << "com event ...";
//WaitCommEvent(PRIVATE->hCom, 0, 0); //WaitCommEvent(PRIVATE->hCom, 0, 0);
// piCoutObj << "read ..."; //piCoutObj << "read ..." << PRIVATE->hCom;
ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0); ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0);
// piCoutObj << "read ok"; //piCoutObj << "read ok" << PRIVATE->readed;
return PRIVATE->readed; return PRIVATE->readed;
#else #else
if (!canRead()) return -1; if (!canRead()) return -1;

View File

@@ -177,8 +177,8 @@ public:
bool send(const void * data, int size) {return (write(data, size) == size);} 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 /// 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. //! \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 //! \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());} //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. //! \brief Write to device byte array "data" and wait for data written if "wait" is \b true.

View File

@@ -38,29 +38,19 @@
* */ * */
PIMutex::PIMutex() { PIMutex::PIMutex(): inited_(false) {
#ifdef WINDOWS init();
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() { PIMutex::~PIMutex() {
#ifdef WINDOWS destroy();
if (mutex != 0) CloseHandle(mutex); }
#else
pthread_mutex_destroy(&mutex);
#endif PIMutex & PIMutex::operator =(const PIMutex & other) {
locked = false; init();
return *this;
} }
@@ -99,3 +89,34 @@ bool PIMutex::tryLock() {
bool PIMutex::isLocked() const { bool PIMutex::isLocked() const {
return locked; 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;
}

View File

@@ -33,11 +33,14 @@ class PIP_EXPORT PIMutex
public: public:
//! Constructs unlocked mutex //! Constructs unlocked mutex
PIMutex(); explicit PIMutex();
~PIMutex(); ~PIMutex();
PIMutex & operator =(const PIMutex & other);
//! \brief Lock mutex //! \brief Lock mutex
//! \details If mutex is unlocked it set to locked state and returns immediate. //! \details If mutex is unlocked it set to locked state and returns immediate.
//! If mutex is already locked function blocks until mutex will be unlocked //! If mutex is already locked function blocks until mutex will be unlocked
@@ -56,12 +59,16 @@ public:
bool isLocked() const; bool isLocked() const;
private: private:
void init();
void destroy();
#ifdef WINDOWS #ifdef WINDOWS
void * void *
#else #else
pthread_mutex_t pthread_mutex_t
#endif #endif
mutex; mutex;
bool inited_;
volatile bool locked; volatile bool locked;
}; };

View File

@@ -48,7 +48,7 @@ public:
void enqueue(const Tin &v) {enqueue(v, 0);} void enqueue(const Tin &v) {enqueue(v, 0);}
EVENT_HANDLER2(void, enqueue, const Tin &, v, bool *, overload) { EVENT_HANDLER2(void, enqueue, const Tin &, v, bool *, overload) {
mutex.lock(); mutex.lock();
// piCout << "enque" << max_size << in.size(); //piCoutObj << "enque" << overload;//max_size << in.size();
if (max_size == 0 || in.size() < max_size) { if (max_size == 0 || in.size() < max_size) {
in.enqueue(v); in.enqueue(v);
if (overload) *overload = false; if (overload) *overload = false;
@@ -119,15 +119,17 @@ protected:
private: private:
void begin() {cnt = 0;} void begin() {cnt = 0;}
void run() { void run() {
//piCoutObj << "run ...";
mutex.lock(); mutex.lock();
if (in.isEmpty()) { if (in.isEmpty()) {
mutex.unlock(); mutex.unlock();
piMSleep(10); piMSleep(10);
//piCoutObj << "run in empty";
return; return;
} }
if (next_overload && wait_next_pipe) { if (next_overload && wait_next_pipe) {
mutex.unlock(); mutex.unlock();
piCoutObj << &next_overload; //piCoutObj << "wait" << &next_overload;
calculated(last, &next_overload); calculated(last, &next_overload);
piMSleep(10); piMSleep(10);
} else { } else {
@@ -141,10 +143,11 @@ private:
mutex_l.unlock(); mutex_l.unlock();
cnt++; cnt++;
// next_overload = true; // next_overload = true;
piCoutObj << &next_overload; //piCoutObj << "calc ok" << &next_overload;
calculated(r, &next_overload); calculated(r, &next_overload);
} }
} }
//piCoutObj << "run ok";
} }
PIMutex mutex; PIMutex mutex;
PIMutex mutex_l; PIMutex mutex_l;