Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
rt_priority.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Felix Ruess <felix.ruess@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
26 #ifndef RT_PRIORITY_H
27 #define RT_PRIORITY_H
28 
29 #include <pthread.h>
30 #include <stdio.h>
31 
32 static inline int get_rt_prio(int prio)
33 {
34  struct sched_param param;
35  int policy;
36  pthread_getschedparam(pthread_self(), &policy, &param);
37  printf("Current schedparam: policy %d, prio %d\n", policy, param.sched_priority);
38 
39  //SCHED_RR, SCHED_FIFO, SCHED_OTHER (POSIX scheduling policies)
40  int sched = SCHED_FIFO;
41  int min = sched_get_priority_min(sched);
42  int max = sched_get_priority_max(sched);
43  param.sched_priority = prio;
44  if (prio > max || prio < min) {
45  printf("Requested prio %d outside current min/max prios: %d/%d\n", prio, min, max);
46  if (prio > max) {
47  param.sched_priority = max;
48  } else {
49  param.sched_priority = min;
50  }
51  }
52 
53  if (pthread_setschedparam(pthread_self(), sched, &param)) {
54  perror("setschedparam failed!");
55  return -1;
56  }
57  else {
58  pthread_getschedparam(pthread_self(), &policy, &param);
59  printf("New schedparam: policy %d, prio %d\n", policy, param.sched_priority);
60  }
61 
62  return 0;
63 }
64 
65 #include <sys/resource.h>
66 #include <unistd.h>
67 #include <sys/syscall.h>
68 
69 static inline int set_nice_level(int level)
70 {
71  pid_t tid;
72  tid = syscall(SYS_gettid);
73 
74  return setpriority(PRIO_PROCESS, tid, level);
75 }
76 
77 #endif /* RT_PRIORITY_H */
static int get_rt_prio(int prio)
Definition: rt_priority.h:32
static int set_nice_level(int level)
Definition: rt_priority.h:69