9.10.2011 - stable backup commit
This commit is contained in:
58
pithread.cpp
58
pithread.cpp
@@ -8,38 +8,73 @@ PIThread::PIThread(bool startNow, int timer_delay) {
|
||||
if (startNow) start(timer_delay);
|
||||
}
|
||||
|
||||
#ifndef WINDOWS
|
||||
#else
|
||||
#endif
|
||||
|
||||
PIThread::~PIThread() {
|
||||
if (!running) return;
|
||||
#ifndef WINDOWS
|
||||
pthread_cancel(thread);
|
||||
#else
|
||||
CloseHandle(thread);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::start(int timer_delay) {
|
||||
pthread_attr_t attr;
|
||||
terminating = running = false;
|
||||
timer = timer_delay;
|
||||
#ifndef WINDOWS
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setschedparam(&attr, &sparam);
|
||||
if (pthread_create(&thread, &attr, thread_function, this) == 0) {
|
||||
running = true;
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0);
|
||||
if (thread == 0)
|
||||
return false;
|
||||
setPriority(priority_);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::startOnce() {
|
||||
pthread_attr_t attr;
|
||||
terminating = running = false;
|
||||
#ifndef WINDOWS
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setschedparam(&attr, &sparam);
|
||||
if (pthread_create(&thread, &attr, thread_function_once, this) == 0)
|
||||
return true;
|
||||
#else
|
||||
thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function_once, this, 0, 0);
|
||||
if (thread == 0)
|
||||
return false;
|
||||
setPriority(priority_);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PIThread::terminate(bool hard) {
|
||||
if (thread == 0) return;
|
||||
running = false;
|
||||
#ifndef WINDOWS
|
||||
if (hard) kill(thread, SIGKILL);
|
||||
else pthread_cancel(thread);
|
||||
#else
|
||||
CloseHandle(thread);
|
||||
#endif
|
||||
thread = 0;
|
||||
end();
|
||||
}
|
||||
|
||||
|
||||
void * PIThread::thread_function(void * t) {
|
||||
PIThread * ct = (PIThread * )t;
|
||||
ct->running = true;
|
||||
@@ -53,7 +88,11 @@ void * PIThread::thread_function(void * t) {
|
||||
ct->end();
|
||||
ct->running = false;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
#ifndef WINDOWS
|
||||
pthread_exit(0);
|
||||
#else
|
||||
ExitThread(0);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -68,21 +107,30 @@ void * PIThread::thread_function_once(void * t) {
|
||||
ct->end();
|
||||
ct->running = false;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
#ifndef WINDOWS
|
||||
pthread_exit(0);
|
||||
#else
|
||||
ExitThread(0);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void PIThread::setPriority(PIThread::Priority prior) {
|
||||
priority_ = prior;
|
||||
#ifndef LINUX
|
||||
#ifndef WINDOWS
|
||||
# ifndef LINUX
|
||||
sparam.sched_priority = (int)priority_;
|
||||
#else
|
||||
# else
|
||||
sparam.__sched_priority = (int)priority_;
|
||||
#endif
|
||||
# endif
|
||||
if (!running) return;
|
||||
pthread_getschedparam(thread, &policy, &sparam);
|
||||
pthread_setschedparam(thread, policy, &sparam);
|
||||
#else
|
||||
if (!running) return;
|
||||
SetThreadPriority(thread, -(int)priority_);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user