remove unnecessary dependancy to reduce memory use
[platform/core/messaging/msg-service.git] / include / utils / MsgMutex.h
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #ifndef __MSG_MUTEX_H__
18 #define __MSG_MUTEX_H__
19
20 /*==================================================================================================
21                                          INCLUDE FILES
22 ==================================================================================================*/
23 #include <iostream>
24 #include <pthread.h>
25 #include <sys/time.h>
26 #include <time.h>
27
28 using namespace std;
29
30 int GetMsgReady();
31 void WaitMsgReady(int sec);
32
33 class MsgMutex
34 {
35 public:
36         MsgMutex() {
37                 pthread_mutexattr_t mattr;
38                 pthread_mutexattr_init(&mattr);
39                 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
40                 pthread_mutex_init(&m, &mattr);
41                 pthread_mutexattr_destroy(&mattr);
42         }
43         ~MsgMutex() { pthread_mutex_destroy(&m); }
44         void lock() { pthread_mutex_lock(&m); }
45
46         int timedlock() {
47                 struct timespec abs_time;
48                 clock_gettime(CLOCK_REALTIME, &abs_time);
49                 abs_time.tv_sec += 1;
50                 return pthread_mutex_timedlock(&m, &abs_time);
51         }
52
53         void unlock() { pthread_mutex_unlock(&m); }
54         pthread_mutex_t* pMsgMutex() { return &m; }
55
56 private:
57         pthread_mutex_t m;
58 };
59
60 class MsgMutexLocker
61 {
62 public:
63         MsgMutexLocker(MsgMutex& mx) {
64                 pm = &mx;
65                 pm->lock();
66         }
67
68         ~MsgMutexLocker() {
69                 pm->unlock();
70         }
71
72 private:
73         MsgMutex *pm;
74 };
75
76 class MsgCndVar
77 {
78 public:
79         MsgCndVar() { pthread_cond_init(&c, 0); }
80         ~MsgCndVar() { pthread_cond_destroy(&c); }
81         void wait(pthread_mutex_t* m) { pthread_cond_wait(&c, m); }
82         int timedwait(pthread_mutex_t* m, int sec) {
83                 struct timeval now = {0};
84                 struct timespec timeout = {0};
85                 gettimeofday(&now, NULL);
86                 timeout.tv_sec = now.tv_sec+sec;
87                 timeout.tv_nsec = now.tv_usec;
88                 int retcode = pthread_cond_timedwait(&c, m, &timeout);
89                 return retcode;
90         }
91         void signal() { pthread_cond_signal(&c); }
92         void broadcast() { pthread_cond_broadcast(&c); }
93
94 private:
95         pthread_cond_t c;
96 };
97
98 #endif /* __MUTEX_H__ */
99