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

This commit is contained in:
2017-04-17 12:04:12 +00:00
parent f76510e74a
commit 2c68e66568
30 changed files with 380 additions and 553 deletions

View File

@@ -35,10 +35,7 @@
* */
#include "pimutex.h"
#ifdef WINDOWS
# include <windef.h>
# include <winbase.h>
#endif
#include "piincludes_p.h"
#ifdef BLACKBERRY
# include <pthread.h>
#endif
@@ -55,6 +52,9 @@ PRIVATE_DEFINITION_END(PIMutex)
PIMutex::PIMutex(): inited_(false) {
#ifdef WINDOWS
PRIVATE->mutex = 0;
#endif
init();
}

View File

@@ -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
}

View File

@@ -28,9 +28,6 @@
#include "piinit.h"
#include "pimutex.h"
#include "piobject.h"
#ifdef BLACKBERRY
# include <pthread.h>
#endif
#ifdef WINDOWS
# define __THREAD_FUNC__ uint __stdcall
#else
@@ -207,13 +204,7 @@ protected:
PITimeMeasurer tmf_, tms_, tmr_;
PIThread::Priority priority_;
ThreadFunc ret_func;
#ifndef WINDOWS
pthread_t thread;
sched_param sparam;
#else
void * thread;
#endif
PRIVATE_DECLARATION
};
#endif // PITHREAD_H

View File

@@ -23,5 +23,7 @@
#include "pimutex.h"
#include "pithread.h"
#include "pitimer.h"
#include "pipipelinethread.h"
#include "pigrabberbase.h"
#endif // PITHREADMODULE_H