Mercurial > hg > beaglert
annotate examples/10-Instruments/d-box/prio.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Mon, 20 Jun 2016 16:20:38 +0100 |
parents | |
children |
rev | line source |
---|---|
robert@464 | 1 /* |
robert@464 | 2 * prio.cpp |
robert@464 | 3 * |
robert@464 | 4 * Created on: May 14, 2014 |
robert@464 | 5 * Author: Victor Zappi |
robert@464 | 6 */ |
robert@464 | 7 |
robert@464 | 8 #include "prio.h" |
robert@464 | 9 using namespace std; |
robert@464 | 10 //----------------------------------------------------------------------------------------------------------- |
robert@464 | 11 // set wanted real-time priority to this thread |
robert@464 | 12 //----------------------------------------------------------------------------------------------------------- |
robert@464 | 13 void set_realtime_priority(int order) |
robert@464 | 14 { |
robert@464 | 15 int ret; |
robert@464 | 16 |
robert@464 | 17 // We'll operate on the currently running thread. |
robert@464 | 18 pthread_t this_thread = pthread_self(); |
robert@464 | 19 // struct sched_param is used to store the scheduling priority |
robert@464 | 20 struct sched_param params; |
robert@464 | 21 // We'll set the priority to the maximum. |
robert@464 | 22 params.sched_priority = sched_get_priority_max(SCHED_FIFO) - order; |
robert@464 | 23 |
robert@464 | 24 // Attempt to set thread real-time priority to the SCHED_FIFO policy |
robert@464 | 25 ret = pthread_setschedparam(this_thread, SCHED_FIFO, ¶ms); |
robert@464 | 26 if (ret != 0) { |
robert@464 | 27 // Print the error |
robert@464 | 28 cout << "Unsuccessful in setting thread realtime prio" << endl; |
robert@464 | 29 return; |
robert@464 | 30 } |
robert@464 | 31 |
robert@464 | 32 // Now verify the change in thread priority |
robert@464 | 33 int policy = 0; |
robert@464 | 34 ret = pthread_getschedparam(this_thread, &policy, ¶ms); |
robert@464 | 35 if (ret != 0) { |
robert@464 | 36 cout << "Couldn't retrieve real-time scheduling parameters" << endl; |
robert@464 | 37 return; |
robert@464 | 38 } |
robert@464 | 39 |
robert@464 | 40 // Check the correct policy was applied |
robert@464 | 41 if(policy != SCHED_FIFO) { |
robert@464 | 42 cout << "Scheduling is NOT SCHED_FIFO!" << endl; |
robert@464 | 43 } |
robert@464 | 44 } |
robert@464 | 45 |
robert@464 | 46 //----------------------------------------------------------------------------------------------------------- |
robert@464 | 47 |
robert@464 | 48 |