Merged -r8639:8641 from jcorgan/gruel into trunk. Adds libgruel, the GNU Radio Utili...
[debian/gnuradio] / gruel / src / lib / realtime.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <gruel/realtime.h>
28
29 #ifdef HAVE_SCHED_H
30 #include <sched.h>
31 #endif
32
33 #include <string.h>
34 #include <errno.h>
35 #include <stdio.h>
36
37 #if defined(HAVE_SCHED_SETSCHEDULER)
38
39 namespace gruel {
40
41   rt_status_t
42   enable_realtime_scheduling()
43   {
44     int policy = SCHED_FIFO;
45     int pri = (sched_get_priority_max (policy) + sched_get_priority_min (policy)) / 2;
46     int pid = 0;  // this process
47
48     struct sched_param param;
49     memset(&param, 0, sizeof(param));
50     param.sched_priority = pri;
51     int result = sched_setscheduler(pid, policy, &param);
52     if (result != 0){
53       if (errno == EPERM)
54         return RT_NO_PRIVS;
55       else {
56         perror ("sched_setscheduler: failed to set real time priority");
57         return RT_OTHER_ERROR;
58       }
59     }
60     
61     //printf("SCHED_FIFO enabled with priority = %d\n", pri);
62     return RT_OK;
63   }
64
65 } // namespace gruel
66
67 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
68
69 #include <pthread.h>
70 #include <stdio.h>
71
72 namespace gruel {
73
74   rt_status_t
75   enable_realtime_scheduling()
76   {
77     int policy = SCHED_FIFO;
78     int pri = (sched_get_priority_max (policy) +
79                sched_get_priority_min (policy)) / 2;
80     pthread_t this_thread = pthread_self ();  // this process
81     struct sched_param param;
82     memset (&param, 0, sizeof (param));
83     param.sched_priority = pri;
84     int result = pthread_setschedparam (this_thread, policy, &param);
85     if (result != 0) {
86       if (errno == EPERM)
87         return RT_NO_PRIVS;
88       else {
89         perror ("pthread_setschedparam: failed to set real time priority");
90         return RT_OTHER_ERROR;
91       }
92     }
93   
94     //printf("SCHED_FIFO enabled with priority = %d\n", pri);
95     return RT_OK;
96   }
97 } // namespace gruel
98
99 // #elif // could try negative niceness
100
101 #else
102
103 namespace gruel {
104
105   rt_status_t
106   enable_realtime_scheduling()
107   {
108     return RT_NOT_IMPLEMENTED;
109   }
110 } // namespace gruel
111
112 #endif