4 * Copyright (c) 2013 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-log-private.h>
27 using namespace sensor;
29 cbase_lock::cbase_lock()
31 m_history_mutex = PTHREAD_MUTEX_INITIALIZER;
34 cbase_lock::~cbase_lock()
36 pthread_mutex_destroy(&m_history_mutex);
39 void cbase_lock::lock(lock_type type, const char* expr, const char *module, const char *func, int line)
42 char m_curent_info[OWNER_INFO_LEN];
44 unsigned long long lock_waiting_start_time = 0;
45 unsigned long long lock_acquired_time = 0;
46 unsigned long long waiting_time = 0;
48 snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line);
50 if (type == LOCK_TYPE_MUTEX)
51 ret = try_lock_impl();
52 else if (type == LOCK_TYPE_READ)
53 ret = try_read_lock_impl();
54 else if (type == LOCK_TYPE_WRITE)
55 ret = try_write_lock_impl();
58 pthread_mutex_lock(&m_history_mutex);
59 snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info);
60 pthread_mutex_unlock(&m_history_mutex);
64 gettimeofday(&sv, NULL);
65 lock_waiting_start_time = MICROSECONDS(sv);
67 pthread_mutex_lock(&m_history_mutex);
68 _I("%s is waiting for getting %s(%p) owned in %s",
69 m_curent_info, expr, this, m_owner_info);
70 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 _I("%s acquires lock after waiting %lluus, %s(%p) 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);
91 void cbase_lock::lock(lock_type type)
93 if (type == LOCK_TYPE_MUTEX)
95 else if (type == LOCK_TYPE_READ)
97 else if (type == LOCK_TYPE_WRITE)
101 void cbase_lock::unlock(void)
106 int cbase_lock::lock_impl(void)
111 int cbase_lock::read_lock_impl(void)
116 int cbase_lock::write_lock_impl(void)
121 int cbase_lock::try_lock_impl(void)
126 int cbase_lock::try_read_lock_impl(void)
131 int cbase_lock::try_write_lock_impl(void)
136 int cbase_lock::unlock_impl(void)
141 Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line)
144 m_lock.lock(type, expr, module, func, line);
147 Autolock::Autolock(cbase_lock &m, lock_type type)
153 Autolock::~Autolock()