43588da171197c391b548aa3ffe5d523e46b6f02
[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 Mutex
34 {
35 public:
36         Mutex() {
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         ~Mutex() { 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* pMutex() { return &m; }
55
56 private:
57         pthread_mutex_t m;
58 };
59
60 class MutexLocker
61 {
62 public:
63         MutexLocker(Mutex& mx) {
64                 pm = &mx;
65                 pm->lock();
66         }
67
68         ~MutexLocker() {
69                 pm->unlock();
70         }
71
72 private:
73         Mutex *pm;
74 };
75
76 class CndVar
77 {
78 public:
79         CndVar() { pthread_cond_init(&c, 0); }
80         void wait(pthread_mutex_t* m) { pthread_cond_wait(&c, m); }
81         int timedwait(pthread_mutex_t* m, int sec) {
82                 struct timeval now = {0};
83                 struct timespec timeout = {0};
84                 gettimeofday(&now, NULL);
85                 timeout.tv_sec = now.tv_sec+sec;
86                 timeout.tv_nsec = now.tv_usec;
87                 int retcode = pthread_cond_timedwait(&c, m, &timeout);
88                 return retcode;
89         }
90         void signal() { pthread_cond_signal(&c); }
91         void broadcast() { pthread_cond_broadcast(&c); }
92
93 private:
94         pthread_cond_t c;
95 };
96
97 #endif /* __MUTEX_H__ */
98