4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <cbase_lock.h>
25 #include <sensor_logs.h>
27 cbase_lock::cbase_lock()
29 m_history_mutex = PTHREAD_MUTEX_INITIALIZER;
32 cbase_lock::~cbase_lock()
34 pthread_mutex_destroy(&m_history_mutex);
37 void cbase_lock::lock(lock_type type, const char* expr, const char *module, const char *func, int line)
40 char m_curent_info[OWNER_INFO_LEN];
42 unsigned long long lock_waiting_start_time = 0;
43 unsigned long long lock_acquired_time = 0;
44 unsigned long long waiting_time = 0;
47 snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line);
49 if (type == LOCK_TYPE_MUTEX)
50 ret = try_lock_impl();
51 else if (type == LOCK_TYPE_READ)
52 ret = try_read_lock_impl();
53 else if (type == LOCK_TYPE_WRITE)
54 ret = try_write_lock_impl();
57 pthread_mutex_lock(&m_history_mutex);
58 snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info);
59 pthread_mutex_unlock(&m_history_mutex);
63 gettimeofday(&sv, NULL);
64 lock_waiting_start_time = MICROSECONDS(sv);
66 pthread_mutex_lock(&m_history_mutex);
67 INFO("%s is waiting for getting %s(0x%x) owned in %s",
68 m_curent_info, expr, this, m_owner_info);
69 pthread_mutex_unlock(&m_history_mutex);
72 if (type == LOCK_TYPE_MUTEX)
74 else if (type == LOCK_TYPE_READ)
76 else if (type == LOCK_TYPE_WRITE)
79 gettimeofday(&sv, NULL);
80 lock_acquired_time = MICROSECONDS(sv);
82 waiting_time = lock_acquired_time - lock_waiting_start_time;
84 pthread_mutex_lock(&m_history_mutex);
85 INFO("%s acquires lock after waiting %lluus, %s(0x%x) was previously owned in %s",
86 m_curent_info, waiting_time, expr, this, m_owner_info);
87 snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info);
88 pthread_mutex_unlock(&m_history_mutex);
92 void cbase_lock::lock(lock_type type)
94 if (type == LOCK_TYPE_MUTEX)
96 else if (type == LOCK_TYPE_READ)
98 else if (type == LOCK_TYPE_WRITE)
102 void cbase_lock::unlock(void)
108 int cbase_lock::lock_impl(void)
113 int cbase_lock::read_lock_impl(void)
118 int cbase_lock::write_lock_impl(void)
123 int cbase_lock::try_lock_impl(void)
128 int cbase_lock::try_read_lock_impl(void)
133 int cbase_lock::try_write_lock_impl(void)
138 int cbase_lock::unlock_impl(void)
143 Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line)
146 m_lock.lock(type, expr, module, func, line);
149 Autolock::Autolock(cbase_lock &m, lock_type type)
155 Autolock::~Autolock()