UniSet  2.6.0
MQMutex.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
17 #ifndef MQMutex_H_
18 #define MQMutex_H_
19 //--------------------------------------------------------------------------
20 #include <deque>
21 #include <list>
22 #include <memory>
23 #include "Mutex.h"
24 #include "MessageType.h"
25 //--------------------------------------------------------------------------
26 namespace uniset
27 {
28  //--------------------------------------------------------------------------
29  typedef std::shared_ptr<uniset::VoidMessage> VoidMessagePtr;
30  //--------------------------------------------------------------------------
31 
43  class MQMutex
44  {
45  public:
46  MQMutex( size_t qsize = 2000 );
47 
49  void push( const VoidMessagePtr& msg );
50 
54  VoidMessagePtr top() noexcept;
55 
56  size_t size();
57  bool empty();
58 
59  // ----- Настройки -----
60  // неявно подразумевается, что всё настраивается до первого использования
61  // ----------------------
62  void setMaxSizeOfMessageQueue( size_t s ) noexcept;
63  size_t getMaxSizeOfMessageQueue() const noexcept;
64 
67  {
68  lostOldData, // default
69  lostNewData
70  };
71 
72  void setLostStrategy( LostStrategy s ) noexcept;
73 
74  // ---- Статистика ----
76  inline size_t getMaxQueueMessages() const noexcept
77  {
78  return stMaxQueueMessages;
79  }
80 
82  inline size_t getCountOfLostMessages() const noexcept
83  {
84  return stCountOfLostMessages;
85  }
86 
87  protected:
88 
89  private:
90 
91  //typedef std::queue<VoidMessagePtr> MQueue;
92  typedef std::deque<VoidMessagePtr> MQueue;
93 
94  MQueue mqueue;
95  std::mutex qmutex;
96 
97  LostStrategy lostStrategy = { lostOldData };
98 
100  size_t SizeOfMessageQueue = { 2000 };
101 
102  // статистическая информация
103  size_t stMaxQueueMessages = { 0 };
104  size_t stCountOfLostMessages = { 0 };
105  };
106  // -------------------------------------------------------------------------
107 } // end of uniset namespace
108 //---------------------------------------------------------------------------
109 #endif
110 //---------------------------------------------------------------------------
Definition: MQMutex.h:43
Definition: CallbackTimer.h:29
LostStrategy
Definition: MQMutex.h:66
void push(const VoidMessagePtr &msg)
Definition: MQMutex.cc:30
VoidMessagePtr top() noexcept
Definition: MQMutex.cc:58
size_t getMaxQueueMessages() const noexcept
Definition: MQMutex.h:76
size_t getCountOfLostMessages() const noexcept
Definition: MQMutex.h:82