plugins/common: Set higher priority for cansocketreader thread
authorPetr Nechaev <petr.nechaev@cogentembedded.com>
Sun, 22 Mar 2015 22:01:28 +0000 (01:01 +0300)
committerPetr Nechaev <petr.nechaev@cogentembedded.com>
Mon, 30 Mar 2015 11:26:10 +0000 (14:26 +0300)
Improves CAN message handling time. CAN plugin's standardFrameReceived(frame) is called within this thread.

plugins/common/cansocketreader.cpp
plugins/common/thread.cpp
plugins/common/thread.h

index a9663e7..b73c6fb 100644 (file)
@@ -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()
index d9d9c99..34f9875 100644 (file)
@@ -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 )
index 8879b5b..bf1fd48 100644 (file)
@@ -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