|
|
|
|
@@ -26,7 +26,7 @@
|
|
|
|
|
extern PINtSetTimerResolution setTimerResolutionAddr;
|
|
|
|
|
void __PISetTimerResolution() {if (setTimerResolutionAddr == NULL) return; ULONG ret; setTimerResolutionAddr(1, TRUE, &ret);}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef MAC_OS
|
|
|
|
|
#if defined(MAC_OS) || defined(BLACKBERRY)
|
|
|
|
|
# include <pthread.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
@@ -80,10 +80,20 @@ end();
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PRIVATE_DEFINITION_START(PIThread)
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
pthread_t thread;
|
|
|
|
|
sched_param sparam;
|
|
|
|
|
#else
|
|
|
|
|
void * thread;
|
|
|
|
|
#endif
|
|
|
|
|
PRIVATE_DEFINITION_END(PIThread)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
|
|
|
|
|
piMonitor.threads++;
|
|
|
|
|
tid_ = -1;
|
|
|
|
|
thread = 0;
|
|
|
|
|
PRIVATE->thread = 0;
|
|
|
|
|
data_ = data;
|
|
|
|
|
ret_func = func;
|
|
|
|
|
terminating = running_ = lockRun = false;
|
|
|
|
|
@@ -96,7 +106,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;
|
|
|
|
|
PRIVATE->thread = 0;
|
|
|
|
|
ret_func = 0;
|
|
|
|
|
terminating = running_ = lockRun = false;
|
|
|
|
|
priority_ = piNormal;
|
|
|
|
|
@@ -107,16 +117,16 @@ PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
|
|
|
|
|
|
|
|
|
PIThread::~PIThread() {
|
|
|
|
|
piMonitor.threads--;
|
|
|
|
|
if (!running_ || thread == 0) return;
|
|
|
|
|
if (!running_ || PRIVATE->thread == 0) return;
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
# ifdef ANDROID
|
|
|
|
|
pthread_kill(thread, SIGTERM);
|
|
|
|
|
pthread_kill(PRIVATE->thread, SIGTERM);
|
|
|
|
|
# else
|
|
|
|
|
pthread_cancel(thread);
|
|
|
|
|
pthread_cancel(PRIVATE->thread);
|
|
|
|
|
# endif
|
|
|
|
|
#else
|
|
|
|
|
TerminateThread(thread, 0);
|
|
|
|
|
CloseHandle(thread);
|
|
|
|
|
TerminateThread(PRIVATE->thread, 0);
|
|
|
|
|
CloseHandle(PRIVATE->thread);
|
|
|
|
|
#endif
|
|
|
|
|
terminating = running_ = false;
|
|
|
|
|
}
|
|
|
|
|
@@ -130,29 +140,29 @@ bool PIThread::start(int timer_delay) {
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
|
# ifndef ANDROID
|
|
|
|
|
//pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
|
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
|
|
|
|
|
# endif
|
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
int ret = pthread_create(&thread, &attr, thread_function, this);
|
|
|
|
|
//piCout << "pthread_create" << thread;
|
|
|
|
|
int ret = pthread_create(&PRIVATE->thread, &attr, thread_function, this);
|
|
|
|
|
//piCout << "pthread_create" << PRIVATE->thread;
|
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
# ifdef MAC_OS
|
|
|
|
|
pthread_threadid_np(thread, (__uint64_t*)&tid_);
|
|
|
|
|
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
|
|
|
|
|
# else
|
|
|
|
|
tid_ = thread;
|
|
|
|
|
tid_ = PRIVATE->thread;
|
|
|
|
|
# endif
|
|
|
|
|
#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) {
|
|
|
|
|
if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread);
|
|
|
|
|
PRIVATE->thread = (void *)_beginthreadex(0, 0, thread_function, this, 0, 0);
|
|
|
|
|
// PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0);
|
|
|
|
|
if (PRIVATE->thread != 0) {
|
|
|
|
|
#endif
|
|
|
|
|
setPriority(priority_);
|
|
|
|
|
running_ = true;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
thread = 0;
|
|
|
|
|
PRIVATE->thread = 0;
|
|
|
|
|
piCoutObj << "Error: Can`t start new thread:" << errorString();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
@@ -166,29 +176,29 @@ bool PIThread::startOnce() {
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
|
# ifndef ANDROID
|
|
|
|
|
//pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
|
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
|
|
|
|
|
# endif
|
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
int ret = pthread_create(&thread, &attr, thread_function_once, this);
|
|
|
|
|
//piCout << "pthread_create" << thread;
|
|
|
|
|
int ret = pthread_create(&(PRIVATE->thread), &attr, thread_function_once, this);
|
|
|
|
|
//piCout << "pthread_create" << PRIVATE->thread;
|
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
# ifdef MAC_OS
|
|
|
|
|
pthread_threadid_np(thread, (__uint64_t*)&tid_);
|
|
|
|
|
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
|
|
|
|
|
# else
|
|
|
|
|
tid_ = thread;
|
|
|
|
|
tid_ = PRIVATE->thread;
|
|
|
|
|
# endif
|
|
|
|
|
#else
|
|
|
|
|
if (thread != 0) CloseHandle(thread);
|
|
|
|
|
thread = (void *)_beginthreadex(0, 0, thread_function_once, this, 0, 0);
|
|
|
|
|
if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread);
|
|
|
|
|
PRIVATE->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) {
|
|
|
|
|
if (PRIVATE->thread != 0) {
|
|
|
|
|
#endif
|
|
|
|
|
setPriority(priority_);
|
|
|
|
|
running_ = true;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
thread = 0;
|
|
|
|
|
PRIVATE->thread = 0;
|
|
|
|
|
piCoutObj << "Error: Can`t start new thread:" << errorString();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
@@ -196,25 +206,25 @@ bool PIThread::startOnce() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIThread::terminate() {
|
|
|
|
|
if (thread == 0) return;
|
|
|
|
|
if (PRIVATE->thread == 0) return;
|
|
|
|
|
PIINTROSPECTION_UNREGISTER_THREAD(tid());
|
|
|
|
|
terminating = running_ = false;
|
|
|
|
|
tid_ = -1;
|
|
|
|
|
//piCout << "terminate" << thread;
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
# ifdef ANDROID
|
|
|
|
|
pthread_kill(thread, SIGTERM);
|
|
|
|
|
pthread_kill(PRIVATE->thread, SIGTERM);
|
|
|
|
|
# else
|
|
|
|
|
//pthread_kill(thread, SIGKILL);
|
|
|
|
|
//pthread_kill(PRIVATE->thread, SIGKILL);
|
|
|
|
|
//void * ret(0);
|
|
|
|
|
pthread_cancel(thread);
|
|
|
|
|
//pthread_join(thread, &ret);
|
|
|
|
|
pthread_cancel(PRIVATE->thread);
|
|
|
|
|
//pthread_join(PRIVATE->thread, &ret);
|
|
|
|
|
# endif
|
|
|
|
|
#else
|
|
|
|
|
TerminateThread(thread, 0);
|
|
|
|
|
CloseHandle(thread);
|
|
|
|
|
TerminateThread(PRIVATE->thread, 0);
|
|
|
|
|
CloseHandle(PRIVATE->thread);
|
|
|
|
|
#endif
|
|
|
|
|
thread = 0;
|
|
|
|
|
PRIVATE->thread = 0;
|
|
|
|
|
end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -265,11 +275,11 @@ __THREAD_FUNC__ PIThread::thread_function(void * t) {
|
|
|
|
|
ct.terminating = ct.running_ = false;
|
|
|
|
|
ct.tid_ = -1;
|
|
|
|
|
//cout << "thread " << t << " exiting ... " << endl;
|
|
|
|
|
//piCout << "pthread_exit" << ct.thread;
|
|
|
|
|
//piCout << "pthread_exit" << ct.PRIVATE->thread;
|
|
|
|
|
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
pthread_detach(ct.thread);
|
|
|
|
|
ct.thread = 0;
|
|
|
|
|
pthread_detach(ct.PRIVATE->thread);
|
|
|
|
|
ct.PRIVATE->thread = 0;
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
pthread_exit(0);
|
|
|
|
|
@@ -307,11 +317,11 @@ __THREAD_FUNC__ PIThread::thread_function_once(void * t) {
|
|
|
|
|
ct.terminating = ct.running_ = false;
|
|
|
|
|
ct.tid_ = -1;
|
|
|
|
|
//cout << "thread " << t << " exiting ... " << endl;
|
|
|
|
|
//piCout << "pthread_exit" << ct.thread;
|
|
|
|
|
//piCout << "pthread_exit" << ct.PRIVATE->thread;
|
|
|
|
|
PIINTROSPECTION_UNREGISTER_THREAD(ct.tid());
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
pthread_detach(ct.thread);
|
|
|
|
|
ct.thread = 0;
|
|
|
|
|
pthread_detach(ct.PRIVATE->thread);
|
|
|
|
|
ct.PRIVATE->thread = 0;
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
pthread_exit(0);
|
|
|
|
|
@@ -355,22 +365,22 @@ int PIThread::priority2System(PIThread::Priority p) {
|
|
|
|
|
void PIThread::setPriority(PIThread::Priority prior) {
|
|
|
|
|
priority_ = prior;
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
if (!running_ || (thread == 0)) return;
|
|
|
|
|
//piCout << "setPriority" << thread;
|
|
|
|
|
if (!running_ || (PRIVATE->thread == 0)) return;
|
|
|
|
|
//piCout << "setPriority" << PRIVATE->thread;
|
|
|
|
|
policy_ = 0;
|
|
|
|
|
memset(&sparam, 0, sizeof(sparam));
|
|
|
|
|
pthread_getschedparam(thread, &policy_, &sparam);
|
|
|
|
|
sparam.
|
|
|
|
|
memset(&(PRIVATE->sparam), 0, sizeof(PRIVATE->sparam));
|
|
|
|
|
pthread_getschedparam(PRIVATE->thread, &policy_, &(PRIVATE->sparam));
|
|
|
|
|
PRIVATE->sparam.
|
|
|
|
|
# ifndef LINUX
|
|
|
|
|
sched_priority
|
|
|
|
|
# else
|
|
|
|
|
__sched_priority
|
|
|
|
|
# endif
|
|
|
|
|
= priority2System(priority_);
|
|
|
|
|
pthread_setschedparam(thread, policy_, &sparam);
|
|
|
|
|
pthread_setschedparam(PRIVATE->thread, policy_, &(PRIVATE->sparam));
|
|
|
|
|
#else
|
|
|
|
|
if (!running_ || (thread == 0)) return;
|
|
|
|
|
SetThreadPriority(thread, priority2System(priority_));
|
|
|
|
|
if (!running_ || (PRIVATE->thread == 0)) return;
|
|
|
|
|
SetThreadPriority(PRIVATE->thread, priority2System(priority_));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|