Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_realtime.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 2, 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 #include <gr_realtime.h>
27
28 #ifdef HAVE_SCHED_H
29 #include <sched.h>
30 #endif
31
32 #include <string.h>
33 #include <errno.h>
34 #include <stdio.h>
35
36 #if defined(HAVE_SCHED_SETSCHEDULER)
37
38 gr_rt_status_t
39 gr_enable_realtime_scheduling()
40 {
41   int policy = SCHED_FIFO;
42   int pri = (sched_get_priority_max (policy) - sched_get_priority_min (policy)) / 2;
43   int pid = 0;  // this process
44
45   struct sched_param param;
46   memset(&param, 0, sizeof(param));
47   param.sched_priority = pri;
48   int result = sched_setscheduler(pid, policy, &param);
49   if (result != 0){
50     if (errno == EPERM)
51       return RT_NO_PRIVS;
52     else {
53       perror ("sched_setscheduler: failed to set real time priority");
54       return RT_OTHER_ERROR;
55     }
56   }
57   //printf("SCHED_FIFO enabled with priority = %d\n", pri);
58   return RT_OK;
59 }
60
61 // #elif // could try negative niceness
62
63 #else
64
65 gr_rt_status_t
66 gr_enable_realtime_scheduling()
67 {
68   return RT_NOT_IMPLEMENTED;
69 }
70
71 #endif