andrewm@0: /*
andrewm@0:  * prio.cpp
andrewm@0:  *
andrewm@0:  *  Created on: May 14, 2014
andrewm@0:  *      Author: Victor Zappi
andrewm@0:  */
andrewm@0: 
andrewm@0: #include "prio.h"
andrewm@0: using namespace std;
andrewm@0: //-----------------------------------------------------------------------------------------------------------
andrewm@0: // set wanted real-time priority to this thread
andrewm@0: //-----------------------------------------------------------------------------------------------------------
andrewm@0: void set_realtime_priority(int order)
andrewm@0: {
andrewm@0:     int ret;
andrewm@0: 
andrewm@0:     // We'll operate on the currently running thread.
andrewm@0:     pthread_t this_thread = pthread_self();
andrewm@0:     // struct sched_param is used to store the scheduling priority
andrewm@0: 	struct sched_param params;
andrewm@0: 	// We'll set the priority to the maximum.
andrewm@0: 	params.sched_priority = sched_get_priority_max(SCHED_FIFO) - order;
andrewm@0: 
andrewm@0: 	// Attempt to set thread real-time priority to the SCHED_FIFO policy
andrewm@0: 	ret = pthread_setschedparam(this_thread, SCHED_FIFO, &params);
andrewm@0: 	if (ret != 0) {
andrewm@0: 		// Print the error
andrewm@0: 		cout << "Unsuccessful in setting thread realtime prio" << endl;
andrewm@0: 		return;
andrewm@0: 	}
andrewm@0: 
andrewm@0: 	// Now verify the change in thread priority
andrewm@0:     int policy = 0;
andrewm@0:     ret = pthread_getschedparam(this_thread, &policy, &params);
andrewm@0:     if (ret != 0) {
andrewm@0:         cout << "Couldn't retrieve real-time scheduling parameters" << endl;
andrewm@0:         return;
andrewm@0:     }
andrewm@0: 
andrewm@0:     // Check the correct policy was applied
andrewm@0:     if(policy != SCHED_FIFO) {
andrewm@0:         cout << "Scheduling is NOT SCHED_FIFO!" << endl;
andrewm@0:     }
andrewm@0: }
andrewm@0: 
andrewm@0: //-----------------------------------------------------------------------------------------------------------
andrewm@0: 
andrewm@0: