Merge "sensord: add the sensord plugin logic for flexibility" into devel/tizen_3.0
[platform/core/system/sensord.git] / src / shared / cbase_lock.cpp
1 /*
2  * libsensord-share
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <sys/time.h>
24 #include <cbase_lock.h>
25 #include <sensor_logs.h>
26
27 cbase_lock::cbase_lock()
28 {
29         m_history_mutex = PTHREAD_MUTEX_INITIALIZER;
30 }
31
32 cbase_lock::~cbase_lock()
33 {
34         pthread_mutex_destroy(&m_history_mutex);
35 }
36
37 void cbase_lock::lock(lock_type type, const char* expr, const char *module, const char *func, int line)
38 {
39         int ret = 0;
40         char m_curent_info[OWNER_INFO_LEN];
41         struct timeval sv;
42         unsigned long long lock_waiting_start_time = 0;
43         unsigned long long lock_acquired_time = 0;
44         unsigned long long waiting_time = 0;
45
46
47         snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line);
48
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();
55
56         if (ret == 0) {
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);
60                 return;
61         }
62
63         gettimeofday(&sv, NULL);
64         lock_waiting_start_time = MICROSECONDS(sv);
65
66         pthread_mutex_lock(&m_history_mutex);
67         _I("%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);
70
71
72         if (type == LOCK_TYPE_MUTEX)
73                 lock_impl();
74         else if (type == LOCK_TYPE_READ)
75                 read_lock_impl();
76         else if (type == LOCK_TYPE_WRITE)
77                 write_lock_impl();
78
79         gettimeofday(&sv, NULL);
80         lock_acquired_time = MICROSECONDS(sv);
81
82         waiting_time = lock_acquired_time - lock_waiting_start_time;
83
84         pthread_mutex_lock(&m_history_mutex);
85         _I("%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);
89 }
90
91
92 void cbase_lock::lock(lock_type type)
93 {
94         if (type == LOCK_TYPE_MUTEX)
95                 lock_impl();
96         else if (type == LOCK_TYPE_READ)
97                 read_lock_impl();
98         else if (type == LOCK_TYPE_WRITE)
99                 write_lock_impl();
100 }
101
102 void cbase_lock::unlock(void)
103 {
104         unlock_impl();
105 }
106
107
108 int cbase_lock::lock_impl(void)
109 {
110         return 0;
111 }
112
113 int cbase_lock::read_lock_impl(void)
114 {
115         return 0;
116 }
117
118 int cbase_lock::write_lock_impl(void)
119 {
120         return 0;
121 }
122
123 int cbase_lock::try_lock_impl(void)
124 {
125         return 0;
126 }
127
128 int cbase_lock::try_read_lock_impl(void)
129 {
130         return 0;
131 }
132
133 int cbase_lock::try_write_lock_impl(void)
134 {
135         return 0;
136 }
137
138 int cbase_lock::unlock_impl(void)
139 {
140         return 0;
141 }
142
143 Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line)
144 : m_lock(m)
145 {
146         m_lock.lock(type, expr, module, func, line);
147 }
148
149 Autolock::Autolock(cbase_lock &m, lock_type type)
150 : m_lock(m)
151 {
152         m_lock.lock(type);
153 }
154
155 Autolock::~Autolock()
156 {
157         m_lock.unlock();
158 }