update tizen source
[framework/messaging/msg-service.git] / include / common / MsgQueue.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 __MsgThdSafeQ_H__
32 #define __MsgThdSafeQ_H__
33
34 #include <queue>
35 #include "MsgMutex.h"
36 #include <list>
37
38 template <typename T> class MsgThdSafeQ
39 {
40 public:
41         MsgThdSafeQ(){};
42         void pop_front ();
43         bool front(T* qItem);
44         void push_front(T const & input);
45         void push_back(T const & input);
46         int size();
47         bool empty();
48         void clear();
49 private:
50         Mutex mx;
51         std::list <T> q;
52 };
53
54 /*
55         mx variable guarantees atomic operation in multi-threaded environment.
56         For example, when a thread is executing Pop(), other threads
57         trying to execute one of Pop, Push, Size, Empty are locked.
58 */
59
60 template <typename T> void MsgThdSafeQ<T>::pop_front()
61 {
62         MutexLocker lock(mx);
63         if( q.empty() ) return;
64
65         q.pop_front();
66 }
67
68 template <typename T> bool MsgThdSafeQ<T>::front(T* qItem)
69 {
70         MutexLocker lock(mx);
71         if( qItem == NULL || q.empty() )
72                 return false; // Fail
73
74         *qItem = q.front(); // copy
75
76         return true; // Success
77 }
78
79
80 template <typename T> void MsgThdSafeQ<T>::push_back(T const & qItem)
81 {
82         MutexLocker lock(mx);
83         q.push_back(qItem);
84 }
85
86 template <typename T> void MsgThdSafeQ<T>::push_front(T const & qItem)
87 {
88         MutexLocker lock(mx);
89         q.push_front(qItem);
90 }
91
92
93 template <typename T> int MsgThdSafeQ<T>::size()
94 {
95         MutexLocker lock(mx);
96         return q.size();
97 }
98
99 template <typename T> bool MsgThdSafeQ<T>::empty()
100 {
101         MutexLocker lock(mx);
102         return q.empty();
103 }
104
105 template <typename T> void MsgThdSafeQ<T>::clear()
106 {
107         MutexLocker lock(mx);
108         q.clear();
109 }
110
111 #endif // __MsgThdSafeQ_H__
112