xref: /OK3568_Linux_fs/external/rockit/tgi/sdk/include/rt_mutex.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 INCLUDE_RT_BASE_RT_MUTEX_H_
23 #define INCLUDE_RT_BASE_RT_MUTEX_H_
24 
25 #define RET_CHECK_REF(ref, ret) \
26     do { \
27         if (ref->isExited()) \
28             return ret; \
29     } while (0)
30 
31 
32 typedef void *(*RTPthreadCallback)(void *);
33 
34 class RtMutex;
35 class RtCondition;
36 
37 /*
38  * for shorter type name and function name
39  */
40 class RtMutex {
41  public:
42     RtMutex();
43     ~RtMutex();
44 
45     void lock();
46     void unlock();
47     int  trylock();
48 
49     class RtAutolock {
50      public:
RtAutolock(RtMutex & rtMutex)51         explicit inline RtAutolock(RtMutex& rtMutex)     // NOLINT
52                 : mLock(rtMutex) {
53             mLock.lock();
54         }
RtAutolock(RtMutex * rtMutex)55         explicit inline RtAutolock(RtMutex* rtMutex)
56                 : mLock(*rtMutex) {
57             mLock.lock();
58         }
~RtAutolock()59         inline ~RtAutolock() { mLock.unlock(); }
60 
61      private:
62         RtMutex& mLock;
63     };
64 
65  private:
66     friend class RtCondition;
67 
68     void* mData;
69 
70     RtMutex(const RtMutex &);
71     RtMutex &operator = (const RtMutex&);
72 };
73 
74 typedef RtMutex::RtAutolock RtAutoMutex;
75 
76 /*
77  * for shorter type name and function name
78  */
79 class RtCondition {
80  public:
81     RtCondition();
82     explicit RtCondition(int type);
83     ~RtCondition();
84 
85     /*These functions atomically release mutex,
86       but block on the condition variable*/
87     INT32 wait(const RtMutex& rtMutex);
88     INT32 wait(RtMutex* rtMutex);
89 
90     /*returns with the timeout error*/
91     INT32 timedwait(const RtMutex& rtMutex, UINT64 timeout_us);
92     INT32 timedwait(RtMutex* rtMutex, UINT64 timeout_us);
93 
94     /*This wakes up at least one thread blocked on the condition variable*/
95     INT32 signal();
96 
97     /*This wakes up all of the threads blocked on the condition variable*/
98     INT32 broadcast();
99 
100  private:
101     void* mData;
102 };
103 
104 class RtSemaphore {
105  public:
106     RtSemaphore();
107     ~RtSemaphore();
108 
109     INT32 init(INT32 pshared, UINT32 value);
110     INT32 getValue(int *val);
111     INT32 tryWait();
112     INT32 wait();
113     INT32 post();
114 
115  private:
116     void* mData;
117 };
118 
119 class RtReference {
120  public:
121     RtReference();
122     ~RtReference();
123     void addRef();
124     void decRef();
125     void wait();
126     void waitExit();
127     void reset();
128     RT_BOOL isExited();
129     INT32 getRefValue();
130 
131  private:
132     RT_BOOL       mIsExit;
133     INT32         mRefNum;
134     RtCondition  *mCond;
135     RtMutex      *mLock;
136 };
137 
138 class RtAutoRef {
139  public:
RtAutoRef(RtReference * ref)140     explicit inline RtAutoRef(RtReference* ref)
141             : mRef(ref) {
142        mRef->addRef();
143     }
144 
~RtAutoRef()145     inline ~RtAutoRef() {
146         mRef->decRef();
147     }
148 
149  private:
150     RtReference* mRef;
151 };
152 
153 
154 #endif  // INCLUDE_RT_BASE_RT_MUTEX_H_
155 
156