15.10.2012 - version 0.2.0 - PIIODevice - base of file, ethernet, serial and packets extractor. PIEthernet now support also TCP (client and server). PIConsole now can align labels in each column individually.
This commit is contained in:
47
pithread.cpp
Normal file → Executable file
47
pithread.cpp
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Thread
|
||||
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2012 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
|
||||
@@ -20,23 +20,26 @@
|
||||
#include "pithread.h"
|
||||
|
||||
|
||||
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
||||
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
data_ = data;
|
||||
ret_func = func;
|
||||
running = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
timer = timer_delay;
|
||||
if (startNow) start(timer_delay);
|
||||
}
|
||||
|
||||
|
||||
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
ret_func = 0;
|
||||
running = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
timer = timer_delay;
|
||||
/*addEvent("started");
|
||||
addEvent("stopped");
|
||||
addEventHandler<int>(HANDLER(PIThread, start));
|
||||
addEventHandler(HANDLER(PIThread, startOnce));
|
||||
addEventHandler(HANDLER(PIThread, stop));
|
||||
addEventHandler<bool>(HANDLER(PIThread, terminate));*/
|
||||
if (startNow) start(timer_delay);
|
||||
}
|
||||
|
||||
#ifndef WINDOWS
|
||||
#else
|
||||
#endif
|
||||
|
||||
PIThread::~PIThread() {
|
||||
piMonitor.threads--;
|
||||
@@ -57,14 +60,17 @@ bool PIThread::start(int timer_delay) {
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setschedparam(&attr, &sparam);
|
||||
if (pthread_create(&thread, &attr, thread_function, this) == 0) {
|
||||
setPriority(priority_);
|
||||
running = true;
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0);
|
||||
if (thread == 0)
|
||||
return false;
|
||||
setPriority(priority_);
|
||||
if (thread != 0) {
|
||||
setPriority(priority_);
|
||||
running = true;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
@@ -76,13 +82,18 @@ bool PIThread::startOnce() {
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setschedparam(&attr, &sparam);
|
||||
if (pthread_create(&thread, &attr, thread_function_once, this) == 0)
|
||||
if (pthread_create(&thread, &attr, thread_function_once, this) == 0) {
|
||||
setPriority(priority_);
|
||||
running = true;
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function_once, this, 0, 0);
|
||||
if (thread == 0)
|
||||
if (thread != 0) {
|
||||
setPriority(priority_);
|
||||
running = true;
|
||||
return false;
|
||||
setPriority(priority_);
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
@@ -110,6 +121,7 @@ void * PIThread::thread_function(void * t) {
|
||||
while (!ct->terminating) {
|
||||
if (ct->lockRun) ct->mutex_.lock();
|
||||
ct->run();
|
||||
if (ct->ret_func != 0) ct->ret_func(ct->data_);
|
||||
if (ct->lockRun) ct->mutex_.unlock();
|
||||
if (ct->timer > 0) msleep(ct->timer);
|
||||
}
|
||||
@@ -133,6 +145,7 @@ void * PIThread::thread_function_once(void * t) {
|
||||
raiseEvent(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();
|
||||
raiseEvent(ct, "stopped");
|
||||
ct->end();
|
||||
|
||||
Reference in New Issue
Block a user