MRPT  2.0.0
scheduler.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/config.h>
13 #include <mrpt/core/exceptions.h>
14 #include <mrpt/system/scheduler.h>
15 
16 #ifdef MRPT_OS_WINDOWS
17 #include <windows.h>
18 
19 #include <process.h>
20 #include <tlhelp32.h>
21 #else
22 #include <pthread.h>
23 #include <sys/select.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <utime.h>
27 #include <cerrno>
28 #include <csignal>
29 #include <cstring> // strerror()
30 #include <ctime>
31 #endif
32 
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #ifdef MRPT_OS_APPLE
36 #include <mach/mach_init.h>
37 #include <mach/thread_act.h>
38 #include <sys/sysctl.h>
39 #endif
40 
41 #include <iostream>
42 
44 {
45 #ifdef MRPT_OS_WINDOWS
46  // TThreadPriority is defined to agree with numbers expected by Win32 API:
47  SetThreadPriority(GetCurrentThread(), priority);
48 #else
49  const pthread_t tid = pthread_self();
50 
51  int ret, policy;
52  struct sched_param param
53  {
54  };
55 
56  if (0 != (ret = pthread_getschedparam(tid, &policy, &param)))
57  {
58  std::cerr
59  << "[mrpt::system::changeThreadPriority] Warning: Failed call to "
60  "pthread_getschedparam (error: `"
61  << strerror(ret) << "`)" << std::endl;
62  return;
63  }
64 
65  policy = SCHED_RR;
66  int min_prio = sched_get_priority_min(policy),
67  max_prio = sched_get_priority_max(policy);
68  if (min_prio < 0) min_prio = 1; // Just in case of error to calls above (!)
69  if (max_prio < 0) max_prio = 99;
70 
71  int prio = 0;
72  switch (priority)
73  {
74  case tpLowests:
75  prio = min_prio;
76  break;
77  case tpLower:
78  prio = (max_prio + 3 * min_prio) / 4;
79  break;
80  case tpLow:
81  prio = (max_prio + 2 * min_prio) / 3;
82  break;
83  case tpNormal:
84  prio = (max_prio + min_prio) / 2;
85  break;
86  case tpHigh:
87  prio = (2 * max_prio + min_prio) / 3;
88  break;
89  case tpHigher:
90  prio = (3 * max_prio + min_prio) / 4;
91  break;
92  case tpHighest:
93  prio = max_prio;
94  break;
95  }
96 
97  param.sched_priority = prio;
98  if (0 != (ret = pthread_setschedparam(tid, policy, &param)))
99  {
100  std::cerr
101  << "[mrpt::system::changeThreadPriority] Warning: Failed call to "
102  "pthread_setschedparam (error: `"
103  << strerror(ret) << "`)" << std::endl;
104  return;
105  }
106 #endif
107 }
108 
110 {
111 #ifdef MRPT_OS_WINDOWS
112  DWORD dwPri;
113  switch (priority)
114  {
115  case ppIdle:
116  dwPri = IDLE_PRIORITY_CLASS;
117  break;
118  case ppNormal:
119  dwPri = NORMAL_PRIORITY_CLASS;
120  break;
121  case ppHigh:
122  dwPri = HIGH_PRIORITY_CLASS;
123  break;
124  case ppVeryHigh:
125  dwPri = REALTIME_PRIORITY_CLASS;
126  break;
127  default:
128  THROW_EXCEPTION("Invalid priority value");
129  }
130  SetPriorityClass(GetCurrentProcess(), dwPri);
131 #else
132  int nice_val;
133  switch (priority)
134  {
135  case ppIdle:
136  nice_val = +19;
137  break;
138  case ppNormal:
139  nice_val = 0;
140  break;
141  case ppHigh:
142  nice_val = -10;
143  break;
144  case ppVeryHigh:
145  nice_val = -20;
146  break;
147  default:
148  THROW_EXCEPTION("Invalid priority value");
149  }
150  errno = 0;
151  const int ret = nice(nice_val);
152  if (ret == -1 && errno == EPERM)
153  {
154  std::cerr << "[mrpt::system::changeCurrentProcessPriority] Error "
155  "calling nice(): Not enough permissions.\n";
156  }
157 #endif
158 }
void changeCurrentThreadPriority(TThreadPriority priority)
Change the priority of the current thread - for Windows, see also changeCurrentProcessPriority() ...
Definition: scheduler.cpp:43
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
Win32: THREAD_PRIORITY_ABOVE_NORMAL.
Definition: scheduler.h:38
void changeCurrentProcessPriority(TProcessPriority priority)
Change the priority of the given process (it applies to all the threads, plus independent modifiers f...
Definition: scheduler.cpp:109
TProcessPriority
The type for cross-platform process (application) priorities.
Definition: scheduler.h:21
Win32: THREAD_PRIORITY_LOWEST.
Definition: scheduler.h:35
Win32: THREAD_PRIORITY_IDLE.
Definition: scheduler.h:34
Win32: THREAD_PRIORITY_NORMAL.
Definition: scheduler.h:37
TThreadPriority
The type for cross-platform thread priorities.
Definition: scheduler.h:32
Win32: THREAD_PRIORITY_TIME_CRITICAL.
Definition: scheduler.h:40
Win32: THREAD_PRIORITY_HIGHEST.
Definition: scheduler.h:39
Win32: THREAD_PRIORITY_BELOW_NORMAL.
Definition: scheduler.h:36



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020