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