thread.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014, RoboPeak
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice, 
00009  *    this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00012  *    this list of conditions and the following disclaimer in the documentation 
00013  *    and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00017  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00018  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00022  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00023  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00025  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 /*
00029  *  RoboPeak LIDAR System
00030  *  Thread abstract layer implementation
00031  *
00032  *  Copyright 2009 - 2014 RoboPeak Team
00033  *  http://www.robopeak.com
00034  * 
00035  */
00036 
00037 #include "arch/linux/arch_linux.h"
00038 
00039 #include <sched.h>
00040 
00041 namespace rp{ namespace hal{
00042 
00043 Thread Thread::create(thread_proc_t proc, void * data)
00044 {
00045     Thread newborn(proc, data);
00046     
00047     // tricky code, we assume pthread_t is not a structure but a word size value
00048     assert( sizeof(newborn._handle) >= sizeof(pthread_t));
00049 
00050     pthread_create((pthread_t *)&newborn._handle, NULL, (void * (*)(void *))proc, data);
00051 
00052     return newborn;
00053 }
00054 
00055 u_result Thread::terminate()
00056 {
00057     if (!this->_handle) return RESULT_OK;
00058     
00059     return pthread_cancel((pthread_t)this->_handle)==0?RESULT_OK:RESULT_OPERATION_FAIL;
00060 }
00061 
00062 u_result Thread::setPriority( priority_val_t p)
00063 {
00064     if (!this->_handle) return RESULT_OPERATION_FAIL;
00065     
00066     // check whether current schedule policy supports priority levels
00067     
00068     int current_policy;
00069     struct sched_param current_param;
00070     int ans;
00071     if (pthread_getschedparam( (pthread_t) this->_handle, &current_policy, &current_param))
00072     {
00073         // cannot retreieve values
00074         return RESULT_OPERATION_FAIL;
00075     }   
00076 
00077     //int pthread_priority = 0 ;
00078 
00079     switch(p)
00080     {
00081     case PRIORITY_REALTIME:
00082         //pthread_priority = pthread_priority_max;
00083         current_policy = SCHED_RR;
00084         break;
00085     case PRIORITY_HIGH:
00086         //pthread_priority = (pthread_priority_max + pthread_priority_min)/2;
00087         current_policy = SCHED_RR;
00088         break;
00089     case PRIORITY_NORMAL:
00090     case PRIORITY_LOW:
00091     case PRIORITY_IDLE:
00092         //pthread_priority = 0;
00093         current_policy = SCHED_OTHER;
00094         break;
00095     }
00096 
00097     current_param.__sched_priority = current_policy;
00098     if ( (ans = pthread_setschedparam( (pthread_t) this->_handle, current_policy, &current_param)) )
00099     {
00100         return RESULT_OPERATION_FAIL;
00101     }
00102     return  RESULT_OK;
00103 }
00104 
00105 Thread::priority_val_t Thread::getPriority()
00106 {
00107     if (!this->_handle) return PRIORITY_NORMAL;
00108 
00109     int current_policy;
00110     struct sched_param current_param;
00111     if (pthread_getschedparam( (pthread_t) this->_handle, &current_policy, &current_param))
00112     {
00113         // cannot retreieve values
00114         return PRIORITY_NORMAL;
00115     }   
00116 
00117     int pthread_priority_max = sched_get_priority_max(SCHED_RR);
00118     int pthread_priority_min = sched_get_priority_min(SCHED_RR);
00119 
00120     if (current_param.__sched_priority ==(pthread_priority_max ))
00121     {
00122         return PRIORITY_REALTIME;
00123     }
00124     if (current_param.__sched_priority >=(pthread_priority_max + pthread_priority_min)/2)
00125     {
00126         return PRIORITY_HIGH;
00127     }
00128     return PRIORITY_NORMAL;
00129 }
00130 
00131 u_result Thread::join(unsigned long timeout)
00132 {
00133     if (!this->_handle) return RESULT_OK;
00134     
00135     pthread_join((pthread_t)(this->_handle), NULL);
00136     return RESULT_OK;
00137 }
00138 
00139 }}


rplidar_ros
Author(s):
autogenerated on Fri Aug 28 2015 12:46:43