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

This commit is contained in:
2017-06-21 14:00:45 +00:00
parent 6ac45691cc
commit ed1a89508a
7 changed files with 253 additions and 18 deletions

View File

@@ -33,6 +33,8 @@
__THREAD_FUNC_RET__ thread_function(void * t) {PIThread::__thread_func__(t); return 0;}
__THREAD_FUNC_RET__ thread_function_once(void * t) {PIThread::__thread_func_once__(t); return 0;}
#define REGISTER_THREAD(t) __PIThreadCollection::instance()->registerThread(t)
#define UNREGISTER_THREAD(t) __PIThreadCollection::instance()->unregisterThread(t)
/*! \class PIThread
* \brief Thread class
@@ -84,6 +86,64 @@ end();
*/
__PIThreadCollection *__PIThreadCollection::instance() {
/*static CDCore * ret = new CDCore();
return ret;*/
return __PIThreadCollection_Initializer__::__instance__;
}
void __PIThreadCollection::registerThread(PIThread * t) {
lock();
if (!threads_.contains(t))
threads_ << t;
unlock();
}
void __PIThreadCollection::unregisterThread(PIThread * t) {
lock();
threads_.removeAll(t);
unlock();
}
PIVector<PIThread * > __PIThreadCollection::threads() const {
return threads_;
}
int __PIThreadCollection_Initializer__::count_(0);
__PIThreadCollection * __PIThreadCollection_Initializer__::__instance__(0);
__PIThreadCollection_Initializer__::__PIThreadCollection_Initializer__() {
count_++;
//piCout << "try create Core" << count_;
if (count_ > 1) return;
//piCout << "create Core";
__instance__ = new __PIThreadCollection();
}
__PIThreadCollection_Initializer__::~__PIThreadCollection_Initializer__() {
count_--;
//piCout << "try delete Core" << count_;
if (count_ > 0) return;
//piCout << "delete Core";
if (__instance__ != 0) {
delete __instance__;
__instance__ = 0;
}
}
PRIVATE_DEFINITION_START(PIThread)
#ifndef WINDOWS
pthread_t thread;
@@ -211,6 +271,7 @@ bool PIThread::startOnce() {
void PIThread::terminate() {
if (PRIVATE->thread == 0) return;
REGISTER_THREAD(this);
PIINTROSPECTION_UNREGISTER_THREAD(tid());
terminating = running_ = false;
tid_ = -1;
@@ -285,6 +346,11 @@ void PIThread::setPriority(PIThread::Priority prior) {
}
void * PIThread::handle() const {
return PRIVATE->thread;
}
bool PIThread::waitForFinish(int timeout_msecs) {
if (!running_) return true;
if (timeout_msecs < 0) {
@@ -327,6 +393,7 @@ void PIThread::__thread_func__(void * t) {
ct.tid_ = GetCurrentThreadId();
#endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
REGISTER_THREAD(&ct);
ct.running_ = true;
if (ct.lockRun) ct.mutex_.lock();
ct.begin();
@@ -368,6 +435,7 @@ void PIThread::__thread_func__(void * t) {
ct.tid_ = -1;
//cout << "thread " << t << " exiting ... " << endl;
//piCout << "pthread_exit" << (ct.__privateinitializer__.p)->thread;
UNREGISTER_THREAD(&ct);
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);
@@ -396,6 +464,7 @@ void PIThread::__thread_func_once__(void * t) {
ct.tid_ = GetCurrentThreadId();
#endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
REGISTER_THREAD(&ct);
ct.running_ = true;
ct.begin();
ct.started();
@@ -409,6 +478,7 @@ void PIThread::__thread_func_once__(void * t) {
ct.tid_ = -1;
//cout << "thread " << t << " exiting ... " << endl;
//piCout << "pthread_exit" << (ct.__privateinitializer__.p)->thread;
UNREGISTER_THREAD(&ct);
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);