3 * Copyright 2006,2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <gruel/realtime.h>
39 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
45 * Rescale our virtual priority so that it maps to the middle 1/2 of
46 * the priorities given by min_real_pri and max_real_pri.
49 rescale_virtual_pri(int virtual_pri, int min_real_pri, int max_real_pri)
51 float rmin = min_real_pri + (0.25 * (max_real_pri - min_real_pri));
52 float rmax = min_real_pri + (0.75 * (max_real_pri - min_real_pri));
53 float m = (rmax - rmin) / (rt_priority_max() - rt_priority_min());
54 float y = m * (virtual_pri - rt_priority_min()) + rmin;
55 int y_int = static_cast<int>(rint(y));
56 return std::max(min_real_pri, std::min(max_real_pri, y_int));
64 #if defined(HAVE_PTHREAD_SETSCHEDPARAM)
69 enable_realtime_scheduling(rt_sched_param p)
71 int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR;
72 int min_real_pri = sched_get_priority_min(policy);
73 int max_real_pri = sched_get_priority_min(policy);
74 int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri);
76 pthread_t this_thread = pthread_self (); // this process
77 struct sched_param param;
78 memset (¶m, 0, sizeof (param));
79 param.sched_priority = pri;
80 int result = pthread_setschedparam (this_thread, policy, ¶m);
85 perror ("pthread_setschedparam: failed to set real time priority");
86 return RT_OTHER_ERROR;
90 //printf("SCHED_FIFO enabled with priority = %d\n", pri);
96 #elif defined(HAVE_SCHED_SETSCHEDULER)
101 enable_realtime_scheduling(rt_sched_param p)
103 int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR;
104 int min_real_pri = sched_get_priority_min(policy);
105 int max_real_pri = sched_get_priority_min(policy);
106 int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri);
108 int pid = 0; // this process
109 struct sched_param param;
110 memset(¶m, 0, sizeof(param));
111 param.sched_priority = pri;
112 int result = sched_setscheduler(pid, policy, ¶m);
117 perror ("sched_setscheduler: failed to set real time priority");
118 return RT_OTHER_ERROR;
122 //printf("SCHED_FIFO enabled with priority = %d\n", pri);
133 enable_realtime_scheduling(rt_sched_param p)
135 return RT_NOT_IMPLEMENTED;