29.07.2011 - fundamental new
This commit is contained in:
61
pithread.cpp
61
pithread.cpp
@@ -17,7 +17,7 @@ PIThread::~PIThread() {
|
||||
|
||||
bool PIThread::start(int timer_delay) {
|
||||
pthread_attr_t attr;
|
||||
terminating = false;
|
||||
terminating = running = false;
|
||||
timer = timer_delay;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setschedparam(&attr, &sparam);
|
||||
@@ -29,13 +29,25 @@ bool PIThread::start(int timer_delay) {
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::startOnce() {
|
||||
pthread_attr_t attr;
|
||||
terminating = running = false;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setschedparam(&attr, &sparam);
|
||||
if (pthread_create(&thread, &attr, thread_function_once, this) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void * PIThread::thread_function(void * t) {
|
||||
PIThread * ct = (PIThread * )t;
|
||||
ct->running = true;
|
||||
ct->begin();
|
||||
while (!ct->terminating) {
|
||||
if (ct->lockRun) ct->mutex_.lock();
|
||||
ct->run();
|
||||
if (ct->lockRun) ct->mutex_.unlock();;
|
||||
if (ct->lockRun) ct->mutex_.unlock();
|
||||
if (ct->timer > 0) msleep(ct->timer);
|
||||
}
|
||||
ct->end();
|
||||
@@ -46,9 +58,24 @@ void * PIThread::thread_function(void * t) {
|
||||
}
|
||||
|
||||
|
||||
void * PIThread::thread_function_once(void * t) {
|
||||
PIThread * ct = (PIThread * )t;
|
||||
ct->running = true;
|
||||
ct->begin();
|
||||
if (ct->lockRun) ct->mutex_.lock();
|
||||
ct->run();
|
||||
if (ct->lockRun) ct->mutex_.unlock();
|
||||
ct->end();
|
||||
ct->running = false;
|
||||
//cout << "thread " << t << " exiting ... " << endl;
|
||||
pthread_exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void PIThread::setPriority(PIThread::Priority prior) {
|
||||
priority_ = prior;
|
||||
#if __QNX__ || __WIN32__
|
||||
#ifndef LINUX
|
||||
sparam.sched_priority = (int)priority_;
|
||||
#else
|
||||
sparam.__sched_priority = (int)priority_;
|
||||
@@ -59,3 +86,31 @@ void PIThread::setPriority(PIThread::Priority prior) {
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user