Debian Codegraph @

src/ApvlvQueue.h

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE ApvlvCmds.h
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #ifndef _APVLV_QUEUE_H_
29 #define _APVLV_QUEUE_H_
30
31 #include <condition_variable>
32 #include <mutex>
33 #include <queue>
34
35 namespace apvlv
36 {
37
38 template <class T> class LockQueue
39 {
40 public:
41 LockQueue () = default;
42 LockQueue (const LockQueue &) = delete;
43 LockQueue &operator= (const LockQueue &) = delete;
44 ~LockQueue () = default;
45
46 void
47 push (const T &node)
48 {
49 std::lock_guard<std::mutex> lock (mMutex);
50 mQueueInternal.push (node);
51 }
52
53 void
54 push (T &&node)
55 {
56 std::lock_guard<std::mutex> lock (mMutex);
57 mQueueInternal.push (std::move (node));
58 }
59
60 bool
61 pop (T &node)
62 {
63 std::lock_guard<std::mutex> lock (mMutex);
64 if (mQueueInternal.empty ())
65 return false;
66
67 node = std::move (mQueueInternal.front ());
68 mQueueInternal.pop ();
69 return true;
70 }
71
72 void
73 empty ()
74 {
75 std::lock_guard<std::mutex> lock (mMutex);
76 return mQueueInternal.empty ();
77 }
78
79 void
80 clear ()
81 {
82 std::lock_guard<std::mutex> lock (mMutex);
83 mQueueInternal = {};
84 }
85
86 private:
87 std::queue<T> mQueueInternal;
88 std::mutex mMutex;
89 };
90
91 class Token;
92 class TokenDispatcher final
93 {
94 public:
95 TokenDispatcher (int count, bool enable)
96 : mCount (count), mEnableSpecial (enable)
97 {
98 mDispatchedCount = 0;
99 }
100 ~TokenDispatcher () = default;
101
102 std::unique_ptr<Token> getToken (bool isSpecial);
103
104 void returnToken (Token *token);
105
106 TokenDispatcher (const TokenDispatcher &) = delete;
107 TokenDispatcher &operator= (const TokenDispatcher &) = delete;
108 TokenDispatcher (TokenDispatcher &&) = delete;
109 TokenDispatcher &operator= (TokenDispatcher &&) = delete;
110
111 private:
112 int mCount;
113 bool mEnableSpecial;
114 int mDispatchedCount;
115 std::mutex mMutex;
116 std::condition_variable mCondition;
117 };
118
119 class Token final
120 {
121 public:
122 explicit Token (TokenDispatcher *parent)
123 : mParent (parent), mIsReturned (false)
124 {
125 }
126 ~Token ()
127 {
128 if (!mIsReturned)
129 mParent->returnToken (this);
130 }
131
132 private:
133 TokenDispatcher *mParent;
134 bool mIsReturned;
135 };
136
137 }
138
139 #endif