update tizen source
[framework/messaging/msg-service.git] / include / utils / MsgMutex.h
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #ifndef __MSG_MUTEX_H__
32 #define __MSG_MUTEX_H__
33
34 /*==================================================================================================
35                                          INCLUDE FILES
36 ==================================================================================================*/
37 #include <iostream>
38 #include <pthread.h>
39 #include <sys/time.h>
40 #include <time.h>
41
42 using namespace std;
43
44 int GetMsgReady();
45 void WaitMsgReady(int sec);
46
47 class Mutex
48 {
49 public:
50         Mutex(){ pthread_mutex_init(&m, 0); }
51         void lock(){ pthread_mutex_lock(&m); }
52         void unlock(){ pthread_mutex_unlock(&m); }
53         pthread_mutex_t* pMutex() { return &m; }
54
55 private:
56         pthread_mutex_t m;
57 };
58
59 class MutexLocker
60 {
61 public:
62         MutexLocker(Mutex& mx)
63         {
64                 pm = &mx;
65                 pm->lock();
66         }
67
68         ~MutexLocker()
69         {
70                 pm->unlock();
71         }
72
73 private:
74         Mutex *pm;
75 };
76
77 class CndVar
78 {
79 public:
80         CndVar(){ pthread_cond_init(&c, 0); }
81         void wait(pthread_mutex_t* m) { pthread_cond_wait(&c, m); }
82         int timedwait(pthread_mutex_t* m, int sec)
83         {
84                 struct timeval now = {0};
85                 struct timespec timeout = {0};
86                 gettimeofday(&now, NULL);
87                 timeout.tv_sec = now.tv_sec+sec;
88                 timeout.tv_nsec = now.tv_usec;
89                 int retcode = pthread_cond_timedwait(&c, m, &timeout);
90                 return retcode;
91         }
92         void signal(){ pthread_cond_signal(&c); }
93         void broadcast(){ pthread_cond_broadcast(&c); }
94
95 private:
96         pthread_cond_t c;
97 };
98
99 #endif //__MUTEX_H__
100