Merge "sensord: add the sensord plugin logic for flexibility" into devel/tizen_3.0
[platform/core/system/sensord.git] / src / shared / cbase_lock.h
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 #ifndef _CBASE_LOCK_H_
21 #define _CBASE_LOCK_H_
22
23 #include <pthread.h>
24
25 enum lock_type {
26         LOCK_TYPE_MUTEX,
27         LOCK_TYPE_READ,
28         LOCK_TYPE_WRITE,
29 };
30
31 #ifdef _LOCK_DEBUG
32 #define AUTOLOCK(x) Autolock x##_autolock((x),LOCK_TYPE_MUTEX, #x, __MODULE__, __func__, __LINE__)
33 #define AUTOLOCK_R(x) Autolock x##_autolock_r((x),LOCK_TYPE_READ, #x,  __MODULE__, __func__, __LINE__)
34 #define AUTOLOCK_W(x) Autolock x##_autolock_w((x),LOCK_TYPE_WRITE, #x, __MODULE__, __func__, __LINE__)
35 #define LOCK(x)         (x).lock(#x, __MODULE__, __func__, __LINE__)
36 #define LOCK_R(x)       (x).lock(LOCK_TYPE_READ, #x, __MODULE__, __func__, __LINE__)
37 #define LOCK_W(x)       (x).lock(LOCK_TYPE_WRITE, #x, __MODULE__, __func__, __LINE__)
38 #define UNLOCK(x)       (x).unlock()
39 #else
40 #define AUTOLOCK(x) Autolock x##_autolock((x),LOCK_TYPE_MUTEX)
41 #define AUTOLOCK_R(x) Autolock x##_autolock_r((x),LOCK_TYPE_READ)
42 #define AUTOLOCK_W(x) Autolock x##_autolock_w((x),LOCK_TYPE_WRITE)
43 #define LOCK(x)         (x).lock()
44 #define LOCK_R(x)       (x).lock(LOCK_TYPE_READ)
45 #define LOCK_W(x)       (x).lock(LOCK_TYPE_WRITE)
46 #define UNLOCK(x)       (x).unlock()
47 #endif
48
49
50 class cbase_lock {
51 public:
52         cbase_lock();
53         virtual ~cbase_lock();
54
55         void lock(lock_type type, const char* expr, const char *module, const char *func, int line);
56         void lock(lock_type type);
57         void unlock(void);
58
59 protected:
60         virtual int lock_impl(void);
61         virtual int read_lock_impl(void);
62         virtual int write_lock_impl(void);
63
64         virtual int try_lock_impl(void);
65         virtual int try_read_lock_impl(void);
66         virtual int try_write_lock_impl(void);
67
68         virtual int unlock_impl(void);
69 private:
70         pthread_mutex_t m_history_mutex;
71         static const int OWNER_INFO_LEN = 256;
72         char m_owner_info[OWNER_INFO_LEN];
73 };
74
75 class Autolock {
76 private:
77         cbase_lock& m_lock;
78 public:
79         Autolock(cbase_lock &m, lock_type type, const char* expr, const char *module, const char *func, int line);
80         Autolock(cbase_lock &m, lock_type type);
81         ~Autolock();
82 };
83
84 #endif /* _CBASE_LOCK_H_ */