diff options
author | Tom Rondeau <trondeau@vt.edu> | 2013-04-17 13:43:52 -0400 |
---|---|---|
committer | Tom Rondeau <trondeau@vt.edu> | 2013-04-29 14:52:56 -0400 |
commit | f3e2e07201c50033bf6c9d0c6a6f068557b4f17f (patch) | |
tree | 140b3c2d20a951ffd4abd564c3378ee2e2f9fc7c /gnuradio-runtime/lib/realtime.cc | |
parent | 35303ae975a5b1bdecc2492bc96e2b8e89b62a3d (diff) |
runtime: converting runtime core to gr namespace, gnuradio include dir.
Diffstat (limited to 'gnuradio-runtime/lib/realtime.cc')
-rw-r--r-- | gnuradio-runtime/lib/realtime.cc | 151 |
1 files changed, 5 insertions, 146 deletions
diff --git a/gnuradio-runtime/lib/realtime.cc b/gnuradio-runtime/lib/realtime.cc index 5dadc26bbd..81f92ae488 100644 --- a/gnuradio-runtime/lib/realtime.cc +++ b/gnuradio-runtime/lib/realtime.cc @@ -24,155 +24,14 @@ #include <config.h> #endif -#include <realtime.h> - -#ifdef HAVE_SCHED_H -#include <sched.h> -#endif - -#include <algorithm> -#include <math.h> -#include <string.h> -#include <errno.h> -#include <stdio.h> - -#if defined(HAVE_PTHREAD_SETSCHEDPARAM) || defined(HAVE_SCHED_SETSCHEDULER) -#include <pthread.h> - -namespace gr { - - /*! - * Rescale our virtual priority so that it maps to the middle 1/2 of - * the priorities given by min_real_pri and max_real_pri. - */ - static int - rescale_virtual_pri(int virtual_pri, int min_real_pri, int max_real_pri) - { - float rmin = min_real_pri + (0.25 * (max_real_pri - min_real_pri)); - float rmax = min_real_pri + (0.75 * (max_real_pri - min_real_pri)); - float m = (rmax - rmin) / (rt_priority_max() - rt_priority_min()); - float y = m * (virtual_pri - rt_priority_min()) + rmin; - int y_int = static_cast<int>(rint(y)); - return std::max(min_real_pri, std::min(max_real_pri, y_int)); - } - -} // namespace gr - -#endif - - -#if defined(HAVE_PTHREAD_SETSCHEDPARAM) - -namespace gr { - - rt_status_t - enable_realtime_scheduling(rt_sched_param p) - { - int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; - int min_real_pri = sched_get_priority_min(policy); - int max_real_pri = sched_get_priority_max(policy); - int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); - - // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. - // fprintf(stderr, "pthread_setschedparam: policy = %d, pri = %d\n", policy, pri); - - struct sched_param param; - memset (¶m, 0, sizeof (param)); - param.sched_priority = pri; - int result = pthread_setschedparam (pthread_self(), policy, ¶m); - if (result != 0) { - if (result == EPERM) // N.B., return value, not errno - return RT_NO_PRIVS; - else { - fprintf(stderr, - "pthread_setschedparam: failed to set real time priority: %s\n", - strerror(result)); - return RT_OTHER_ERROR; - } - } - - //printf("SCHED_FIFO enabled with priority = %d\n", pri); - return RT_OK; - } -} // namespace gr - - -#elif defined(HAVE_SCHED_SETSCHEDULER) - -namespace gr { - - rt_status_t - enable_realtime_scheduling(rt_sched_param p) - { - int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; - int min_real_pri = sched_get_priority_min(policy); - int max_real_pri = sched_get_priority_max(policy); - int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); - - // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. - // fprintf(stderr, "sched_setscheduler: policy = %d, pri = %d\n", policy, pri); - - int pid = 0; // this process - struct sched_param param; - memset(¶m, 0, sizeof(param)); - param.sched_priority = pri; - int result = sched_setscheduler(pid, policy, ¶m); - if (result != 0){ - if (errno == EPERM) - return RT_NO_PRIVS; - else { - perror ("sched_setscheduler: failed to set real time priority"); - return RT_OTHER_ERROR; - } - } - - //printf("SCHED_FIFO enabled with priority = %d\n", pri); - return RT_OK; - } - -} // namespace gr - -#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - -#include <windows.h> - -namespace gr { - - rt_status_t enable_realtime_scheduling(rt_sched_param p){ - - //set the priority class on the process - int pri_class = (true)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; - if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) - return RT_OTHER_ERROR; - - //scale the priority value to the constants - int priorities[] = { - THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, - THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL - }; - const double priority = double(p.priority)/(rt_priority_max() - rt_priority_min()); - size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 - pri_index %= sizeof(priorities)/sizeof(*priorities); //range check - - //set the thread priority on the thread - if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) - return RT_OTHER_ERROR; - - //printf("SetPriorityClass + SetThreadPriority\n"); - return RT_OK; - } - -} // namespace gr - -#else +#include <gnuradio/realtime.h> namespace gr { - + rt_status_t - enable_realtime_scheduling(rt_sched_param p) + enable_realtime_scheduling() { - return RT_NOT_IMPLEMENTED; + return enable_realtime_scheduling(); } -} // namespace gr -#endif +} /* namespace gr */ |