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

This commit is contained in:
2017-05-02 12:40:56 +00:00
parent 08052ac9e2
commit 2bb1ab82a9
8 changed files with 162 additions and 117 deletions

View File

@@ -23,12 +23,16 @@
#include "piintrospection_proxy.h"
#include <signal.h>
#ifdef WINDOWS
extern PINtSetTimerResolution setTimerResolutionAddr;
void __PISetTimerResolution() {if (setTimerResolutionAddr == NULL) return; ULONG ret; setTimerResolutionAddr(1, TRUE, &ret);}
# define __THREAD_FUNC_RET__ uint __stdcall
#else
# define __THREAD_FUNC_RET__ void*
#endif
#if defined(MAC_OS) || defined(BLACKBERRY)
# include <pthread.h>
#endif
__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;}
/*! \class PIThread
* \brief Thread class
@@ -229,111 +233,6 @@ void PIThread::terminate() {
}
__THREAD_FUNC__ PIThread::thread_function(void * t) {
#ifndef WINDOWS
# ifndef ANDROID
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
# endif
#else
__PISetTimerResolution();
#endif
PIThread & ct = *((PIThread * )t);
#ifdef WINDOWS
ct.tid_ = GetCurrentThreadId();
#endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
ct.running_ = true;
if (ct.lockRun) ct.mutex_.lock();
ct.begin();
if (ct.lockRun) ct.mutex_.unlock();
ct.started();
while (!ct.terminating) {
ct.maybeCallQueuedEvents();
if (ct.lockRun) ct.mutex_.lock();
//piCout << "thread" << ct.name() << "...";
ct.run();
//piCout << "thread" << ct.name() << "done";
//printf("thread %p tick\n", &ct);
if (ct.ret_func != 0) ct.ret_func(ct.data_);
if (ct.lockRun) ct.mutex_.unlock();
if (ct.delay_ > 0) {
ct.tmr_.reset();
double sl(0.);
while (1) {
sl = piMind(ct.delay_ - ct.tmr_.elapsed_m(), 2.);
if (sl <= 0.) break;
piMSleep(sl);
if (ct.terminating)
break;
}
}
}
ct.stopped();
if (ct.lockRun) ct.mutex_.lock();
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.__privateinitializer__.p)->thread;
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);
(ct.__privateinitializer__.p)->thread = 0;
#endif
#ifndef WINDOWS
pthread_exit(0);
#else
_endthreadex(0);
//ExitThread(0);
#endif
return 0;
}
__THREAD_FUNC__ PIThread::thread_function_once(void * t) {
#ifndef WINDOWS
# ifndef ANDROID
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
# endif
#else
__PISetTimerResolution();
#endif
PIThread & ct = *((PIThread * )t);
#ifdef WINDOWS
ct.tid_ = GetCurrentThreadId();
#endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
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.terminating = ct.running_ = false;
ct.tid_ = -1;
//cout << "thread " << t << " exiting ... " << endl;
//piCout << "pthread_exit" << (ct.__privateinitializer__.p)->thread;
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);
(ct.__privateinitializer__.p)->thread = 0;
#endif
#ifndef WINDOWS
pthread_exit(0);
#else
_endthreadex(0);
//ExitThread(0);
#endif
return 0;
}
int PIThread::priority2System(PIThread::Priority p) {
switch (p) {
# ifdef QNX
@@ -412,3 +311,113 @@ bool PIThread::waitForStart(int timeout_msecs) {
msleep(1);
return tms_.elapsed_m() < timeout_msecs;
}
void PIThread::__thread_func__(void * t) {
#ifndef WINDOWS
# ifndef ANDROID
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
# endif
#else
//__PISetTimerResolution();
#endif
PIThread & ct = *((PIThread * )t);
#ifdef WINDOWS
ct.tid_ = GetCurrentThreadId();
#endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
ct.running_ = true;
if (ct.lockRun) ct.mutex_.lock();
ct.begin();
if (ct.lockRun) ct.mutex_.unlock();
ct.started();
while (!ct.terminating) {
ct.maybeCallQueuedEvents();
if (ct.lockRun) ct.mutex_.lock();
//piCout << "thread" << ct.name() << "...";
ct.run();
//piCout << "thread" << ct.name() << "done";
//printf("thread %p tick\n", &ct);
if (ct.ret_func != 0) ct.ret_func(ct.data_);
if (ct.lockRun) ct.mutex_.unlock();
if (ct.delay_ > 0) {
ct.tmr_.reset();
double sl(0.);
while (1) {
sl = piMind(ct.delay_ - ct.tmr_.elapsed_m(), 2.);
#ifdef WINDOWS
/*if (sl <= 1. && sl >= 0.) {
piMSleep(1.);
continue;
}*/
#endif
//printf("%f %f %f\n", double(ct.delay_), ct.tmr_.elapsed_m(), sl);
if (sl <= 0.) break;
piMSleep(sl);
if (ct.terminating)
break;
}
}
}
ct.stopped();
if (ct.lockRun) ct.mutex_.lock();
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.__privateinitializer__.p)->thread;
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);
(ct.__privateinitializer__.p)->thread = 0;
#endif
#ifndef WINDOWS
pthread_exit(0);
#else
_endthreadex(0);
//ExitThread(0);
#endif
}
void PIThread::__thread_func_once__(void * t) {
#ifndef WINDOWS
# ifndef ANDROID
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
# endif
#else
//__PISetTimerResolution();
#endif
PIThread & ct = *((PIThread * )t);
#ifdef WINDOWS
ct.tid_ = GetCurrentThreadId();
#endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
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.terminating = ct.running_ = false;
ct.tid_ = -1;
//cout << "thread " << t << " exiting ... " << endl;
//piCout << "pthread_exit" << (ct.__privateinitializer__.p)->thread;
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
#ifndef WINDOWS
pthread_detach((ct.__privateinitializer__.p)->thread);
(ct.__privateinitializer__.p)->thread = 0;
#endif
#ifndef WINDOWS
pthread_exit(0);
#else
_endthreadex(0);
//ExitThread(0);
#endif
}