winthread.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 "sdkcommon.h"
00038 #include <process.h>
00039 
00040 namespace rp{ namespace hal{
00041 
00042 Thread Thread::create(thread_proc_t proc, void * data)
00043 {
00044     Thread newborn(proc, data);
00045 
00046     newborn._handle = (_word_size_t)( 
00047         _beginthreadex(NULL, 0, (unsigned int (_stdcall * )( void * ))proc,
00048                         data, 0, NULL));
00049     return newborn;
00050 }
00051 
00052 u_result Thread::terminate()
00053 {
00054     if (!this->_handle) return RESULT_OK;
00055     if (TerminateThread( reinterpret_cast<HANDLE>(this->_handle), -1))
00056     {
00057         CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
00058         this->_handle = NULL;
00059         return RESULT_OK;
00060     }else
00061     {
00062         return RESULT_OPERATION_FAIL;
00063     }
00064 }
00065 
00066 u_result Thread::setPriority( priority_val_t p)
00067 {
00068         if (!this->_handle) return RESULT_OPERATION_FAIL;
00069 
00070         int win_priority =  THREAD_PRIORITY_NORMAL;
00071         switch(p)
00072         {
00073         case PRIORITY_REALTIME:
00074                 win_priority = THREAD_PRIORITY_TIME_CRITICAL;
00075                 break;
00076         case PRIORITY_HIGH:
00077                 win_priority = THREAD_PRIORITY_HIGHEST;
00078                 break;
00079         case PRIORITY_NORMAL:
00080                 win_priority = THREAD_PRIORITY_NORMAL;
00081                 break;
00082         case PRIORITY_LOW:
00083                 win_priority = THREAD_PRIORITY_LOWEST;
00084                 break;
00085         case PRIORITY_IDLE:
00086                 win_priority = THREAD_PRIORITY_IDLE;
00087                 break;
00088         }
00089 
00090         if (SetThreadPriority(reinterpret_cast<HANDLE>(this->_handle), win_priority))
00091         {
00092                 return RESULT_OK;
00093         }
00094         return RESULT_OPERATION_FAIL;
00095 }
00096 
00097 Thread::priority_val_t Thread::getPriority()
00098 {
00099         if (!this->_handle) return PRIORITY_NORMAL;
00100         int win_priority =  ::GetThreadPriority(reinterpret_cast<HANDLE>(this->_handle));
00101         
00102         if (win_priority == THREAD_PRIORITY_ERROR_RETURN)
00103         {
00104                 return PRIORITY_NORMAL;
00105         }
00106 
00107         if (win_priority >= THREAD_PRIORITY_TIME_CRITICAL )
00108         {
00109                 return PRIORITY_REALTIME;
00110         }
00111         else if (win_priority<THREAD_PRIORITY_TIME_CRITICAL && win_priority>=THREAD_PRIORITY_ABOVE_NORMAL)
00112         {       
00113                 return PRIORITY_HIGH;
00114         }
00115         else if (win_priority<THREAD_PRIORITY_ABOVE_NORMAL && win_priority>THREAD_PRIORITY_BELOW_NORMAL)
00116         {
00117                 return PRIORITY_NORMAL;
00118         }else if (win_priority<=THREAD_PRIORITY_BELOW_NORMAL && win_priority>THREAD_PRIORITY_IDLE)
00119         {
00120                 return PRIORITY_LOW;
00121         }else if (win_priority<=THREAD_PRIORITY_IDLE)
00122         {
00123                 return PRIORITY_IDLE;
00124         }
00125         return PRIORITY_NORMAL;
00126 }
00127 
00128 u_result Thread::join(unsigned long timeout)
00129 {
00130     if (!this->_handle) return RESULT_OK;
00131     switch ( WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout))
00132     {
00133     case WAIT_OBJECT_0:
00134         CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
00135         this->_handle = NULL;
00136         return RESULT_OK;
00137     case WAIT_ABANDONED:
00138         return RESULT_OPERATION_FAIL;
00139     case WAIT_TIMEOUT:
00140         return RESULT_OPERATION_TIMEOUT;
00141     }
00142 
00143     return RESULT_OK;
00144 }
00145 
00146 }}


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