UniSet  2.24.2
MQAtomic.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 MQAtomic_H_
18 #define MQAtomic_H_
19 //--------------------------------------------------------------------------
20 #include <atomic>
21 #include <vector>
22 #include <memory>
23 #include <mutex>
24 #include "MessageType.h"
25 //--------------------------------------------------------------------------
26 typedef std::shared_ptr<uniset::VoidMessage> VoidMessagePtr;
27 //--------------------------------------------------------------------------
28 namespace uniset
29 {
62  class MQAtomic
63  {
64  public:
65  MQAtomic( size_t qsize = 2000 );
66 
68  bool push( const VoidMessagePtr& msg ) noexcept;
69 
73  VoidMessagePtr top() noexcept;
74 
75  size_t size() const noexcept;
76  bool empty() const noexcept;
77 
78  // ----- Настройки -----
79  // неявно подразумевается, что всё настраивается до первого использования
80  // ----------------------
81  void setMaxSizeOfMessageQueue( size_t s );
82  size_t getMaxSizeOfMessageQueue() const noexcept;
83 
86  {
87  lostOldData, // default
88  lostNewData
89  };
90 
91  void setLostStrategy( LostStrategy s ) noexcept;
92 
93  // ---- Статистика ----
95  inline size_t getMaxQueueMessages() const noexcept
96  {
97  return stMaxQueueMessages;
98  }
99 
101  inline size_t getCountOfLostMessages() const noexcept
102  {
103  return stCountOfLostMessages;
104  }
105 
106  protected:
107 
108  // заполнить всю очередь указанным сообщением
109  void mqFill( const VoidMessagePtr& v );
110 
111  // для возможности тестирования переполнения
112  // специально делается такая функция
113  void set_wpos( unsigned long pos ) noexcept;
114  void set_rpos( unsigned long pos ) noexcept;
115 
116  private:
117 
118  typedef std::vector<VoidMessagePtr> MQueue;
119 
120  MQueue mqueue;
121  std::atomic_ulong wpos = { 0 }; // позиция на запись
122  std::atomic_ulong rpos = { 0 }; // позиция на чтение
123  std::atomic_ulong qpos = { 0 }; // текущая позиция последнего элемента (max position) (реально добавленного в очередь)
124 
125  LostStrategy lostStrategy = { lostOldData };
126 
128  size_t SizeOfMessageQueue = { 2000 };
129 
130  // статистическая информация
131  size_t stMaxQueueMessages = { 0 };
132  size_t stCountOfLostMessages = { 0 };
133  };
134  // -------------------------------------------------------------------------
135 } // end of uniset namespace
136 //---------------------------------------------------------------------------
137 #endif
138 //---------------------------------------------------------------------------
Definition: MQAtomic.h:63
size_t getMaxQueueMessages() const noexcept
Definition: MQAtomic.h:95
bool push(const VoidMessagePtr &msg) noexcept
Definition: MQAtomic.cc:31
size_t getCountOfLostMessages() const noexcept
Definition: MQAtomic.h:101
VoidMessagePtr top() noexcept
Definition: MQAtomic.cc:75
LostStrategy
Definition: MQAtomic.h:86
Definition: CommonEventLoop.h:15