2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef _cl_lock_h_ // NOLINT(build/header_guard)
17 #define _cl_lock_h_ // NOLINT(build/header_guard)
27 * @brief \~english This file contains the base api of cl_clock.
30 /** @addtogroup BaseSystem
33 /** @addtogroup native_service
37 /** @addtogroup common_library
38 * @ingroup native_service
42 ////////////////////////////////////////////////////////////////////////////////////
43 /// \ingroup CL_LockSystemInit
44 /// \~english @par Brief
45 /// Initialize the system lock.
46 /// \~english @retval 0 Success
47 /// \~english @retval -1 Error
48 /// \~english @par Prerequisite
49 /// - Prerequisites are nothing.
50 /// \~english @par Change of internal state
51 /// - Change of internal state according to the API does not occur.
52 /// \~english @par Conditions of processing failure
53 /// - On system call shm_open error, -1 is returned.
54 /// - On system call ftrancate error, -1 is returned.
55 /// - On system call mmap error, -1 is returned.
56 /// - On system call pthread_mutexattr_setpshared error, -1 is returned.
57 /// \~english @par Detail
58 /// This function will generate the Lock file, and initialize all the pthread_mutex_t slots.\n
59 /// This function must to be called in the system once, which is recommended on the SystemManager start.\n
61 /// Lock file is formed with lots of slots. Every slot occupies 4KB.
62 /// The slot layout is:
63 /// 0 ~ 4Byte : field of PID
64 /// 4Byte ~ 28Byte : field of pthread_mutex_t
65 /// \~english @par Classification
67 /// \~english @par Type
71 ////////////////////////////////////////////////////////////////////////////////////
72 int32_t CL_LockSystemInit(void); // NOLINT(readability/nolint)
74 ////////////////////////////////////////////////////////////////////////////////////
75 /// \ingroup CL_LockProcessInit
76 /// \~english @par Brief
77 /// Initialize the process Lock file.
78 /// \~english @retval 0 Success
79 /// \~english @retval -1 Error
80 /// \~english @par Prerequisite
81 /// Should call CL_LockSystemInit() to generate Lock file.
82 /// \~english @par Change of internal state
83 /// - Change of internal state according to the API does not occur.
84 /// \~english @par Conditions of processing failure
85 /// - On system call shm_open error, -1 is returned.
86 /// \~english @par Detail
87 /// Open the Lock file.\n
88 /// If process want to use the Lock, this function should be called.\n
89 /// It is recommended that this function should be called since start main function.
90 /// \~english @par Classification
92 /// \~english @par Type
96 ////////////////////////////////////////////////////////////////////////////////////
97 int32_t CL_LockProcessInit(void); // NOLINT(readability/nolint)
99 ////////////////////////////////////////////////////////////////////////////////////
100 /// \ingroup CL_LockMap
101 /// \~english @par Brief
102 /// Mapping the Lock information address.
103 /// \~english @param [in] lid
104 /// int32_t - LockID of the Lock used.(0~LID_NUM) \n
105 /// \~english @retval addr Lock address
106 /// \~english @retval MAP_FAILED Error (errno)
107 /// \~english @par Prerequisite
108 /// Should call CL_LockSystemInit(), CL_LockProcessInit() to open the Lock file.
109 /// \~english @par Change of internal state
110 /// - Change of internal state according to the API does not occur.
111 /// \~english @par Conditions of processing failure
112 /// - If LockID(lid) < 0, errno is set as EINVAL, and MAP_FAILED is returned
113 /// - If LockID(lid) > LID_NUM, errno is set as EINVAL, and MAP_FAILED is returned
114 /// - If systemcall mmap is failure, errno is set euqal with the errno of mmap, and MAP_FAILED is returned
115 /// \~english @par Detail
116 /// Mapping the Lock information to the memory.\n
117 /// The related LockID should be assigned, when apply the share memory.\n
118 /// \~english @par Classification
120 /// \~english @par Type
124 ////////////////////////////////////////////////////////////////////////////////////
125 void *CL_LockMap(int32_t lid); // NOLINT(readability/nolint)
127 ////////////////////////////////////////////////////////////////////////////////////
128 /// \ingroup CL_LockUnmap
129 /// \~english @par Brief
130 /// Unmapping the Lock information.
131 /// \~english @param [in] addr
132 /// void* - Lock information address.
133 /// \~english @retval 0 Success
134 /// \~english @retval -1 Error
135 /// \~english @par Prerequisite
136 /// Should call CL_LockSystemInit(), CL_LockProcessInit() before this function is called
137 /// \~english @par Change of internal state
138 /// - Change of internal state according to the API does not occur.
139 /// \~english @par Conditions of processing failure
140 /// - If systemcall munmap is failure, errno is set euqal with the errno of munmap, -1 is returned
141 /// \~english @par Detail
142 /// 0 would be returned, even if not get the Lock.
143 /// The addr is the Lock information address that acquired from CL_LockMap.
144 /// \~english @par Classification
146 /// \~english @par Type
150 ////////////////////////////////////////////////////////////////////////////////////
151 int32_t CL_LockUnmap(void *addr); // NOLINT(readability/nolint)
153 ////////////////////////////////////////////////////////////////////////////////////
154 /// \ingroup CL_LockGet
155 /// \~english @par Brief
156 /// Get the Lock. Block until get the Lock.
157 /// \~english @param [in] addr
158 /// void* - Lock information address.
159 /// \~english @retval 0 Success
160 /// \~english @retval EINVAL Invalid parameter
161 /// \~english @retval EDEADLK Mutex already locked
162 /// \~english @par Prerequisite
163 /// Should call CL_LockMap to get the Lock information address.
164 /// \~english @par Change of internal state
165 /// - Change of internal state according to the API does not occur.
166 /// \~english @par Conditions of processing failure
167 /// - EINVAL as the parameter of Lock information address is NULL
168 /// - EINVAL as the parameter of Lock information address is MAP_FAILED
169 /// - EINVAL as the mutex does not be initialized in the systemcall pthread_mutex_lock failure.
170 /// - EDEADLK as the current thread already owns the mutex in the systemcall pthread_mutex_lock failure.
171 /// \~english @par Detail
172 /// Get the Lock until other threads release it.
173 /// It will be deadlock if the current thread already owns the mutex.
174 /// The addr is the Lock information address that should be acquired from CL_LockMap.
175 /// \~english @par Classification
177 /// \~english @par Type
181 ////////////////////////////////////////////////////////////////////////////////////
182 int32_t CL_LockGet(void *addr); // NOLINT(readability/nolint)
184 ////////////////////////////////////////////////////////////////////////////////////
185 /// \ingroup CL_LockNowait
186 /// \~english @par Brief
187 /// Try to get Lock, shall immediately return.
188 /// \~english @param [in] addr
189 /// void* - Lock information address.
190 /// \~english @retval 0 Success
191 /// \~english @retval EINVAL Invalid parameter
192 /// \~english @retval EBUSY Busy
193 /// \~english @par Prerequisite
194 /// Should call CL_LockMap to get the Lock information address.
195 /// \~english @par Change of internal state
196 /// - Change of internal state according to the API does not occur.
197 /// \~english @par Conditions of processing failure
198 /// - EINVAL as input parameter addr, the Lock information address is NULL
199 /// - EINVAL as input parameter addr, the Lock information address if MAP_FAILED
200 /// - EINVAL as the mutex does not be initialized in the systemcall pthread_mutex_trylock failure.
201 /// - EBUSY as the mutex could not be acquired as it was already locked in the
202 /// systemcall pthread_mutex_trylock failure.
203 /// \~english @par Detail
204 /// The addr is the Lock information address that should be acquired from CL_LockMap.
205 /// Get the Lock information from the pointed share memory.
206 /// Shall immediately return while not acquire the Lock.
207 /// \~english @par Classification
209 /// \~english @par Type
213 ////////////////////////////////////////////////////////////////////////////////////
214 int32_t CL_LockNowait(void *addr); // NOLINT(readability/nolint)
216 ////////////////////////////////////////////////////////////////////////////////////
217 /// \ingroup CL_LockRelease
218 /// \~english @par Brief
219 /// Release the Lock.
220 /// \~english @param [in] addr
221 /// addr - Lock information address.
222 /// \~english @retval 0 Success
223 /// \~english @retval EINVAL Invalid parameter
224 /// \~english @retval EPERM The current thread does not own the mutex
225 /// \~english @par Prerequisite
226 /// - Should call CL_LockMap to get the Lock information address.
227 /// \~english @par Change of internal state
228 /// - Change of internal state according to the API does not occur.
229 /// \~english @par Conditions of processing failure
230 /// - EINVAL as input parameter addr, the Lock information address is NULL
231 /// - EINVAL as input parameter addr, the Lock information address if MAP_FAILED
232 /// - EINVAL as the mutex does not be initialized in the systemcall pthread_mutex_unlock failure.
233 /// - EPERM as the current thread does not own the mutex in the systemcall pthread_mutex_unlock failure.
234 /// \~english @par Detail
235 /// 0 would be returned, even if not get the Lock.
236 /// The addr is the Lock information address that should be acquired from CL_LockMap.
237 /// Release the Lock related with the share memory.
238 /// \~english @par Classification
240 /// \~english @par Type
243 /// CL_LockGet, CL_LockNowait
244 ////////////////////////////////////////////////////////////////////////////////////
245 int CL_LockRelease(void *addr); // NOLINT(readability/nolint)
252 /** @}*/ // end of common_library
253 /** @}*/ // end of NativeService
254 /** @}*/ // end of BaseSystem
255 #define LOCK_POS_MTX_RSV 40
256 #define LOCK_HRDS_RSV 15
257 #define LOCK_NSLOG_ACCES_IF_RSV 50
259 #endif // #ifndef _cl_lock_h_ // NOLINT(build/header_guard)