| 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 | #pragma once |
| 36 | |
| 37 | #include "hal/types.h" |
| 38 | #define CLASS_THREAD(c , x ) \ |
| 39 | rp::hal::Thread::create_member<c, &c::x>(this ) |
| 40 | |
| 41 | namespace rp{ namespace hal{ |
| 42 | |
| 43 | class Thread |
| 44 | { |
| 45 | public: |
| 46 | enum priority_val_t |
| 47 | { |
| 48 | PRIORITY_REALTIME = 0, |
| 49 | PRIORITY_HIGH = 1, |
| 50 | PRIORITY_NORMAL = 2, |
| 51 | PRIORITY_LOW = 3, |
| 52 | PRIORITY_IDLE = 4, |
| 53 | }; |
| 54 | |
| 55 | template <class T, u_result (T::*PROC)(void)> |
| 56 | static Thread create_member(T * pthis) |
| 57 | { |
| 58 | return create(_thread_thunk<T,PROC>, pthis); |
| 59 | } |
| 60 | |
| 61 | template <class T, u_result (T::*PROC)(void) > |
| 62 | static _word_size_t THREAD_PROC _thread_thunk(void * data) |
| 63 | { |
| 64 | return (static_cast<T *>(data)->*PROC)(); |
| 65 | } |
| 66 | static Thread create(thread_proc_t proc, void * data = NULL ); |
| 67 | |
| 68 | public: |
| 69 | ~Thread() { } |
| 70 | Thread(): _data(NULL),_func(NULL),_handle(0) {} |
| 71 | _word_size_t getHandle(){ return _handle;} |
| 72 | u_result terminate(); |
| 73 | void *getData() { return _data;} |
| 74 | u_result join(unsigned long timeout = -1); |
| 75 | |
| 76 | // disabled as on platforms like Linux, the priority will be inherited by the child thread |
| 77 | // which may caused unexpected behavior. |
| 78 | // Please using Thread::SetSelfPriority instead |
| 79 | // u_result setPriority( priority_val_t p); |
| 80 | priority_val_t getPriority(); |
| 81 | |
| 82 | static u_result SetSelfPriority(priority_val_t p); |
| 83 | |
| 84 | |
| 85 | bool operator== ( const Thread & right) { return this->_handle == right._handle; } |
| 86 | protected: |
| 87 | Thread( thread_proc_t proc, void * data ): _data(data),_func(proc), _handle(0) {} |
| 88 | void * _data; |
| 89 | thread_proc_t _func; |
| 90 | _word_size_t _handle; |
| 91 | }; |
| 92 | |
| 93 | }} |
| 94 |