| 1 | /* |
| 2 | * RPLIDAR SDK |
| 3 | * |
| 4 | * Copyright (c) 2009 - 2014 RoboPeak Team |
| 5 | * http://www.robopeak.com |
| 6 | * Copyright (c) 2014 - 2020 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 "sdkcommon.h" |
| 36 | #include <process.h> |
| 37 | |
| 38 | namespace rp{ namespace hal{ |
| 39 | |
| 40 | Thread Thread::create(thread_proc_t proc, void * data) |
| 41 | { |
| 42 | Thread newborn(proc, data); |
| 43 | |
| 44 | newborn._handle = (_word_size_t)( |
| 45 | _beginthreadex(NULL, 0, (unsigned int (_stdcall * )( void * ))proc, |
| 46 | data, 0, NULL)); |
| 47 | return newborn; |
| 48 | } |
| 49 | |
| 50 | u_result Thread::terminate() |
| 51 | { |
| 52 | if (!this->_handle) return RESULT_OK; |
| 53 | if (TerminateThread( reinterpret_cast<HANDLE>(this->_handle), -1)) |
| 54 | { |
| 55 | CloseHandle(reinterpret_cast<HANDLE>(this->_handle)); |
| 56 | this->_handle = NULL; |
| 57 | return RESULT_OK; |
| 58 | }else |
| 59 | { |
| 60 | return RESULT_OPERATION_FAIL; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | u_result Thread::SetSelfPriority( priority_val_t p) |
| 65 | { |
| 66 | HANDLE selfHandle = GetCurrentThread(); |
| 67 | |
| 68 | int win_priority = THREAD_PRIORITY_NORMAL; |
| 69 | switch(p) |
| 70 | { |
| 71 | case PRIORITY_REALTIME: |
| 72 | win_priority = THREAD_PRIORITY_TIME_CRITICAL; |
| 73 | break; |
| 74 | case PRIORITY_HIGH: |
| 75 | win_priority = THREAD_PRIORITY_HIGHEST; |
| 76 | break; |
| 77 | case PRIORITY_NORMAL: |
| 78 | win_priority = THREAD_PRIORITY_NORMAL; |
| 79 | break; |
| 80 | case PRIORITY_LOW: |
| 81 | win_priority = THREAD_PRIORITY_LOWEST; |
| 82 | break; |
| 83 | case PRIORITY_IDLE: |
| 84 | win_priority = THREAD_PRIORITY_IDLE; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | if (SetThreadPriority(selfHandle, win_priority)) |
| 89 | { |
| 90 | return RESULT_OK; |
| 91 | } |
| 92 | return RESULT_OPERATION_FAIL; |
| 93 | } |
| 94 | |
| 95 | Thread::priority_val_t Thread::getPriority() |
| 96 | { |
| 97 | if (!this->_handle) return PRIORITY_NORMAL; |
| 98 | int win_priority = ::GetThreadPriority(reinterpret_cast<HANDLE>(this->_handle)); |
| 99 | |
| 100 | if (win_priority == THREAD_PRIORITY_ERROR_RETURN) |
| 101 | { |
| 102 | return PRIORITY_NORMAL; |
| 103 | } |
| 104 | |
| 105 | if (win_priority >= THREAD_PRIORITY_TIME_CRITICAL ) |
| 106 | { |
| 107 | return PRIORITY_REALTIME; |
| 108 | } |
| 109 | else if (win_priority<THREAD_PRIORITY_TIME_CRITICAL && win_priority>=THREAD_PRIORITY_ABOVE_NORMAL) |
| 110 | { |
| 111 | return PRIORITY_HIGH; |
| 112 | } |
| 113 | else if (win_priority<THREAD_PRIORITY_ABOVE_NORMAL && win_priority>THREAD_PRIORITY_BELOW_NORMAL) |
| 114 | { |
| 115 | return PRIORITY_NORMAL; |
| 116 | }else if (win_priority<=THREAD_PRIORITY_BELOW_NORMAL && win_priority>THREAD_PRIORITY_IDLE) |
| 117 | { |
| 118 | return PRIORITY_LOW; |
| 119 | }else if (win_priority<=THREAD_PRIORITY_IDLE) |
| 120 | { |
| 121 | return PRIORITY_IDLE; |
| 122 | } |
| 123 | return PRIORITY_NORMAL; |
| 124 | } |
| 125 | |
| 126 | u_result Thread::join(unsigned long timeout) |
| 127 | { |
| 128 | if (!this->_handle) return RESULT_OK; |
| 129 | switch ( WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout)) |
| 130 | { |
| 131 | case WAIT_OBJECT_0: |
| 132 | CloseHandle(reinterpret_cast<HANDLE>(this->_handle)); |
| 133 | this->_handle = NULL; |
| 134 | return RESULT_OK; |
| 135 | case WAIT_ABANDONED: |
| 136 | return RESULT_OPERATION_FAIL; |
| 137 | case WAIT_TIMEOUT: |
| 138 | return RESULT_OPERATION_TIMEOUT; |
| 139 | } |
| 140 | |
| 141 | return RESULT_OK; |
| 142 | } |
| 143 | |
| 144 | }} |