Init basesystem source codes.
[staging/basesystem.git] / nsframework / framework_unified / client / NS_UtilityCenter / src / ns_utility.cpp
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 #include <native_service/ns_utility_if.h>
18 #include <native_service/ns_utility_sys.hpp>
19 #include <stdlib.h>
20 #include <ctime>
21 #include <cstring>
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;
27   if (!s_seeded) {
28     s_seeded = true;
29     srand48(std::time(NULL));
30   }
31 }
32
33 UI_32 utility_sys_rand() {  // NOLINT (readability/nolint)
34   utility_sys_seedrand();
35   return static_cast< UI_32 >(mrand48());
36 }
37
38 // Lock wrapper implementation /////////////////////////////////////
39
40 ///////////////////////////////////////////
41 // Implements locking policy for mutexes //
42 ///////////////////////////////////////////
43
44 // CMutex helper function
45 template< class TErr >
46 void MutexLock(pthread_mutex_t *mtx) {
47   if (EOK != pthread_mutex_lock(mtx)) {
48     throw TErr("");
49   }
50 }
51
52 CMutex::CMutex() {
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();
57   }
58 }
59
60 CMutex::~CMutex() {
61   pthread_mutex_destroy(&m_mtx);
62 }
63
64 void CMutex::ReadLock() {
65   MutexLock< lock_acquireread_error >(&m_mtx);
66 }
67
68 void CMutex::WriteLock() {
69   MutexLock< lock_acquirewrite_error >(&m_mtx);
70 }
71
72 void CMutex::Unlock() {
73   if (EOK != pthread_mutex_unlock(&m_mtx)) {
74     throw lock_release_error("");
75   }
76 }
77
78
79 /////////////////////////////////////////////////////////
80 // implements locking policy for reader / writer locks //
81 /////////////////////////////////////////////////////////
82 CRWLock::CRWLock() {
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();
87   }
88 }
89
90 CRWLock::~CRWLock() {
91   pthread_rwlock_destroy(&m_rwl);
92 }
93
94 void CRWLock::ReadLock() {
95   if (EOK != pthread_rwlock_rdlock(&m_rwl)) {
96     throw lock_acquireread_error();
97   }
98 }
99
100 void CRWLock::WriteLock() {
101   if (EOK != pthread_rwlock_wrlock(&m_rwl)) {
102     throw lock_acquirewrite_error();
103   }
104 }
105
106 void CRWLock::Unlock() {
107   if (EOK != pthread_rwlock_unlock(&m_rwl)) {
108     throw lock_release_error();
109   }
110 }
111
112 /////////////////////////////////////////////////////////
113 // implements GNU Builtins for counting leading zeros  //
114 /////////////////////////////////////////////////////////
115
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)
119 }
120
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)
124 }
125
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)
129 }
130
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)
134 }
135
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)
139 }
140
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)
144 }