git-svn-id: svn://db.shs.com.ru/pip@184 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "pithread.h"
|
||||
#include "pisystemtests.h"
|
||||
#include "piintrospection.h"
|
||||
#include <signal.h>
|
||||
#ifdef WINDOWS
|
||||
void __PISetTimerResolution() {if (setTimerResolutionAddr == NULL) return; ULONG ret; setTimerResolutionAddr(1, TRUE, &ret);}
|
||||
@@ -77,6 +78,7 @@ end();
|
||||
|
||||
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
tid_ = -1;
|
||||
thread = 0;
|
||||
data_ = data;
|
||||
ret_func = func;
|
||||
@@ -89,6 +91,7 @@ PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay)
|
||||
|
||||
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
tid_ = -1;
|
||||
thread = 0;
|
||||
ret_func = 0;
|
||||
terminating = running_ = lockRun = false;
|
||||
@@ -130,11 +133,13 @@ bool PIThread::start(int timer_delay) {
|
||||
//piCout << "pthread_create" << thread;
|
||||
pthread_attr_destroy(&attr);
|
||||
if (ret == 0) {
|
||||
tid_ = thread;
|
||||
#else
|
||||
if (thread != 0) CloseHandle(thread);
|
||||
thread = (void *)_beginthreadex(0, 0, thread_function, this, 0, 0);
|
||||
// thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0);
|
||||
if (thread != 0) {
|
||||
tid_ = GetThreadId(thread);
|
||||
#endif
|
||||
setPriority(priority_);
|
||||
running_ = true;
|
||||
@@ -161,11 +166,13 @@ bool PIThread::startOnce() {
|
||||
//piCout << "pthread_create" << thread;
|
||||
pthread_attr_destroy(&attr);
|
||||
if (ret == 0) {
|
||||
tid_ = thread;
|
||||
#else
|
||||
if (thread != 0) CloseHandle(thread);
|
||||
thread = (void *)_beginthreadex(0, 0, thread_function_once, this, 0, 0);
|
||||
// thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function_once, this, 0, 0);
|
||||
if (thread != 0) {
|
||||
tid_ = GetThreadId(thread);
|
||||
#endif
|
||||
setPriority(priority_);
|
||||
running_ = true;
|
||||
@@ -180,7 +187,9 @@ bool PIThread::startOnce() {
|
||||
|
||||
void PIThread::terminate() {
|
||||
if (thread == 0) return;
|
||||
PIINTROSPECTION->unregisterThread(tid());
|
||||
terminating = running_ = false;
|
||||
tid_ = -1;
|
||||
//piCout << "terminate" << thread;
|
||||
#ifndef WINDOWS
|
||||
# ifdef ANDROID
|
||||
@@ -210,6 +219,7 @@ __THREAD_FUNC__ PIThread::thread_function(void * t) {
|
||||
__PISetTimerResolution();
|
||||
#endif
|
||||
PIThread & ct = *((PIThread * )t);
|
||||
PIINTROSPECTION->registerThread(ct.tid(), ct.priority(), ct.name());
|
||||
ct.running_ = true;
|
||||
if (ct.lockRun) ct.mutex_.lock();
|
||||
ct.begin();
|
||||
@@ -238,8 +248,10 @@ __THREAD_FUNC__ PIThread::thread_function(void * t) {
|
||||
ct.end();
|
||||
if (ct.lockRun) ct.mutex_.unlock();
|
||||
ct.terminating = ct.running_ = false;
|
||||
ct.tid_ = -1;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
//piCout << "pthread_exit" << ct.thread;
|
||||
PIINTROSPECTION->unregisterThread(ct.tid());
|
||||
#ifndef WINDOWS
|
||||
pthread_detach(ct.thread);
|
||||
ct.thread = 0;
|
||||
@@ -264,6 +276,7 @@ __THREAD_FUNC__ PIThread::thread_function_once(void * t) {
|
||||
__PISetTimerResolution();
|
||||
#endif
|
||||
PIThread & ct = *((PIThread * )t);
|
||||
PIINTROSPECTION->registerThread(ct.tid(), ct.priority(), ct.name());
|
||||
ct.running_ = true;
|
||||
ct.begin();
|
||||
ct.started();
|
||||
@@ -274,8 +287,10 @@ __THREAD_FUNC__ PIThread::thread_function_once(void * t) {
|
||||
ct.stopped();
|
||||
ct.end();
|
||||
ct.terminating = ct.running_ = false;
|
||||
ct.tid_ = -1;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
//piCout << "pthread_exit" << ct.thread;
|
||||
PIINTROSPECTION->unregisterThread(ct.tid());
|
||||
#ifndef WINDOWS
|
||||
pthread_detach(ct.thread);
|
||||
ct.thread = 0;
|
||||
|
||||
@@ -101,8 +101,11 @@ public:
|
||||
EVENT_HANDLER0(void, lock) {mutex_.lock();}
|
||||
EVENT_HANDLER0(void, unlock) {mutex_.unlock();}
|
||||
|
||||
//! \brief Return internal mutex
|
||||
//! \brief Returns internal mutex
|
||||
PIMutex & mutex() {return mutex_;}
|
||||
|
||||
//! \brief Returns thread ID
|
||||
int tid() const {return tid_;}
|
||||
|
||||
EVENT(started)
|
||||
EVENT(stopped)
|
||||
@@ -197,7 +200,7 @@ protected:
|
||||
virtual void end() {;}
|
||||
|
||||
volatile bool terminating, running_, lockRun;
|
||||
int delay_, policy_;
|
||||
int delay_, policy_, tid_;
|
||||
void * data_;
|
||||
PIMutex mutex_;
|
||||
PITimeMeasurer tmf_, tms_, tmr_;
|
||||
|
||||
Reference in New Issue
Block a user