summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Nechaev <petr.nechaev@cogentembedded.com>2015-03-23 01:01:28 +0300
committerPetr Nechaev <petr.nechaev@cogentembedded.com>2015-03-30 14:26:10 +0300
commitf273b98c57fd2e7f72420fc5ed9c2bc892e0b956 (patch)
treec099c32b3909404a717c1e94986283e3cf699db4
parentfbe961b9ee32502ff1d5960e44beadda6cfcddfb (diff)
downloadautomotive-message-broker-f273b98c57fd2e7f72420fc5ed9c2bc892e0b956.tar.gz
plugins/common: Set higher priority for cansocketreader thread
Improves CAN message handling time. CAN plugin's standardFrameReceived(frame) is called within this thread.
-rw-r--r--plugins/common/cansocketreader.cpp6
-rw-r--r--plugins/common/thread.cpp73
-rw-r--r--plugins/common/thread.h9
3 files changed, 63 insertions, 25 deletions
diff --git a/plugins/common/cansocketreader.cpp b/plugins/common/cansocketreader.cpp
index a9663e7f..b73c6fbf 100644
--- a/plugins/common/cansocketreader.cpp
+++ b/plugins/common/cansocketreader.cpp
@@ -38,7 +38,11 @@ bool CANSocketReader::start()
{
LOG_TRACE("");
- return CUtil::Thread::start();
+ bool res = CUtil::Thread::start();
+
+ // try to set higher priority
+ if (res) res = setPriority(4);
+ return res;
}
void CANSocketReader::stop()
diff --git a/plugins/common/thread.cpp b/plugins/common/thread.cpp
index d9d9c999..34f98754 100644
--- a/plugins/common/thread.cpp
+++ b/plugins/common/thread.cpp
@@ -44,18 +44,18 @@ static int gActiveThreadCount(0);
static void *PosixThreadProc(void *param)
{
- gMutex.lock();
- ++gActiveThreadCount;
- gMutex.unlock();
-
- CUtil::Thread *thread = (CUtil::Thread *)param;
- thread->run();
-
- gMutex.lock();
- --gActiveThreadCount;
- LOG_INFO("PosixThreadProc() - active threads: " << gActiveThreadCount);
- gMutex.unlock();
- return 0;
+ gMutex.lock();
+ ++gActiveThreadCount;
+ gMutex.unlock();
+
+ CUtil::Thread *thread = (CUtil::Thread *)param;
+ thread->run();
+
+ gMutex.lock();
+ --gActiveThreadCount;
+ LOG_INFO("PosixThreadProc() - active threads: " << gActiveThreadCount);
+ gMutex.unlock();
+ return 0;
}
namespace CUtil{
@@ -80,19 +80,44 @@ bool Thread::start()
}
// try to run
if (pthread_create(&thread, NULL/*&thread_attr*/, PosixThreadProc, this) != 0) {
- //pthread_attr_destroy(&thread_attr);
+ //pthread_attr_destroy(&thread_attr);
pthread_mutex_unlock(&mutex);
- return false;
- }
- //pthread_attr_destroy(&thread_attr);
+ return false;
+ }
+ //pthread_attr_destroy(&thread_attr);
runnableFlag = true;
pthread_mutex_unlock(&mutex);
- return true;
+ return true;
+}
+
+
+bool Thread::setPriority(int priority)
+{
+ pthread_mutex_lock(&mutex);
+ if (!runnableFlag) {// not running yet or terminated already
+ pthread_mutex_unlock(&mutex);
+ return false;
+ }
+
+ priority = priority < 1 ? 1 : priority;
+ priority = priority < 99 ? priority : 99;
+
+ // set priority
+ struct sched_param pr;
+ pr.__sched_priority = priority;
+ if (pthread_setschedparam(thread, SCHED_FIFO, &pr) < 0)
+ {
+ pthread_mutex_unlock(&mutex);
+ return false;
+ }
+
+ pthread_mutex_unlock(&mutex);
+ return true;
}
Thread::~Thread()
{
- stop();
+ stop();
pthread_cond_destroy( &cond );
pthread_mutex_destroy( &mutex );
thread = 0;
@@ -100,8 +125,8 @@ Thread::~Thread()
void Thread::stop()
{
- if (setRunnableFlag(false) == true) {
- if( thread != 0 ){
+ if (setRunnableFlag(false) == true) {
+ if( thread != 0 ){
if (thread == pthread_self()){
int s = pthread_detach(thread);
((void)s);// prevent compiler warning in RELEASE build
@@ -117,9 +142,9 @@ void Thread::stop()
}
}
thread = 0;
- }
- }
- return;
+ }
+ }
+ return;
}
bool Thread::setRunnableFlag(bool flag)
@@ -145,7 +170,7 @@ bool Thread::isRunnable(long miliseconds)
runnable = runnableFlag;
pthread_mutex_unlock(&mutex);
- return runnable;
+ return runnable;
}
bool Thread::wait( long miliseconds )
diff --git a/plugins/common/thread.h b/plugins/common/thread.h
index 8879b5b4..bf1fd485 100644
--- a/plugins/common/thread.h
+++ b/plugins/common/thread.h
@@ -126,6 +126,15 @@ public:
*/
virtual bool start();
+ /**
+ * Sets the priority of the thread for FIFO scheduling.
+ * @fn set_priority
+ * @param priority Integer ranging from 1 (lowest) to 99 (highest).
+ * @return True if the operation was successful.
+ * @public
+ */
+ bool setPriority(int priority);
+
/**
* Stops the thread
* @fn stop