MRPT  1.9.9
scheduler.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/scheduler.h>
13 #include <mrpt/core/exceptions.h>
14 #include <mrpt/config.h>
15 
16 #ifdef MRPT_OS_WINDOWS
17 #include <windows.h>
18 #include <process.h>
19 #include <tlhelp32.h>
20 #else
21 #include <pthread.h>
22 #include <unistd.h>
23 #include <sys/select.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <utime.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <string.h> // strerror()
31 #endif
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #ifdef MRPT_OS_APPLE
36 #include <sys/sysctl.h>
37 #include <mach/mach_init.h>
38 #include <mach/thread_act.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 =
50 #ifdef MRPT_OS_APPLE
51  reinterpret_cast<long unsigned int>(pthread_self());
52 #else
53  pthread_self();
54 #endif
55 
56  int ret, policy;
57  struct sched_param param;
58 
59  if (0 != (ret = pthread_getschedparam(tid, &policy, &param)))
60  {
61  std::cerr
62  << "[mrpt::system::changeThreadPriority] Warning: Failed call to "
63  "pthread_getschedparam (error: `"
64  << strerror(ret) << "`)" << std::endl;
65  return;
66  }
67 
68  policy = SCHED_RR;
69  int min_prio = sched_get_priority_min(policy),
70  max_prio = sched_get_priority_max(policy);
71  if (min_prio < 0) min_prio = 1; // Just in case of error to calls above (!)
72  if (max_prio < 0) max_prio = 99;
73 
74  int prio = 0;
75  switch (priority)
76  {
77  case tpLowests:
78  prio = min_prio;
79  break;
80  case tpLower:
81  prio = (max_prio + 3 * min_prio) / 4;
82  break;
83  case tpLow:
84  prio = (max_prio + 2 * min_prio) / 3;
85  break;
86  case tpNormal:
87  prio = (max_prio + min_prio) / 2;
88  break;
89  case tpHigh:
90  prio = (2 * max_prio + min_prio) / 3;
91  break;
92  case tpHigher:
93  prio = (3 * max_prio + min_prio) / 4;
94  break;
95  case tpHighest:
96  prio = max_prio;
97  break;
98  }
99 
100  param.sched_priority = prio;
101  if (0 != (ret = pthread_setschedparam(tid, policy, &param)))
102  {
103  std::cerr
104  << "[mrpt::system::changeThreadPriority] Warning: Failed call to "
105  "pthread_setschedparam (error: `"
106  << strerror(ret) << "`)" << std::endl;
107  return;
108  }
109 #endif
110 }
111 
113 {
114 #ifdef MRPT_OS_WINDOWS
115  DWORD dwPri;
116  switch (priority)
117  {
118  case ppIdle:
119  dwPri = IDLE_PRIORITY_CLASS;
120  break;
121  case ppNormal:
122  dwPri = NORMAL_PRIORITY_CLASS;
123  break;
124  case ppHigh:
125  dwPri = HIGH_PRIORITY_CLASS;
126  break;
127  case ppVeryHigh:
128  dwPri = REALTIME_PRIORITY_CLASS;
129  break;
130  default:
131  THROW_EXCEPTION("Invalid priority value");
132  }
133  SetPriorityClass(GetCurrentProcess(), dwPri);
134 #else
135  int nice_val;
136  switch (priority)
137  {
138  case ppIdle:
139  nice_val = +19;
140  break;
141  case ppNormal:
142  nice_val = 0;
143  break;
144  case ppHigh:
145  nice_val = -10;
146  break;
147  case ppVeryHigh:
148  nice_val = -20;
149  break;
150  default:
151  THROW_EXCEPTION("Invalid priority value");
152  }
153  errno = 0;
154  const int ret = nice(nice_val);
155  if (ret == -1 && errno == EPERM)
156  {
157  std::cerr << "[mrpt::system::changeCurrentProcessPriority] Error "
158  "calling nice(): Not enough permissions.\n";
159  }
160 #endif
161 }
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:41
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:112
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
GLfloat param
Definition: glext.h:3831
Win32: THREAD_PRIORITY_BELOW_NORMAL.
Definition: scheduler.h:36



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020