root / host / lib / utils / thread_priority.cpp @ 4357f5d3
History | View | Annotate | Download (4.19 KB)
| 1 |
//
|
|---|---|
| 2 |
// Copyright 2010-2011 Ettus Research LLC
|
| 3 |
//
|
| 4 |
// This program is free software: you can redistribute it and/or modify
|
| 5 |
// it under the terms of the GNU General Public License as published by
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or
|
| 7 |
// (at your option) any later version.
|
| 8 |
//
|
| 9 |
// This program is distributed in the hope that it will be useful,
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
// GNU General Public License for more details.
|
| 13 |
//
|
| 14 |
// You should have received a copy of the GNU General Public License
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 16 |
//
|
| 17 |
|
| 18 |
#include <uhd/utils/thread_priority.hpp> |
| 19 |
#include <uhd/utils/warning.hpp> |
| 20 |
#include <uhd/exception.hpp> |
| 21 |
#include <boost/format.hpp> |
| 22 |
#include <iostream> |
| 23 |
|
| 24 |
bool uhd::set_thread_priority_safe(float priority, bool realtime){ |
| 25 |
try{
|
| 26 |
set_thread_priority(priority, realtime); |
| 27 |
return true; |
| 28 |
}catch(const std::exception &e){ |
| 29 |
uhd::warning::post(str(boost::format( |
| 30 |
"%s\n"
|
| 31 |
"Failed to set thread priority %d (%s):\n"
|
| 32 |
"Performance may be negatively affected.\n"
|
| 33 |
"See the general application notes.\n"
|
| 34 |
) % e.what() % priority % (realtime?"realtime":""))); |
| 35 |
return false; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static void check_priority_range(float priority){ |
| 40 |
if (priority > +1.0 or priority < -1.0) |
| 41 |
throw std::range_error("priority out of range [-1.0, +1.0]"); |
| 42 |
} |
| 43 |
|
| 44 |
/***********************************************************************
|
| 45 |
* Pthread API to set priority
|
| 46 |
**********************************************************************/
|
| 47 |
#ifdef HAVE_PTHREAD_SETSCHEDPARAM
|
| 48 |
#include <pthread.h> |
| 49 |
|
| 50 |
void uhd::set_thread_priority(float priority, bool realtime){ |
| 51 |
check_priority_range(priority); |
| 52 |
|
| 53 |
//when realtime is not enabled, use sched other
|
| 54 |
int policy = (realtime)? SCHED_RR : SCHED_OTHER;
|
| 55 |
|
| 56 |
//we cannot have below normal priority, set to zero
|
| 57 |
if (priority < 0) priority = 0; |
| 58 |
|
| 59 |
//get the priority bounds for the selected policy
|
| 60 |
int min_pri = sched_get_priority_min(policy);
|
| 61 |
int max_pri = sched_get_priority_max(policy);
|
| 62 |
if (min_pri == -1 or max_pri == -1) throw uhd::os_error("error in sched_get_priority_min/max"); |
| 63 |
|
| 64 |
//set the new priority and policy
|
| 65 |
sched_param sp; |
| 66 |
sp.sched_priority = int(priority*(max_pri - min_pri)) + min_pri;
|
| 67 |
int ret = pthread_setschedparam(pthread_self(), policy, &sp);
|
| 68 |
if (ret != 0) throw uhd::os_error("error in pthread_setschedparam"); |
| 69 |
} |
| 70 |
#endif /* HAVE_PTHREAD_SETSCHEDPARAM */ |
| 71 |
|
| 72 |
/***********************************************************************
|
| 73 |
* Windows API to set priority
|
| 74 |
**********************************************************************/
|
| 75 |
#ifdef HAVE_WIN_SETTHREADPRIORITY
|
| 76 |
#include <windows.h> |
| 77 |
|
| 78 |
void uhd::set_thread_priority(float priority, bool realtime){ |
| 79 |
check_priority_range(priority); |
| 80 |
|
| 81 |
//set the priority class on the process
|
| 82 |
int pri_class = (realtime)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
|
| 83 |
if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) |
| 84 |
throw uhd::os_error("error in SetPriorityClass"); |
| 85 |
|
| 86 |
//scale the priority value to the constants
|
| 87 |
int priorities[] = {
|
| 88 |
THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, |
| 89 |
THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL |
| 90 |
}; |
| 91 |
size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 |
| 92 |
|
| 93 |
//set the thread priority on the thread
|
| 94 |
if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) |
| 95 |
throw uhd::os_error("error in SetThreadPriority"); |
| 96 |
} |
| 97 |
#endif /* HAVE_WIN_SETTHREADPRIORITY */ |
| 98 |
|
| 99 |
/***********************************************************************
|
| 100 |
* Unimplemented API to set priority
|
| 101 |
**********************************************************************/
|
| 102 |
#ifdef HAVE_LOAD_MODULES_DUMMY
|
| 103 |
void uhd::set_thread_priority(float, bool){ |
| 104 |
throw uhd::not_implemented_error("set thread priority not implemented"); |
| 105 |
} |
| 106 |
|
| 107 |
#endif /* HAVE_LOAD_MODULES_DUMMY */ |