18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.

This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
peri4
2013-03-18 12:07:44 +04:00
parent cfc5eed75e
commit 66c53a27fc
72 changed files with 4407 additions and 960 deletions

63
pithread.cpp Executable file → Normal file
View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
Thread
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -18,6 +18,17 @@
*/
#include "pithread.h"
#include "pisystemtests.h"
void piUSleep(int usecs) {
#ifdef WINDOWS
if (usecs > 0) Sleep(usecs / 1000);
#else
usecs -= PISystemTests::usleep_offset_us;
if (usecs > 0) usleep(usecs);
#endif
}
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
@@ -103,7 +114,7 @@ void PIThread::terminate(bool hard) {
if (thread == 0) return;
running = false;
#ifndef WINDOWS
if (hard) kill(thread, SIGKILL);
if (hard) kill((ullong)thread, SIGKILL);
else pthread_cancel(thread);
#else
CloseHandle(thread);
@@ -114,20 +125,20 @@ void PIThread::terminate(bool hard) {
void * PIThread::thread_function(void * t) {
PIThread * ct = (PIThread * )t;
ct->running = true;
ct->begin();
raiseEvent(ct, "started");
while (!ct->terminating) {
if (ct->lockRun) ct->mutex_.lock();
ct->run();
if (ct->ret_func != 0) ct->ret_func(ct->data_);
if (ct->lockRun) ct->mutex_.unlock();
if (ct->timer > 0) msleep(ct->timer);
PIThread & ct = *((PIThread * )t);
ct.running = true;
ct.begin();
ct.started();
while (!ct.terminating) {
if (ct.lockRun) ct.mutex_.lock();
ct.run();
if (ct.ret_func != 0) ct.ret_func(ct.data_);
if (ct.lockRun) ct.mutex_.unlock();
if (ct.timer > 0) msleep(ct.timer);
}
raiseEvent(ct, "stopped");
ct->end();
ct->running = false;
ct.stopped();
ct.end();
ct.running = false;
//cout << "thread " << t << " exiting ... " << endl;
#ifndef WINDOWS
pthread_exit(0);
@@ -139,17 +150,17 @@ void * PIThread::thread_function(void * t) {
void * PIThread::thread_function_once(void * t) {
PIThread * ct = (PIThread * )t;
ct->running = true;
ct->begin();
raiseEvent(ct, "started");
if (ct->lockRun) ct->mutex_.lock();
ct->run();
if (ct->ret_func != 0) ct->ret_func(ct->data_);
if (ct->lockRun) ct->mutex_.unlock();
raiseEvent(ct, "stopped");
ct->end();
ct->running = false;
PIThread & ct = *((PIThread * )t);
ct.running = true;
ct.begin();
ct.started();
if (ct.lockRun) ct.mutex_.lock();
ct.run();
if (ct.ret_func != 0) ct.ret_func(ct.data_);
if (ct.lockRun) ct.mutex_.unlock();
ct.stopped();
ct.end();
ct.running = false;
//cout << "thread " << t << " exiting ... " << endl;
#ifndef WINDOWS
pthread_exit(0);