/*
PIP - Platform Independent Primitives
Thread
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
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 .
*/
#include "pithread.h"
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
piMonitor.threads++;
running = lockRun = false;
priority_ = piNormal;
timer = timer_delay;
/*addEvent("started");
addEvent("stopped");
addEventHandler(HANDLER(PIThread, start));
addEventHandler(HANDLER(PIThread, startOnce));
addEventHandler(HANDLER(PIThread, stop));
addEventHandler(HANDLER(PIThread, terminate));*/
if (startNow) start(timer_delay);
}
#ifndef WINDOWS
#else
#endif
PIThread::~PIThread() {
piMonitor.threads--;
if (!running) return;
#ifndef WINDOWS
pthread_cancel(thread);
#else
CloseHandle(thread);
#endif
}
bool PIThread::start(int timer_delay) {
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() {
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;
ct->begin();
raiseEvent(ct, "started");
while (!ct->terminating) {
if (ct->lockRun) ct->mutex_.lock();
ct->run();
if (ct->lockRun) ct->mutex_.unlock();
if (ct->timer > 0) msleep(ct->timer);
}
raiseEvent(ct, "stopped");
ct->end();
ct->running = false;
//cout << "thread " << t << " exiting ... " << endl;
#ifndef WINDOWS
pthread_exit(0);
#else
ExitThread(0);
#endif
return 0;
}
void * PIThread::thread_function_once(void * t) {
PIThread * ct = (PIThread * )t;
ct->running = true;
ct->begin();
raiseEvent(ct, "started");
if (ct->lockRun) ct->mutex_.lock();
ct->run();
if (ct->lockRun) ct->mutex_.unlock();
raiseEvent(ct, "stopped");
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 WINDOWS
# ifndef LINUX
sparam.sched_priority = (int)priority_;
# else
sparam.__sched_priority = (int)priority_;
# endif
if (!running) return;
pthread_getschedparam(thread, &policy, &sparam);
pthread_setschedparam(thread, policy, &sparam);
#else
if (!running) return;
SetThreadPriority(thread, -(int)priority_);
#endif
}
bool PIThread::waitForFinish(int timeout_msecs) {
if (timeout_msecs < 0) {
while (running)
msleep(1);
return true;
}
int cnt = 0;
while (running && cnt < timeout_msecs) {
msleep(1);
++cnt;
}
return cnt < timeout_msecs;
}
bool PIThread::waitForStart(int timeout_msecs) {
if (timeout_msecs < 0) {
while (!running)
msleep(1);
return true;
}
int cnt = 0;
while (!running && cnt < timeout_msecs) {
msleep(1);
++cnt;
}
return cnt < timeout_msecs;
}