Unify socket class
[platform/core/api/sensor.git] / src / shared / cbase_lock.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 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-log-private.h>
26
27 using namespace sensor;
28
29 cbase_lock::cbase_lock()
30 {
31         m_history_mutex = PTHREAD_MUTEX_INITIALIZER;
32 }
33
34 cbase_lock::~cbase_lock()
35 {
36         pthread_mutex_destroy(&m_history_mutex);
37 }
38
39 void cbase_lock::lock(lock_type type, const char* expr, const char *module, const char *func, int line)
40 {
41         int ret = 0;
42         char m_curent_info[OWNER_INFO_LEN];
43         struct timeval sv;
44         unsigned long long lock_waiting_start_time = 0;
45         unsigned long long lock_acquired_time = 0;
46         unsigned long long waiting_time = 0;
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         _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);
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(%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);
89 }
90
91 void cbase_lock::lock(lock_type type)
92 {
93         if (type == LOCK_TYPE_MUTEX)
94                 lock_impl();
95         else if (type == LOCK_TYPE_READ)
96                 read_lock_impl();
97         else if (type == LOCK_TYPE_WRITE)
98                 write_lock_impl();
99 }
100
101 void cbase_lock::unlock(void)
102 {
103         unlock_impl();
104 }
105
106 int cbase_lock::lock_impl(void)
107 {
108         return 0;
109 }
110
111 int cbase_lock::read_lock_impl(void)
112 {
113         return 0;
114 }
115
116 int cbase_lock::write_lock_impl(void)
117 {
118         return 0;
119 }
120
121 int cbase_lock::try_lock_impl(void)
122 {
123         return 0;
124 }
125
126 int cbase_lock::try_read_lock_impl(void)
127 {
128         return 0;
129 }
130
131 int cbase_lock::try_write_lock_impl(void)
132 {
133         return 0;
134 }
135
136 int cbase_lock::unlock_impl(void)
137 {
138         return 0;
139 }
140
141 Autolock::Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line)
142 : m_lock(m)
143 {
144         m_lock.lock(type, expr, module, func, line);
145 }
146
147 Autolock::Autolock(cbase_lock &m, lock_type type)
148 : m_lock(m)
149 {
150         m_lock.lock(type);
151 }
152
153 Autolock::~Autolock()
154 {
155         m_lock.unlock();
156 }