winthread.hpp
Go to the documentation of this file.
00001 /*
00002  *  RPLIDAR SDK
00003  *
00004  *  Copyright (c) 2009 - 2014 RoboPeak Team
00005  *  http://www.robopeak.com
00006  *  Copyright (c) 2014 - 2019 Shanghai Slamtec Co., Ltd.
00007  *  http://www.slamtec.com
00008  *
00009  */
00010 /*
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  *
00017  * 2. Redistributions in binary form must reproduce the above copyright notice,
00018  *    this list of conditions and the following disclaimer in the documentation
00019  *    and/or other materials provided with the distribution.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00023  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00024  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00025  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00027  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00028  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00029  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00030  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00031  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032  *
00033  */
00034 
00035 #include "sdkcommon.h"
00036 #include <process.h>
00037 
00038 namespace rp{ namespace hal{
00039 
00040 Thread Thread::create(thread_proc_t proc, void * data)
00041 {
00042     Thread newborn(proc, data);
00043 
00044     newborn._handle = (_word_size_t)( 
00045         _beginthreadex(NULL, 0, (unsigned int (_stdcall * )( void * ))proc,
00046                         data, 0, NULL));
00047     return newborn;
00048 }
00049 
00050 u_result Thread::terminate()
00051 {
00052     if (!this->_handle) return RESULT_OK;
00053     if (TerminateThread( reinterpret_cast<HANDLE>(this->_handle), -1))
00054     {
00055         CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
00056         this->_handle = NULL;
00057         return RESULT_OK;
00058     }else
00059     {
00060         return RESULT_OPERATION_FAIL;
00061     }
00062 }
00063 
00064 u_result Thread::setPriority( priority_val_t p)
00065 {
00066         if (!this->_handle) return RESULT_OPERATION_FAIL;
00067 
00068         int win_priority =  THREAD_PRIORITY_NORMAL;
00069         switch(p)
00070         {
00071         case PRIORITY_REALTIME:
00072                 win_priority = THREAD_PRIORITY_TIME_CRITICAL;
00073                 break;
00074         case PRIORITY_HIGH:
00075                 win_priority = THREAD_PRIORITY_HIGHEST;
00076                 break;
00077         case PRIORITY_NORMAL:
00078                 win_priority = THREAD_PRIORITY_NORMAL;
00079                 break;
00080         case PRIORITY_LOW:
00081                 win_priority = THREAD_PRIORITY_LOWEST;
00082                 break;
00083         case PRIORITY_IDLE:
00084                 win_priority = THREAD_PRIORITY_IDLE;
00085                 break;
00086         }
00087 
00088         if (SetThreadPriority(reinterpret_cast<HANDLE>(this->_handle), win_priority))
00089         {
00090                 return RESULT_OK;
00091         }
00092         return RESULT_OPERATION_FAIL;
00093 }
00094 
00095 Thread::priority_val_t Thread::getPriority()
00096 {
00097         if (!this->_handle) return PRIORITY_NORMAL;
00098         int win_priority =  ::GetThreadPriority(reinterpret_cast<HANDLE>(this->_handle));
00099         
00100         if (win_priority == THREAD_PRIORITY_ERROR_RETURN)
00101         {
00102                 return PRIORITY_NORMAL;
00103         }
00104 
00105         if (win_priority >= THREAD_PRIORITY_TIME_CRITICAL )
00106         {
00107                 return PRIORITY_REALTIME;
00108         }
00109         else if (win_priority<THREAD_PRIORITY_TIME_CRITICAL && win_priority>=THREAD_PRIORITY_ABOVE_NORMAL)
00110         {       
00111                 return PRIORITY_HIGH;
00112         }
00113         else if (win_priority<THREAD_PRIORITY_ABOVE_NORMAL && win_priority>THREAD_PRIORITY_BELOW_NORMAL)
00114         {
00115                 return PRIORITY_NORMAL;
00116         }else if (win_priority<=THREAD_PRIORITY_BELOW_NORMAL && win_priority>THREAD_PRIORITY_IDLE)
00117         {
00118                 return PRIORITY_LOW;
00119         }else if (win_priority<=THREAD_PRIORITY_IDLE)
00120         {
00121                 return PRIORITY_IDLE;
00122         }
00123         return PRIORITY_NORMAL;
00124 }
00125 
00126 u_result Thread::join(unsigned long timeout)
00127 {
00128     if (!this->_handle) return RESULT_OK;
00129     switch ( WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout))
00130     {
00131     case WAIT_OBJECT_0:
00132         CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
00133         this->_handle = NULL;
00134         return RESULT_OK;
00135     case WAIT_ABANDONED:
00136         return RESULT_OPERATION_FAIL;
00137     case WAIT_TIMEOUT:
00138         return RESULT_OPERATION_TIMEOUT;
00139     }
00140 
00141     return RESULT_OK;
00142 }
00143 
00144 }}


rplidar_ros
Author(s):
autogenerated on Mon Mar 18 2019 02:34:23