git-svn-id: svn://db.shs.com.ru/pip@400 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
413
src_main/thread/pithread.cpp
Executable file
413
src_main/thread/pithread.cpp
Executable file
@@ -0,0 +1,413 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Thread
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pithread.h"
|
||||
#include "pisystemtests.h"
|
||||
#include "piintrospection_proxy.h"
|
||||
#include <signal.h>
|
||||
#ifdef WINDOWS
|
||||
extern PINtSetTimerResolution setTimerResolutionAddr;
|
||||
void __PISetTimerResolution() {if (setTimerResolutionAddr == NULL) return; ULONG ret; setTimerResolutionAddr(1, TRUE, &ret);}
|
||||
#endif
|
||||
#if defined(MAC_OS) || defined(BLACKBERRY)
|
||||
# include <pthread.h>
|
||||
#endif
|
||||
|
||||
/*! \class PIThread
|
||||
* \brief Thread class
|
||||
* \details This class allow you exec your code in separate thread.
|
||||
*
|
||||
* \section PIThread_sec0 Synopsis
|
||||
* Multithreading allow you to write program which will be executed
|
||||
* in several threads simultaneously. This trend allow you to use all
|
||||
* cores of modern processors, but there are many dangers.
|
||||
*
|
||||
* This class provide virtual functions \a begin(), \a run() and \a end(),
|
||||
* which describes start, execution and finish work of some process.
|
||||
* These functions executes in \b separate thread. When you execute
|
||||
* \a start(), %PIThread create separate system thread and sequentially
|
||||
* executes function \a begin(), \a run() and \a end(). You can
|
||||
* reimplement each function and write your own code to execute.
|
||||
* Scheme of functions executing:
|
||||
\code{.cpp}
|
||||
begin();
|
||||
event started();
|
||||
while (isRunning()) {
|
||||
run();
|
||||
ThreadFunc();
|
||||
msleep(timer_delay);
|
||||
}
|
||||
event stopped();
|
||||
end();
|
||||
\endcode
|
||||
* Unlike from directly using "pthread" or some similar you doesn`t need
|
||||
* to write your own main thread cycle and sleep at every cycle end.
|
||||
* %PIThread make it for you, and your job is to set sleep value from
|
||||
* contructor or when starting thread, and reimplement \a begin(), \a run()
|
||||
* and \a end() functions.
|
||||
*
|
||||
* \section PIThread_sec1 Using without subclassing
|
||||
* You can use %PIThread without subclassing by using "ThreadFunc" pointer
|
||||
* that can be set from constructor or by overloaded function \a start(ThreadFunc func, int timer_delay).
|
||||
* If "func" if not null this function will be executed as \a run(). ThreadFunc is any static
|
||||
* function with format void func(void * data). "Data" is custom data set from constructor or
|
||||
* with function \a setData(). \n Also you can connect to event \a started(), but
|
||||
* in this case you should to white your thread main cycle, because this event raised only one time.
|
||||
*
|
||||
* \section PIThread_sec2 Locking
|
||||
* %PIThread has inrternal mutex that can be locked and unlocked every \a run() if you set this flag
|
||||
* with function \a needLockRun(bool). Also you can access to this mutex by functions \a lock(), \a unlock()
|
||||
* and \a mutex(). Using this functions together with needLockRun(true) can guarantee one-thread access to
|
||||
* some data.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
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;
|
||||
PRIVATE->thread = 0;
|
||||
data_ = data;
|
||||
ret_func = func;
|
||||
terminating = running_ = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
delay_ = timer_delay;
|
||||
if (startNow) start(timer_delay);
|
||||
}
|
||||
|
||||
|
||||
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
tid_ = -1;
|
||||
PRIVATE->thread = 0;
|
||||
ret_func = 0;
|
||||
terminating = running_ = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
delay_ = timer_delay;
|
||||
if (startNow) start(timer_delay);
|
||||
}
|
||||
|
||||
|
||||
PIThread::~PIThread() {
|
||||
piMonitor.threads--;
|
||||
if (!running_ || PRIVATE->thread == 0) return;
|
||||
#ifndef WINDOWS
|
||||
# ifdef ANDROID
|
||||
pthread_kill(PRIVATE->thread, SIGTERM);
|
||||
# else
|
||||
pthread_cancel(PRIVATE->thread);
|
||||
# endif
|
||||
#else
|
||||
TerminateThread(PRIVATE->thread, 0);
|
||||
CloseHandle(PRIVATE->thread);
|
||||
#endif
|
||||
terminating = running_ = false;
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::start(int timer_delay) {
|
||||
if (running_) return false;
|
||||
terminating = running_ = false;
|
||||
delay_ = timer_delay;
|
||||
#ifndef WINDOWS
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
# ifndef ANDROID
|
||||
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
|
||||
# endif
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
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(PRIVATE->thread, (__uint64_t*)&tid_);
|
||||
# else
|
||||
tid_ = PRIVATE->thread;
|
||||
# endif
|
||||
#else
|
||||
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 {
|
||||
PRIVATE->thread = 0;
|
||||
piCoutObj << "Error: Can`t start new thread:" << errorString();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::startOnce() {
|
||||
if (running_) return false;
|
||||
terminating = running_ = false;
|
||||
#ifndef WINDOWS
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
# ifndef ANDROID
|
||||
//pthread_attr_setschedparam(&attr, &(PRIVATE->sparam));
|
||||
# endif
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
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(PRIVATE->thread, (__uint64_t*)&tid_);
|
||||
# else
|
||||
tid_ = PRIVATE->thread;
|
||||
# endif
|
||||
#else
|
||||
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 (PRIVATE->thread != 0) {
|
||||
#endif
|
||||
setPriority(priority_);
|
||||
running_ = true;
|
||||
return true;
|
||||
} else {
|
||||
PRIVATE->thread = 0;
|
||||
piCoutObj << "Error: Can`t start new thread:" << errorString();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PIThread::terminate() {
|
||||
if (PRIVATE->thread == 0) return;
|
||||
PIINTROSPECTION_UNREGISTER_THREAD(tid());
|
||||
terminating = running_ = false;
|
||||
tid_ = -1;
|
||||
//piCout << "terminate" << thread;
|
||||
#ifndef WINDOWS
|
||||
# ifdef ANDROID
|
||||
pthread_kill(PRIVATE->thread, SIGTERM);
|
||||
# else
|
||||
//pthread_kill(PRIVATE->thread, SIGKILL);
|
||||
//void * ret(0);
|
||||
pthread_cancel(PRIVATE->thread);
|
||||
//pthread_join(PRIVATE->thread, &ret);
|
||||
# endif
|
||||
#else
|
||||
TerminateThread(PRIVATE->thread, 0);
|
||||
CloseHandle(PRIVATE->thread);
|
||||
#endif
|
||||
PRIVATE->thread = 0;
|
||||
end();
|
||||
}
|
||||
|
||||
|
||||
__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) {
|
||||
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
|
||||
case piLowerst: return 8;
|
||||
case piLow: return 9;
|
||||
case piNormal: return 10;
|
||||
case piHigh: return 11;
|
||||
case piHighest: return 12;
|
||||
# else
|
||||
# ifdef WINDOWS
|
||||
case piLowerst: return -2;
|
||||
case piLow: return -1;
|
||||
case piNormal: return 0;
|
||||
case piHigh: return 1;
|
||||
case piHighest: return 2;
|
||||
# else
|
||||
case piLowerst: return 2;
|
||||
case piLow: return 1;
|
||||
case piNormal: return 0;
|
||||
case piHigh: return -1;
|
||||
case piHighest: return -2;
|
||||
# endif
|
||||
# endif
|
||||
default: return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void PIThread::setPriority(PIThread::Priority prior) {
|
||||
priority_ = prior;
|
||||
#ifndef WINDOWS
|
||||
if (!running_ || (PRIVATE->thread == 0)) return;
|
||||
//piCout << "setPriority" << PRIVATE->thread;
|
||||
policy_ = 0;
|
||||
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(PRIVATE->thread, policy_, &(PRIVATE->sparam));
|
||||
#else
|
||||
if (!running_ || (PRIVATE->thread == 0)) return;
|
||||
SetThreadPriority(PRIVATE->thread, priority2System(priority_));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::waitForFinish(int timeout_msecs) {
|
||||
if (!running_) return true;
|
||||
if (timeout_msecs < 0) {
|
||||
while (running_)
|
||||
msleep(1);
|
||||
return true;
|
||||
}
|
||||
tmf_.reset();
|
||||
while (running_ && tmf_.elapsed_m() < timeout_msecs)
|
||||
msleep(1);
|
||||
return tmf_.elapsed_m() < timeout_msecs;
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::waitForStart(int timeout_msecs) {
|
||||
if (running_) return true;
|
||||
if (timeout_msecs < 0) {
|
||||
while (!running_)
|
||||
msleep(1);
|
||||
return true;
|
||||
}
|
||||
tms_.reset();
|
||||
while (!running_ && tms_.elapsed_m() < timeout_msecs)
|
||||
msleep(1);
|
||||
return tms_.elapsed_m() < timeout_msecs;
|
||||
}
|
||||
Reference in New Issue
Block a user