From: TaeminYeom Date: Wed, 13 Jul 2022 08:39:55 +0000 (+0900) Subject: Unify cmutex class X-Git-Tag: submit/tizen/20220715.042407^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F83%2F277783%2F1;p=platform%2Fcore%2Fapi%2Fsensor.git Unify cmutex class Remove cbase_lock class Change-Id: I74356a2cded045a25e16919c7059547530f5f1ca Signed-off-by: TaeminYeom --- diff --git a/src/shared/cbase_lock.cpp b/src/shared/cbase_lock.cpp deleted file mode 100644 index 9cfac7f..0000000 --- a/src/shared/cbase_lock.cpp +++ /dev/null @@ -1,156 +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 -#include - -using namespace sensor; - -cbase_lock::cbase_lock() -{ - m_history_mutex = PTHREAD_MUTEX_INITIALIZER; -} - -cbase_lock::~cbase_lock() -{ - pthread_mutex_destroy(&m_history_mutex); -} - -void cbase_lock::lock(lock_type type, 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); - - if (type == LOCK_TYPE_MUTEX) - ret = try_lock_impl(); - else if (type == LOCK_TYPE_READ) - ret = try_read_lock_impl(); - else if (type == LOCK_TYPE_WRITE) - ret = try_write_lock_impl(); - - if (ret == 0) { - pthread_mutex_lock(&m_history_mutex); - snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info); - pthread_mutex_unlock(&m_history_mutex); - return; - } - - gettimeofday(&sv, NULL); - lock_waiting_start_time = MICROSECONDS(sv); - - pthread_mutex_lock(&m_history_mutex); - _I("%s is waiting for getting %s(%p) owned in %s", - m_curent_info, expr, this, m_owner_info); - pthread_mutex_unlock(&m_history_mutex); - - if (type == LOCK_TYPE_MUTEX) - lock_impl(); - else if (type == LOCK_TYPE_READ) - read_lock_impl(); - else if (type == LOCK_TYPE_WRITE) - write_lock_impl(); - - gettimeofday(&sv, NULL); - lock_acquired_time = MICROSECONDS(sv); - - waiting_time = lock_acquired_time - lock_waiting_start_time; - - pthread_mutex_lock(&m_history_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_history_mutex); -} - -void cbase_lock::lock(lock_type type) -{ - if (type == LOCK_TYPE_MUTEX) - lock_impl(); - else if (type == LOCK_TYPE_READ) - read_lock_impl(); - else if (type == LOCK_TYPE_WRITE) - write_lock_impl(); -} - -void cbase_lock::unlock(void) -{ - unlock_impl(); -} - -int cbase_lock::lock_impl(void) -{ - return 0; -} - -int cbase_lock::read_lock_impl(void) -{ - return 0; -} - -int cbase_lock::write_lock_impl(void) -{ - return 0; -} - -int cbase_lock::try_lock_impl(void) -{ - return 0; -} - -int cbase_lock::try_read_lock_impl(void) -{ - return 0; -} - -int cbase_lock::try_write_lock_impl(void) -{ - return 0; -} - -int cbase_lock::unlock_impl(void) -{ - return 0; -} - -Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line) -: m_lock(m) -{ - m_lock.lock(type, expr, module, func, line); -} - -Autolock::Autolock(cbase_lock &m, lock_type type) -: m_lock(m) -{ - m_lock.lock(type); -} - -Autolock::~Autolock() -{ - m_lock.unlock(); -} diff --git a/src/shared/cbase_lock.h b/src/shared/cbase_lock.h deleted file mode 100644 index 5c76040..0000000 --- a/src/shared/cbase_lock.h +++ /dev/null @@ -1,90 +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 _CBASE_LOCK_H_ -#define _CBASE_LOCK_H_ - -#include - -namespace sensor { - -enum lock_type { - LOCK_TYPE_MUTEX, - LOCK_TYPE_READ, - LOCK_TYPE_WRITE, -}; - -#ifndef MICROSECONDS -#define MICROSECONDS(tv) ((tv.tv_sec * 1000000ll) + tv.tv_usec) -#endif - -#ifdef _LOCK_DEBUG -#define AUTOLOCK(x) Autolock x##_autolock((x), LOCK_TYPE_MUTEX, #x, __MODULE__, __func__, __LINE__) -#define AUTOLOCK_R(x) Autolock x##_autolock_r((x), LOCK_TYPE_READ, #x, __MODULE__, __func__, __LINE__) -#define AUTOLOCK_W(x) Autolock x##_autolock_w((x), LOCK_TYPE_WRITE, #x, __MODULE__, __func__, __LINE__) -#define LOCK(x) (x).lock(#x, __MODULE__, __func__, __LINE__) -#define LOCK_R(x) (x).lock(LOCK_TYPE_READ, #x, __MODULE__, __func__, __LINE__) -#define LOCK_W(x) (x).lock(LOCK_TYPE_WRITE, #x, __MODULE__, __func__, __LINE__) -#define UNLOCK(x) (x).unlock() -#else -#define AUTOLOCK(x) Autolock x##_autolock((x), LOCK_TYPE_MUTEX) -#define AUTOLOCK_R(x) Autolock x##_autolock_r((x), LOCK_TYPE_READ) -#define AUTOLOCK_W(x) Autolock x##_autolock_w((x), LOCK_TYPE_WRITE) -#define LOCK(x) (x).lock() -#define LOCK_R(x) (x).lock(LOCK_TYPE_READ) -#define LOCK_W(x) (x).lock(LOCK_TYPE_WRITE) -#define UNLOCK(x) (x).unlock() -#endif - -class cbase_lock { -public: - cbase_lock(); - virtual ~cbase_lock(); - - void lock(lock_type type, const char* expr, const char *module, const char *func, int line); - void lock(lock_type type); - void unlock(void); - -protected: - virtual int lock_impl(void); - virtual int read_lock_impl(void); - virtual int write_lock_impl(void); - - virtual int try_lock_impl(void); - virtual int try_read_lock_impl(void); - virtual int try_write_lock_impl(void); - - virtual int unlock_impl(void); -private: - pthread_mutex_t m_history_mutex; - static const int OWNER_INFO_LEN = 256; - char m_owner_info[OWNER_INFO_LEN]; -}; - -class Autolock { -private: - cbase_lock& m_lock; -public: - Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line); - Autolock(cbase_lock &m, lock_type type); - ~Autolock(); -}; -} - -#endif /* _CBASE_LOCK_H_ */ diff --git a/src/shared/cmutex.cpp b/src/shared/cmutex.cpp index df28253..c29aa72 100644 --- a/src/shared/cmutex.cpp +++ b/src/shared/cmutex.cpp @@ -17,6 +17,9 @@ * */ +#include +#include +#include #include #include @@ -39,15 +42,57 @@ cmutex::~cmutex() void cmutex::lock(void) { #ifdef _LOCK_DEBUG - cbase_lock::lock(LOCK_TYPE_MUTEX, "mutex", __MODULE__, __func__, __LINE__); + lock("mutex", __MODULE__, __func__, __LINE__); #else - cbase_lock::lock(LOCK_TYPE_MUTEX); + lock_impl(); #endif } void cmutex::lock(const char* expr, const char *module, const char *func, int line) { - cbase_lock::lock(LOCK_TYPE_MUTEX, expr, module, func, 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) @@ -69,3 +114,20 @@ 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 index b6c3d4d..91219a0 100644 --- a/src/shared/cmutex.h +++ b/src/shared/cmutex.h @@ -20,16 +20,32 @@ #ifndef _CMUTEX_H_ #define _CMUTEX_H_ -#include "cbase_lock.h" +#include + namespace sensor { -class cmutex : public cbase_lock { +#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: @@ -39,6 +55,17 @@ protected: 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(); }; }