Exclude from all builds except 32bit arm mobile build
[platform/hal/backend/tm1/sensor-tm1.git] / src / lib / 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 <cbase_lock.h>
22 #include <stdio.h>
23 #include <sensor_logs.h>
24 #include <errno.h>
25 #include <sys/time.h>
26
27
28 cbase_lock::cbase_lock()
29 {
30         m_history_mutex = PTHREAD_MUTEX_INITIALIZER;
31 }
32
33 cbase_lock::~cbase_lock()
34 {
35         pthread_mutex_destroy(&m_history_mutex);
36 }
37
38 void cbase_lock::lock(lock_type type, const char* expr, const char *module, const char *func, int line)
39 {
40         int ret = 0;
41         char m_curent_info[OWNER_INFO_LEN];
42         struct timeval sv;
43         unsigned long long lock_waiting_start_time = 0;
44         unsigned long long lock_acquired_time = 0;
45         unsigned long long waiting_time = 0;
46
47
48         snprintf(m_curent_info, OWNER_INFO_LEN, "%s:%s(%d)", module, func, line);
49
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();
56
57         if (ret == 0) {
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);
61                 return;
62         }
63
64         gettimeofday(&sv, NULL);
65         lock_waiting_start_time = MICROSECONDS(sv);
66
67         pthread_mutex_lock(&m_history_mutex);
68         INFO("%s is waiting for getting %s(0x%x) owned in %s",
69                 m_curent_info, expr, this, m_owner_info);
70         pthread_mutex_unlock(&m_history_mutex);
71
72
73         if (type == LOCK_TYPE_MUTEX)
74                 lock_impl();
75         else if (type == LOCK_TYPE_READ)
76                 read_lock_impl();
77         else if (type == LOCK_TYPE_WRITE)
78                 write_lock_impl();
79
80         gettimeofday(&sv, NULL);
81         lock_acquired_time = MICROSECONDS(sv);
82
83         waiting_time = lock_acquired_time - lock_waiting_start_time;
84
85         pthread_mutex_lock(&m_history_mutex);
86         INFO("%s acquires lock after waiting %lluus, %s(0x%x) was previously owned in %s",
87                 m_curent_info, waiting_time, expr, this, m_owner_info);
88         snprintf(m_owner_info, OWNER_INFO_LEN, "%s", m_curent_info);
89         pthread_mutex_unlock(&m_history_mutex);
90 }
91
92
93 void cbase_lock::lock(lock_type type)
94 {
95         if (type == LOCK_TYPE_MUTEX)
96                 lock_impl();
97         else if (type == LOCK_TYPE_READ)
98                 read_lock_impl();
99         else if (type == LOCK_TYPE_WRITE)
100                 write_lock_impl();
101 }
102
103 void cbase_lock::unlock(void)
104 {
105         unlock_impl();
106 }
107
108
109 int cbase_lock::lock_impl(void)
110 {
111         return 0;
112 }
113
114 int cbase_lock::read_lock_impl(void)
115 {
116         return 0;
117 }
118
119 int cbase_lock::write_lock_impl(void)
120 {
121         return 0;
122 }
123
124 int cbase_lock::try_lock_impl(void)
125 {
126         return 0;
127 }
128
129 int cbase_lock::try_read_lock_impl(void)
130 {
131         return 0;
132 }
133
134 int cbase_lock::try_write_lock_impl(void)
135 {
136         return 0;
137 }
138
139 int cbase_lock::unlock_impl(void)
140 {
141         return 0;
142 }
143
144 Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line)
145 : m_lock(m)
146 {
147         m_lock.lock(type, expr, module, func, line);
148 }
149
150 Autolock::Autolock(cbase_lock &m, lock_type type)
151 : m_lock(m)
152 {
153         m_lock.lock(type);
154 }
155
156 Autolock::~Autolock()
157 {
158         m_lock.unlock();
159 }