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.
17 #include <native_service/ns_utility_if.h>
18 #include <native_service/ns_utility_sys.hpp>
22 #include "ns_utility_sys_internal.hpp"
23 #include "ns_utility_if_internal.h"
24 // Random functions ////////////////////////////////////////////////////
25 void utility_sys_seedrand() { // NOLINT (readability/nolint)
26 static bool s_seeded = false;
29 srand48(std::time(NULL));
33 UI_32 utility_sys_rand() { // NOLINT (readability/nolint)
34 utility_sys_seedrand();
35 return static_cast< UI_32 >(mrand48());
38 // Lock wrapper implementation /////////////////////////////////////
40 ///////////////////////////////////////////
41 // Implements locking policy for mutexes //
42 ///////////////////////////////////////////
44 // CMutex helper function
45 template< class TErr >
46 void MutexLock(pthread_mutex_t *mtx) {
47 if (EOK != pthread_mutex_lock(mtx)) {
53 pthread_mutex_t mtx_tmp = PTHREAD_MUTEX_INITIALIZER;
54 std::memcpy(&m_mtx, &mtx_tmp, sizeof(m_mtx));
55 if (EOK != pthread_mutex_init(&m_mtx, NULL)) {
56 throw lock_creation_error();
61 pthread_mutex_destroy(&m_mtx);
64 void CMutex::ReadLock() {
65 MutexLock< lock_acquireread_error >(&m_mtx);
68 void CMutex::WriteLock() {
69 MutexLock< lock_acquirewrite_error >(&m_mtx);
72 void CMutex::Unlock() {
73 if (EOK != pthread_mutex_unlock(&m_mtx)) {
74 throw lock_release_error("");
79 /////////////////////////////////////////////////////////
80 // implements locking policy for reader / writer locks //
81 /////////////////////////////////////////////////////////
83 pthread_rwlock_t rw_tmp = PTHREAD_RWLOCK_INITIALIZER;
84 std::memcpy(&m_rwl, &rw_tmp, sizeof(m_rwl));
85 if (EOK != pthread_rwlock_init(&m_rwl, NULL)) {
86 throw lock_creation_error();
91 pthread_rwlock_destroy(&m_rwl);
94 void CRWLock::ReadLock() {
95 if (EOK != pthread_rwlock_rdlock(&m_rwl)) {
96 throw lock_acquireread_error();
100 void CRWLock::WriteLock() {
101 if (EOK != pthread_rwlock_wrlock(&m_rwl)) {
102 throw lock_acquirewrite_error();
106 void CRWLock::Unlock() {
107 if (EOK != pthread_rwlock_unlock(&m_rwl)) {
108 throw lock_release_error();
112 /////////////////////////////////////////////////////////
113 // implements GNU Builtins for counting leading zeros //
114 /////////////////////////////////////////////////////////
116 // returns the number of leading zeros in a given 16bit value
117 SI_16 NS_CountLeadingZeros_16Bit(UI_16 f_ui_val) { // NOLINT (readability/nolint)
118 return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_clz(f_ui_val)); // NOLINT (readability/nolint)
121 // returns the number of leading zeros in a given 32bit value
122 SI_16 NS_CountLeadingZeros_32Bit(UI_32 f_ui_val) { // NOLINT (readability/nolint)
123 return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_clzl(f_ui_val)); // NOLINT (readability/nolint)
126 // returns the number of leading zeros in a given 64bit value
127 SI_16 NS_CountLeadingZeros_64Bit(UI_64 f_ui_val) { // NOLINT (readability/nolint)
128 return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_clzll(f_ui_val)); // NOLINT (readability/nolint)
131 // returns the number of trailing zeros in a given 16bit value
132 SI_16 NS_CountTrailingZeros_16Bit(UI_16 f_ui_val) { // NOLINT (readability/nolint)
133 return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_ctz(f_ui_val)); // NOLINT (readability/nolint)
136 // returns the number of trailing zeros in a given 32bit value
137 SI_16 NS_CountTrailingZeros_32Bit(UI_32 f_ui_val) { // NOLINT (readability/nolint)
138 return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_ctzl(f_ui_val)); // NOLINT (readability/nolint)
141 // returns the number of trailing zeros in a given 64bit value
142 SI_16 NS_CountTrailingZeros_64Bit(UI_64 f_ui_val) { // NOLINT (readability/nolint)
143 return static_cast<SI_16>((f_ui_val == 0) ? NS_INVALID_RETURN : __builtin_ctzll(f_ui_val)); // NOLINT (readability/nolint)