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

@@ -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);
}

View File

@@ -356,9 +356,9 @@ private:
if (as == piv_rsize) return;
//cout << std::hex << " ![("<<this<<")realloc " << piv_data << " data ... <\n" << endl;
T * p_d = (T*)(realloc(piv_data, as*sizeof(T)));
if (p_d)
piv_data = p_d;
T * p_d = (T*)(realloc(piv_data, as*sizeof(T)));
assert(p_d);
piv_data = p_d;
//zeroRaw(&(piv_data[os]), as - os);
piv_rsize = as;
//cout << std::hex << " > (new 0x" << (llong)piv_data << ") ok]!" << endl;

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

View File

@@ -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);
};

View File

@@ -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;

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;
};

View File

@@ -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;