1 /* 2 * Copyright 2018 Rockchip Electronics Co. LTD 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * author: martin.cheng@rock-chips.com 17 * date: 20180719 18 */ 19 20 #include "rt_header.h" // NOLINT 21 22 #ifndef SRC_RT_BASE_INCLUDE_RT_THREAD_H_ 23 #define SRC_RT_BASE_INCLUDE_RT_THREAD_H_ 24 25 #define MAX_COND_TIMETOUT 33*1000 26 #define MIN_COND_TIMETOUT 5*1000 27 #define MAX_BIND_CPUS_NUM 32 28 29 // RtTaskSlot: atomic task, short time consumption 30 class RtRunnable { 31 public: ~RtRunnable()32 virtual ~RtRunnable() {} 33 virtual void runTask(void* args) = 0; 34 }; 35 36 typedef enum { 37 THREAD_IDLE = 0, 38 THREAD_LOOP, 39 THREAD_EXIT, 40 THREAD_MAX, 41 } ThreadState; 42 43 typedef enum _RTThreadSched { 44 RT_SCHED_OTHER, 45 RT_SCHED_RR, 46 RT_SCHED_FIFO, 47 RT_SCHED_MAX, 48 } RTThreadSched; 49 50 typedef enum _RTThreadInheritSched { 51 RT_INHERIT_SCHED, 52 RT_EXPLICIT_SCHED, 53 } RTThreadInheritSched; 54 55 class RtThread { 56 public: 57 // RtTaskSlot: atomic task, short time consumption 58 typedef void* (*RtTaskSlot)(void*); 59 60 explicit RtThread(RtTaskSlot taskslot, void* data = NULL); 61 explicit RtThread(RtRunnable* runnable, void* data = NULL); 62 63 /** 64 * Non-virtual, do not subclass. 65 */ 66 ~RtThread(); 67 68 /** 69 * Starts the thread. Returns false if the thread could not be started. 70 */ 71 RT_BOOL start(); 72 73 RT_BOOL postCond(const char* optName); 74 RT_BOOL waitCond(const char* optName); 75 RT_BOOL waitCond(const char* optName, UINT64 timeoutUs); 76 77 /** 78 * Set and Get Thread Name. 79 */ 80 void setName(const char* name); 81 const char* getName(); 82 INT32 getState(); 83 84 /** 85 * Waits for the thread to finish. 86 * If the thread has not started, returns immediately. 87 */ 88 void join(); 89 void requestInterruption(); 90 91 /* 92 * set schedule policy for thread 93 * there are three policy : SCHED_FIFO, SCHED_RR and SCHED_OTHER 94 */ 95 RT_RET setSchedPolicy(RTThreadSched schedPolicy); 96 RTThreadSched getSchedPolicy(); 97 RT_RET setInheritSched(RTThreadInheritSched inherit); 98 RTThreadInheritSched getInheritSched(); 99 INT32 setPriority(INT32 prior); 100 INT32 getPriority(); 101 INT32 getMinPriority(); 102 INT32 getMaxPriority(); 103 RT_RET bindCpus(INT32 *selectedCpus, INT32 size); 104 105 public: 106 static INT32 getThreadID(); 107 108 public: 109 void *mData; 110 RtMutex *mLock; 111 RtMutex *mCondLock; 112 RtCondition *mCondition; 113 }; 114 115 #endif // SRC_RT_BASE_INCLUDE_RT_THREAD_H_ 116