Robotics Library  0.7.0
thread.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_THREAD_H
28 #define RL_UTIL_THREAD_H
29 
30 #ifdef WIN32
31 #include <windows.h>
32 #else // WIN32
33 #include <pthread.h>
34 #endif // WIN32
35 
36 #include <system_error>
37 
38 namespace rl
39 {
40  namespace util
41  {
42  namespace this_thread
43  {
44  inline int get_priority()
45  {
46 #ifdef WIN32
47  return ::GetThreadPriority(::GetCurrentThread());
48 #else // WIN32
49  int policy;
50  ::sched_param param;
51 
52  if (-1 == ::pthread_getschedparam(::pthread_self(), &policy, &param))
53  {
54  throw ::std::system_error(::std::error_code(errno, ::std::generic_category()));
55  }
56 
57  return param.sched_priority;
58 #endif // WIN32
59  }
60 
61  inline int get_priority_max()
62  {
63 #ifdef WIN32
64  return THREAD_PRIORITY_TIME_CRITICAL;
65 #else // WIN32
66  return ::sched_get_priority_max(SCHED_FIFO);
67 #endif // WIN32
68  }
69 
70  inline int get_priority_min()
71  {
72 #ifdef WIN32
73  return THREAD_PRIORITY_IDLE;
74 #else // WIN32
75  return ::sched_get_priority_min(SCHED_FIFO);
76 #endif // WIN32
77  }
78 
79  inline void set_priority(const int& priority)
80  {
81 #ifdef WIN32
82  if (!::SetThreadPriority(::GetCurrentThread(), priority))
83  {
84  throw ::std::system_error(::std::error_code(::GetLastError(), ::std::generic_category()));
85  }
86 #else // WIN32
87  ::sched_param param;
88  param.sched_priority = priority;
89 
90  if (-1 == ::pthread_setschedparam(::pthread_self(), SCHED_FIFO, &param))
91  {
92  throw ::std::system_error(::std::error_code(errno, ::std::generic_category()));
93  }
94 #endif // WIN32
95  }
96  }
97  }
98 }
99 
100 #endif // RL_UTIL_THREAD_H
rl::util::this_thread::get_priority
int get_priority()
Definition: thread.h:44
rl::util::this_thread::get_priority_max
int get_priority_max()
Definition: thread.h:61
rl::util::this_thread::get_priority_min
int get_priority_min()
Definition: thread.h:70
rl::util::this_thread::set_priority
void set_priority(const int &priority)
Definition: thread.h:79
rl
Robotics Library.
Definition: AnalogInput.cpp:30