From: TaeminYeom Date: Mon, 18 Jul 2022 00:46:38 +0000 (+0900) Subject: Change filename "cmutex.h" to "lock.h" X-Git-Tag: accepted/tizen/unified/20220919.021608~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=39c20e36d8d0d005fe5b63805afa5912298ee670;p=platform%2Fcore%2Fapi%2Fsensor.git Change filename "cmutex.h" to "lock.h" Change-Id: I42689942d5b220244dffab30557d2ac2055b590d Signed-off-by: TaeminYeom --- diff --git a/src/api/sensor-internal.cpp b/src/api/sensor-internal.cpp index 35b20b5..77d3c58 100644 --- a/src/api/sensor-internal.cpp +++ b/src/api/sensor-internal.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include "sensor_reader.h" diff --git a/src/api/sensor_listener.h b/src/api/sensor_listener.h index f654353..eeb8065 100644 --- a/src/api/sensor_listener.h +++ b/src/api/sensor_listener.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/shared/channel.h b/src/shared/channel.h index dec1149..32a200c 100644 --- a/src/shared/channel.h +++ b/src/shared/channel.h @@ -28,7 +28,7 @@ #include "message.h" #include "event_loop.h" #include "channel_handler.h" -#include "cmutex.h" +#include "lock.h" namespace ipc { diff --git a/src/shared/cmutex.cpp b/src/shared/cmutex.cpp deleted file mode 100644 index c29aa72..0000000 --- a/src/shared/cmutex.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include -#include -#include -#include - -using namespace sensor; - -cmutex::cmutex() -{ - pthread_mutexattr_t mutex_attr; - pthread_mutexattr_init(&mutex_attr); - pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&m_mutex, &mutex_attr); - pthread_mutexattr_destroy(&mutex_attr); -} - -cmutex::~cmutex() -{ - pthread_mutex_destroy(&m_mutex); -} - -void cmutex::lock(void) -{ -#ifdef _LOCK_DEBUG - lock("mutex", __MODULE__, __func__, __LINE__); -#else - lock_impl(); -#endif -} - -void cmutex::lock(const char* expr, const char *module, const char *func, int line) -{ - int ret = 0; - char m_curent_info[OWNER_INFO_LEN]; - struct timeval sv; - unsigned long long lock_waiting_start_time = 0; - unsigned long long lock_acquired_time = 0; - unsigned long long waiting_time = 0; - - snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line); - - ret = try_lock_impl(); - - if (ret == 0) { - pthread_mutex_lock(&m_mutex); - snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); - pthread_mutex_unlock(&m_mutex); - return; - } - - gettimeofday(&sv, NULL); - lock_waiting_start_time = MICROSECONDS(sv); - - pthread_mutex_lock(&m_mutex); - _I("%s is waiting for getting %s(%p) owned in %s", - m_curent_info, expr, this, m_owner_info); - pthread_mutex_unlock(&m_mutex); - - lock_impl(); - - gettimeofday(&sv, NULL); - lock_acquired_time = MICROSECONDS(sv); - - waiting_time = lock_acquired_time - lock_waiting_start_time; - - pthread_mutex_lock(&m_mutex); - _I("%s acquires lock after waiting %lluus, %s(%p) was previously owned in %s", - m_curent_info, waiting_time, expr, this, m_owner_info); - snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); - pthread_mutex_unlock(&m_mutex); -} - -void cmutex::unlock(void) -{ - unlock_impl(); -} - -int cmutex::try_lock(void) -{ - return try_lock_impl(); -} - -int cmutex::lock_impl(void) -{ - return pthread_mutex_lock(&m_mutex); -} - -int cmutex::try_lock_impl(void) -{ - return pthread_mutex_trylock(&m_mutex); -} - -int cmutex::unlock_impl(void) -{ - return pthread_mutex_unlock(&m_mutex); -} - -Autolock::Autolock(cmutex &m, const char* expr, const char *module, const char *func, int line) -: m_lock(m) -{ - m_lock.lock(expr, module, func, line); -} - -Autolock::Autolock(cmutex &m) -: m_lock(m) -{ - m_lock.lock(); -} - -Autolock::~Autolock() -{ - m_lock.unlock(); -} diff --git a/src/shared/cmutex.h b/src/shared/cmutex.h deleted file mode 100644 index 91219a0..0000000 --- a/src/shared/cmutex.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2013 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef _CMUTEX_H_ -#define _CMUTEX_H_ - -#include - -namespace sensor { - -#ifndef MICROSECONDS -#define MICROSECONDS(tv) ((tv.tv_sec * 1000000ll) + tv.tv_usec) -#endif - -#ifdef _LOCK_DEBUG -#define AUTOLOCK(x) Autolock x##_autolock((x), #x, __MODULE__, __func__, __LINE__) -#define LOCK(x) (x).lock(#x, __MODULE__, __func__, __LINE__) -#define UNLOCK(x) (x).unlock() -#else -#define AUTOLOCK(x) Autolock x##_autolock((x)) -#define LOCK(x) (x).lock() -#define UNLOCK(x) (x).unlock() -#endif - -class cmutex { -public: - cmutex(); - virtual ~cmutex(); - - void lock(void); - void lock(const char* expr, const char *module, const char *func, int line); - void unlock(void); - int try_lock(void); - -protected: - int lock_impl(void); - int try_lock_impl(void); - int unlock_impl(void); - -private: - pthread_mutex_t m_mutex; - static const int OWNER_INFO_LEN = 256; - char m_owner_info[OWNER_INFO_LEN]; -}; - -class Autolock { -private: - cmutex& m_lock; -public: - Autolock(cmutex &m, const char* expr, const char *module, const char *func, int line); - Autolock(cmutex &m); - ~Autolock(); -}; - -} -#endif /* _CMUTEX_H_ */ diff --git a/src/shared/event_loop.h b/src/shared/event_loop.h index aeae959..6e560ff 100644 --- a/src/shared/event_loop.h +++ b/src/shared/event_loop.h @@ -26,7 +26,7 @@ #include #include "event_handler.h" -#include "cmutex.h" +#include "lock.h" namespace ipc { diff --git a/src/shared/lock.cpp b/src/shared/lock.cpp new file mode 100644 index 0000000..650e5c7 --- /dev/null +++ b/src/shared/lock.cpp @@ -0,0 +1,133 @@ +/* + * sensord + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include + +using namespace sensor; + +cmutex::cmutex() +{ + pthread_mutexattr_t mutex_attr; + pthread_mutexattr_init(&mutex_attr); + pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&m_mutex, &mutex_attr); + pthread_mutexattr_destroy(&mutex_attr); +} + +cmutex::~cmutex() +{ + pthread_mutex_destroy(&m_mutex); +} + +void cmutex::lock(void) +{ +#ifdef _LOCK_DEBUG + lock("mutex", __MODULE__, __func__, __LINE__); +#else + lock_impl(); +#endif +} + +void cmutex::lock(const char* expr, const char *module, const char *func, int line) +{ + int ret = 0; + char m_curent_info[OWNER_INFO_LEN]; + struct timeval sv; + unsigned long long lock_waiting_start_time = 0; + unsigned long long lock_acquired_time = 0; + unsigned long long waiting_time = 0; + + snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line); + + ret = try_lock_impl(); + + if (ret == 0) { + pthread_mutex_lock(&m_mutex); + snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); + pthread_mutex_unlock(&m_mutex); + return; + } + + gettimeofday(&sv, NULL); + lock_waiting_start_time = MICROSECONDS(sv); + + pthread_mutex_lock(&m_mutex); + _I("%s is waiting for getting %s(%p) owned in %s", + m_curent_info, expr, this, m_owner_info); + pthread_mutex_unlock(&m_mutex); + + lock_impl(); + + gettimeofday(&sv, NULL); + lock_acquired_time = MICROSECONDS(sv); + + waiting_time = lock_acquired_time - lock_waiting_start_time; + + pthread_mutex_lock(&m_mutex); + _I("%s acquires lock after waiting %lluus, %s(%p) was previously owned in %s", + m_curent_info, waiting_time, expr, this, m_owner_info); + snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); + pthread_mutex_unlock(&m_mutex); +} + +void cmutex::unlock(void) +{ + unlock_impl(); +} + +int cmutex::try_lock(void) +{ + return try_lock_impl(); +} + +int cmutex::lock_impl(void) +{ + return pthread_mutex_lock(&m_mutex); +} + +int cmutex::try_lock_impl(void) +{ + return pthread_mutex_trylock(&m_mutex); +} + +int cmutex::unlock_impl(void) +{ + return pthread_mutex_unlock(&m_mutex); +} + +Autolock::Autolock(cmutex &m, const char* expr, const char *module, const char *func, int line) +: m_lock(m) +{ + m_lock.lock(expr, module, func, line); +} + +Autolock::Autolock(cmutex &m) +: m_lock(m) +{ + m_lock.lock(); +} + +Autolock::~Autolock() +{ + m_lock.unlock(); +} diff --git a/src/shared/lock.h b/src/shared/lock.h new file mode 100644 index 0000000..3961779 --- /dev/null +++ b/src/shared/lock.h @@ -0,0 +1,72 @@ +/* + * sensord + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef _LOCK_H_ +#define _LOCK_H_ + +#include + +namespace sensor { + +#ifndef MICROSECONDS +#define MICROSECONDS(tv) ((tv.tv_sec * 1000000ll) + tv.tv_usec) +#endif + +#ifdef _LOCK_DEBUG +#define AUTOLOCK(x) Autolock x##_autolock((x), #x, __MODULE__, __func__, __LINE__) +#define LOCK(x) (x).lock(#x, __MODULE__, __func__, __LINE__) +#define UNLOCK(x) (x).unlock() +#else +#define AUTOLOCK(x) Autolock x##_autolock((x)) +#define LOCK(x) (x).lock() +#define UNLOCK(x) (x).unlock() +#endif + +class cmutex { +public: + cmutex(); + virtual ~cmutex(); + + void lock(void); + void lock(const char* expr, const char *module, const char *func, int line); + void unlock(void); + int try_lock(void); + +protected: + int lock_impl(void); + int try_lock_impl(void); + int unlock_impl(void); + +private: + pthread_mutex_t m_mutex; + static const int OWNER_INFO_LEN = 256; + char m_owner_info[OWNER_INFO_LEN]; +}; + +class Autolock { +private: + cmutex& m_lock; +public: + Autolock(cmutex &m, const char* expr, const char *module, const char *func, int line); + Autolock(cmutex &m); + ~Autolock(); +}; + +} +#endif /* _LOCK_H_ */