Robotics Library  0.7.0
process.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_UTIL_PROCESS_H
28 #define RL_UTIL_PROCESS_H
29 
30 #ifdef WIN32
31 #include <windows.h>
32 #else // WIN32
33 #include <cerrno>
34 #include <unistd.h>
35 #include <sys/mman.h>
36 #endif // WIN32
37 
38 #include <iostream>
39 #include <system_error>
40 
41 namespace rl
42 {
43  namespace util
44  {
45  namespace this_process
46  {
47  inline int get_priority_max()
48  {
49 #ifdef WIN32
50  return REALTIME_PRIORITY_CLASS;
51 #else // WIN32
52  return ::sched_get_priority_max(SCHED_FIFO);
53 #endif // WIN32
54  }
55 
56  inline int get_priority_min()
57  {
58 #ifdef WIN32
59  return IDLE_PRIORITY_CLASS;
60 #else // WIN32
61  return ::sched_get_priority_min(SCHED_FIFO);
62 #endif // WIN32
63  }
64 
65  inline void memory_lock_all()
66  {
67 #if !defined(WIN32) && !defined(__APPLE__)
68  if (-1 == ::mlockall(MCL_CURRENT | MCL_FUTURE))
69  {
70  throw ::std::system_error(::std::error_code(errno, ::std::generic_category()));
71  }
72 #endif // !WIN32 && !__APPLE__
73  }
74 
75  inline void memory_reserve(const ::std::size_t& size)
76  {
77 #ifdef WIN32
78 #else // WIN32
79  char* buffer = new char[size];
80 
81  for (::std::size_t i = 0; i < size; i += ::sysconf(_SC_PAGESIZE))
82  {
83  buffer[i] = 0;
84  }
85 
86  delete[] buffer;
87 #endif // WIN32
88  }
89 
90  inline void memory_unlock_all()
91  {
92 #if !defined(WIN32) && !defined(__APPLE__)
93  if (-1 == ::munlockall())
94  {
95  throw ::std::system_error(::std::error_code(errno, ::std::generic_category()));
96  }
97 #endif // !WIN32 && !__APPLE__
98  }
99 
100  inline void set_priority(const int& priority)
101  {
102 #ifdef WIN32
103  if (!::SetPriorityClass(::GetCurrentProcess(), priority))
104  {
105  throw ::std::system_error(::std::error_code(::GetLastError(), ::std::generic_category()));
106  }
107 #else // WIN32
108  ::sched_param param;
109  param.sched_priority = priority;
110 
111 #ifdef __APPLE__
112 #warning "rl/util/process.h: set_priority not implemented for Mac OS"
113  //FIXME https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html
114  ::std::cerr << "set_priority not implemented for Mac OS" << ::std::endl;
115 #else // __APPLE__
116  if (-1 == ::sched_setscheduler(::getpid(), SCHED_FIFO, &param))
117  {
118  throw ::std::system_error(::std::error_code(errno, ::std::generic_category()));
119  }
120 #endif // __APPLE__
121 #endif // WIN32
122  }
123  }
124  }
125 }
126 
127 #endif // RL_UTIL_PROCESS_H
rl::util::this_process::memory_unlock_all
void memory_unlock_all()
Definition: process.h:90
rl::util::this_process::get_priority_min
int get_priority_min()
Definition: process.h:56
rl::util::this_process::get_priority_max
int get_priority_max()
Definition: process.h:47
rl::util::this_process::memory_lock_all
void memory_lock_all()
Definition: process.h:65
rl::util::this_process::set_priority
void set_priority(const int &priority)
Definition: process.h:100
rl::util::this_process::memory_reserve
void memory_reserve(const ::std::size_t &size)
Definition: process.h:75
rl
Robotics Library.
Definition: AnalogInput.cpp:30