Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / include / native_service / ns_utility_sys.hpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \brief    This file contains declaration of common APIs for NS_UtilityCenter.
19 ///
20 ////////////////////////////////////////////////////////////////////////////////////////////////////
21
22 /**
23  * @file ns_utility_sys.hpp
24  */
25
26 #ifndef __NSFRAMEWORK_NSUTILITY_NSUTILITYSYS__  // NOLINT  (build/header_guard)
27 #define __NSFRAMEWORK_NSUTILITY_NSUTILITYSYS__
28
29 #include <native_service/frameworkunified_types.h>
30 #include <native_service/ns_utility.hpp>
31 #include <pthread.h>
32 #include <errno.h>
33
34 /** @addtogroup BaseSystem
35  *  @{
36  */
37 /** @addtogroup native_service
38  *  @ingroup BaseSystem
39  *  @{
40  */
41 /** @addtogroup framework_unified
42  *  @ingroup native_service
43  *  @{
44  */
45 /** @addtogroup native
46  *  @ingroup framework_unified
47  *  @{
48  */
49
50 // Lock Wrapper ////////////////////////////////////////////////////////////////
51
52 /**
53  * \~english define the lock_error class inherit from std::runtime_error
54  */
55 DEFINE_EXCEPTION(lock_error, std::runtime_error);
56
57 /**
58  * \~english define the lock_creation_error class inherit from lock_error
59  */
60 DEFINE_EXCEPTION(lock_creation_error, lock_error);
61
62 /**
63  * \~english define the lock_acquireread_error class inherit from lock_error
64  */
65 DEFINE_EXCEPTION(lock_acquireread_error, lock_error);
66
67 /**
68  * \~english define the lock_acquirewrite_error class inherit from lock_error
69  */
70 DEFINE_EXCEPTION(lock_acquirewrite_error, lock_error);
71
72 /**
73  * \~english define the lock_release_error class inherit from lock_error
74  */
75 DEFINE_EXCEPTION(lock_release_error, lock_error);
76
77
78 /**
79  * @class CMutex
80  * @brief \~english Mutex Lock Class
81  * @par   \~english Brief Introduction
82  * \~english This class defines locking policy for Mutexes.
83  *
84  */
85 class CMutex {
86  public:
87   ////////////////////////////////////////////////////////////////////////////////////////////////
88   /// \ingroup NS_UtilityCenter
89   /// \~english @par Brief
90   ///       Construct a CMutex object.
91   /// \~english @par Prerequisite
92   ///       - None
93   /// \~english @par Change of internal state
94   ///       - None
95   /// \~english @par Conditions of processing failure
96   ///       - None
97   /// \~english @par Classification
98   ///       Public
99   /// \~english @par Type
100   ///       Sync Only
101   /// \~english @par Detail
102   ///       The function constructs a CMutex object.
103   /// \~english @see
104   ////////////////////////////////////////////////////////////////////////////////////////////////
105   CMutex();
106
107   ////////////////////////////////////////////////////////////////////////////////////////////////
108   /// \ingroup NS_UtilityCenter
109   /// \~english @par Brief
110   ///       Destruct the CMutex object.
111   /// \~english @par Prerequisite
112   ///       - Construct a CMutex object
113   /// \~english @par Change of internal state
114   ///       - None
115   /// \~english @par Conditions of processing failure
116   ///       - None
117   /// \~english @par Classification
118   ///       Public
119   /// \~english @par Type
120   ///       Sync Only
121   /// \~english @par Detail
122   ///       The function destructs the CMutex object.
123   /// \~english @see CMutex()
124   ////////////////////////////////////////////////////////////////////////////////////////////////
125   ~CMutex();
126
127   ////////////////////////////////////////////////////////////////////////////////////////////////
128   /// \ingroup NS_UtilityCenter
129   /// \~english @par Brief
130   ///       Lock the CMutex object when reading.
131   /// \~english @retval void
132   ///
133   /// \~english @par Prerequisite
134   ///       - Construct a CMutex object
135   /// \~english @par Change of internal state
136   ///       - None
137   /// \~english @par Conditions of processing failure
138   ///       - None
139   /// \~english @par Classification
140   ///       Public
141   /// \~english @par Type
142   ///       Sync Only
143   /// \~english @par Detail
144   ///       The function locks the CMutex object when reading.
145   /// \~english @see CMutex(), WriteLock(), UnLock()
146   ////////////////////////////////////////////////////////////////////////////////////////////////
147   void ReadLock();
148
149   ////////////////////////////////////////////////////////////////////////////////////////////////
150   /// \ingroup NS_UtilityCenter
151   /// \~english @par Brief
152   ///       Lock the CMutex object when writing.
153   /// \~english @retval void
154   ///
155   /// \~english @par Prerequisite
156   ///       - Construct a CMutex object
157   /// \~english @par Change of internal state
158   ///       - None
159   /// \~english @par Conditions of processing failure
160   ///       - None
161   /// \~english @par Classification
162   ///       Public
163   /// \~english @par Type
164   ///       Sync Only
165   /// \~english @par Detail
166   ///       The function locks the CMutex object when writing.
167   /// \~english @see CMutex(), ReadLock(), UnLock()
168   ////////////////////////////////////////////////////////////////////////////////////////////////
169   void WriteLock();
170
171   ////////////////////////////////////////////////////////////////////////////////////////////////
172   /// \ingroup NS_UtilityCenter
173   /// \~english @par Brief
174   ///       Open the lock of the CMutex object.
175   /// \~english @retval void
176   ///
177   /// \~english @par Prerequisite
178   ///       - Construct a CMutex object
179   /// \~english @par Change of internal state
180   ///       - None
181   /// \~english @par Conditions of processing failure
182   ///       - None
183   /// \~english @par Classification
184   ///       Public
185   /// \~english @par Type
186   ///       Sync Only
187   /// \~english @par Detail
188   ///       The function opens the lock of the CMutex object.
189   /// \~english @see CMutex(), ReadLock(), WriteLock()
190   ////////////////////////////////////////////////////////////////////////////////////////////////
191   void Unlock();
192
193  private:
194
195   pthread_mutex_t m_mtx;
196 };
197
198 /**
199  * @class CRWLock
200  * @brief \~english Read/Write Lock Class
201  * @par   \~english Brief Introduction
202  * \~english This class defines locking policy for reader/writer.
203  *
204  */
205 class CRWLock {
206  public:
207   ////////////////////////////////////////////////////////////////////////////////////////////////
208   /// \ingroup NS_UtilityCenter
209   /// \~english @par Brief
210   ///       - Construct a CRWLock object.
211   /// \~english @par Prerequisite
212   ///       - None
213   /// \~english @par Change of internal state
214   ///       - None
215   /// \~english @par Conditions of processing failure
216   ///       - None
217   /// \~english @par Classification
218   ///       Public
219   /// \~english @par Type
220   ///       Sync Only
221   /// \~english @par Detail
222   ///       The function constructs a CRWLock object.
223   /// \~english @see ~CRWLock()
224   ////////////////////////////////////////////////////////////////////////////////////////////////
225   CRWLock();
226
227   ////////////////////////////////////////////////////////////////////////////////////////////////
228   /// \ingroup NS_UtilityCenter
229   /// \~english @par Brief
230   ///       Destruct the CRWLock object.
231   /// \~english @par Prerequisite
232   ///       - Construct a CRWLock object.
233   /// \~english @par Change of internal state
234   ///       - None
235   /// \~english @par Conditions of processing failure
236   ///       - None
237   /// \~english @par Classification
238   ///       Public
239   /// \~english @par Type
240   ///       Sync Only
241   /// \~english @par Detail
242   ///       The function destructs the CRWLock object.
243   /// \~english @see CRWLock()
244   ////////////////////////////////////////////////////////////////////////////////////////////////
245   ~CRWLock();
246
247   ////////////////////////////////////////////////////////////////////////////////////////////////
248   /// \ingroup NS_UtilityCenter
249   /// \~english @par Brief
250   ///       Lock the CRWLock object when reading.
251   /// \~english @retval void
252   ///
253   /// \~english @par Prerequisite
254   ///       - Construct a CRWLock object.
255   /// \~english @par Change of internal state
256   ///       - None
257   /// \~english @par Conditions of processing failure
258   ///       - None
259   /// \~english @par Classification
260   ///       Public
261   /// \~english @par Type
262   ///       Sync Only
263   /// \~english @par Detail
264   ///       The function locks the CRWLock object when reading.
265   /// \~english @see CRWLock(), WriteLock(), UnLoack()
266   ////////////////////////////////////////////////////////////////////////////////////////////////
267   void ReadLock();
268
269   ////////////////////////////////////////////////////////////////////////////////////////////////
270   /// \ingroup NS_UtilityCenter
271   /// \~english @par Brief
272   ///       Lock the CRWLock object when writing.
273   /// \~english @retval void
274   ///
275   /// \~english @par Prerequisite
276   ///       - Construct a CRWLock object.
277   /// \~english @par Change of internal state
278   ///       - None
279   /// \~english @par Conditions of processing failure
280   ///       - None
281   /// \~english @par Classification
282   ///       Public
283   /// \~english @par Type
284   ///       Sync Only
285   /// \~english @par Detail
286   ///       The function locks the CRWLock object when writing.
287   /// \~english @see CRWLock(), ReadLock(), UnLock()
288   ////////////////////////////////////////////////////////////////////////////////////////////////
289   void WriteLock();
290
291   ////////////////////////////////////////////////////////////////////////////////////////////////
292   /// \ingroup NS_UtilityCenter
293   /// \~english @par Brief
294   ///       Open the lock of CRWLock object
295   /// \~english @retval void
296   ///
297   /// \~english @par Prerequisite
298   ///       - Construct a CRWLock object.
299   /// \~english @par Change of internal state
300   ///       - None
301   /// \~english @par Conditions of processing failure
302   ///       - None
303   /// \~english @par Classification
304   ///       Public
305   /// \~english @par Type
306   ///       Sync Only
307   /// \~english @par Detail
308   ///       The function opens the lock of the CRWLock object.
309   /// \~english @see CRWLock(), ReadLock(), WriteLock()
310   ////////////////////////////////////////////////////////////////////////////////////////////////
311   void Unlock();
312
313  private:
314   pthread_rwlock_t m_rwl;
315 };
316
317 #endif  // __NSFRAMEWORK_NSUTILITY_NSUTILITYSYS__  // NOLINT  (build/header_guard)
318 /** @}*/  // end of native
319 /** @}*/  // end of framework_unified
320 /** @}*/  // end of native_service
321 /** @}*/  // end of BaseSystem