linux/thread.hpp
Go to the documentation of this file.
1 /*
2  * RPLIDAR SDK
3  *
4  * Copyright (c) 2009 - 2014 RoboPeak Team
5  * http://www.robopeak.com
6  * Copyright (c) 2014 - 2018 Shanghai Slamtec Co., Ltd.
7  * http://www.slamtec.com
8  *
9  */
10 /*
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include "arch/linux/arch_linux.h"
36 
37 #include <sched.h>
38 #include <sys/types.h>
39 #include <sys/syscall.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 
43 namespace rp{ namespace hal{
44 
45 Thread Thread::create(thread_proc_t proc, void * data)
46 {
47  Thread newborn(proc, data);
48 
49  // tricky code, we assume pthread_t is not a structure but a word size value
50  assert( sizeof(newborn._handle) >= sizeof(pthread_t));
51 
52  pthread_create((pthread_t *)&newborn._handle, NULL, (void * (*)(void *))proc, data);
53 
54  return newborn;
55 }
56 
58 {
59  if (!this->_handle) return RESULT_OK;
60 
61  return pthread_cancel((pthread_t)this->_handle)==0?RESULT_OK:RESULT_OPERATION_FAIL;
62 }
63 
65 {
66 
67  pid_t selfTid = syscall(SYS_gettid);
68 
69  // check whether current schedule policy supports priority levels
70  int current_policy = SCHED_OTHER;
71  struct sched_param current_param;
72  int nice = 0;
73  int ans;
74 
75  if (sched_getparam(selfTid, &current_param))
76  {
77  // cannot retreieve values
78  return RESULT_OPERATION_FAIL;
79  }
80 
81  int pthread_priority_min;
82 
83 #if 1
84  pthread_priority_min = sched_get_priority_min(SCHED_RR);
85 #else
86  pthread_priority_min = 1;
87 #endif
88  int pthread_priority = 0 ;
89 
90  switch(p)
91  {
92  case PRIORITY_REALTIME:
93  //pthread_priority = pthread_priority_max;
94  current_policy = SCHED_RR;
95  pthread_priority = pthread_priority_min + 1;
96  nice = 0;
97  break;
98  case PRIORITY_HIGH:
99  //pthread_priority = (pthread_priority_max + pthread_priority_min)/2;
100  current_policy = SCHED_RR;
101  pthread_priority = pthread_priority_min;
102  nice = 0;
103  break;
104  case PRIORITY_NORMAL:
105  pthread_priority = 0;
106  current_policy = SCHED_OTHER;
107  nice = 0;
108  break;
109  case PRIORITY_LOW:
110  pthread_priority = 0;
111  current_policy = SCHED_OTHER;
112  nice = 10;
113  break;
114  case PRIORITY_IDLE:
115  pthread_priority = 0;
116  current_policy = SCHED_IDLE;
117  nice = 0;
118  break;
119  }
120  // change the inhertiable behavior
121  current_policy |= SCHED_RESET_ON_FORK;
122 
123  current_param.__sched_priority = pthread_priority;
124 
125 
126 
127 
128  // do not use pthread version as it will make the priority be inherited by a thread child
129  if ( (ans = sched_setscheduler(selfTid, current_policy , &current_param)) )
130  {
131  if (ans == EPERM)
132  {
133  //DBG_PRINT("warning, current process hasn't the right permission to set threads priority\n");
134  }
135  return RESULT_OPERATION_FAIL;
136  }
137 
138 
139  if ((current_policy == SCHED_OTHER) || (current_policy == SCHED_BATCH))
140  {
141  if (setpriority(PRIO_PROCESS, selfTid, nice)) {
142  return RESULT_OPERATION_FAIL;
143  }
144  }
145 
146 
147  return RESULT_OK;
148 }
149 
151 {
152  if (!this->_handle) return PRIORITY_NORMAL;
153 
154  int current_policy;
155  struct sched_param current_param;
156  if (pthread_getschedparam( (pthread_t) this->_handle, &current_policy, &current_param))
157  {
158  // cannot retreieve values
159  return PRIORITY_NORMAL;
160  }
161 
162  int pthread_priority_max = sched_get_priority_max(SCHED_RR);
163  int pthread_priority_min = sched_get_priority_min(SCHED_RR);
164 
165  if (current_param.__sched_priority ==(pthread_priority_max ))
166  {
167  return PRIORITY_REALTIME;
168  }
169  if (current_param.__sched_priority >=(pthread_priority_max + pthread_priority_min)/2)
170  {
171  return PRIORITY_HIGH;
172  }
173  return PRIORITY_NORMAL;
174 }
175 
176 u_result Thread::join(unsigned long timeout)
177 {
178  if (!this->_handle) return RESULT_OK;
179 
180  pthread_join((pthread_t)(this->_handle), NULL);
181  this->_handle = 0;
182  return RESULT_OK;
183 }
184 
185 }}
u_result
uint32_t u_result
Definition: rptypes.h:100
rp::hal::Thread::PRIORITY_NORMAL
@ PRIORITY_NORMAL
Definition: thread.h:50
rp::hal::Thread::SetSelfPriority
static u_result SetSelfPriority(priority_val_t p)
Definition: linux/thread.hpp:64
rp::hal::Thread::_handle
_word_size_t _handle
Definition: thread.h:90
rp::hal::Thread::create
static Thread create(thread_proc_t proc, void *data=NULL)
Definition: linux/thread.hpp:45
RESULT_OK
#define RESULT_OK
Definition: rptypes.h:102
arch_linux.h
rp::hal::Thread::PRIORITY_IDLE
@ PRIORITY_IDLE
Definition: thread.h:52
rp::hal::Thread::priority_val_t
priority_val_t
Definition: thread.h:46
rp::hal::Thread::PRIORITY_REALTIME
@ PRIORITY_REALTIME
Definition: thread.h:48
rp::hal::Thread::terminate
u_result terminate()
Definition: linux/thread.hpp:57
rp::hal::Thread::PRIORITY_HIGH
@ PRIORITY_HIGH
Definition: thread.h:49
rp::hal::Thread
Definition: thread.h:43
RESULT_OPERATION_FAIL
#define RESULT_OPERATION_FAIL
Definition: rptypes.h:106
rp::hal::Thread::getPriority
priority_val_t getPriority()
Definition: linux/thread.hpp:150
rp
Definition: rplidar_driver.h:43
data
sl_u8 data[0]
Definition: sl_lidar_protocol.h:5
rp::hal::Thread::PRIORITY_LOW
@ PRIORITY_LOW
Definition: thread.h:51
rp::hal::Thread::join
u_result join(unsigned long timeout=-1)
Definition: linux/thread.hpp:176


rplidar_ros
Author(s):
autogenerated on Fri Aug 2 2024 08:42:14