helics  3.5.2
IpcBlockingPriorityQueueImpl.hpp
1 /*
2 Copyright (c) 2017-2018,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC All rights reserved. See LICENSE file and DISCLAIMER for more details.
5 */
6 #pragma once
7 
8 #include "helics/external/optional.hpp"
9 
10 #include <boost/interprocess/mapped_region.hpp>
11 #include <boost/interprocess/shared_memory_object.hpp>
12 #include <boost/interprocess/sync/interprocess_condition.hpp>
13 #include <boost/interprocess/sync/interprocess_mutex.hpp>
14 #include <chrono>
15 #include <utility>
16 
17 namespace helics {
18 namespace ipc {
19  namespace detail {
20  struct dataIndex {
21  int32_t offset;
22  int32_t dataSize;
23  };
25  class dataBlock {
26  private:
27  unsigned char* origin = nullptr;
28  unsigned char* next = nullptr;
29  size_t totalSize = 0;
30  dataIndex* next = nullptr;
31  int dataCount = 0;
32 
33  public:
34  dataBlock(unsigned char* newBlock, size_t blockSize);
35 
36  void swap(dataBlock& other) noexcept;
37 
38  int getCurrentCount() const { return dataCount; }
39  bool isSpaceAvaialble(int sz) const;
40  bool empty() const { return (dataCount == 0); }
41 
42  bool push(const unsigned char* block, int blockSize);
43 
44  int next_data_size() const;
45 
46  int pop(unsigned char* block);
47 
49  void reverse();
51  void clear();
52  };
59  private:
60  mutable boost::interprocess::interprocess_mutex
61  m_pushLock;
62  dataBlock pushData;
63  mutable boost::interprocess::interprocess_mutex
64  m_pullLock;
65  dataBlock pullData;
66  mutable boost::interprocess::interprocess_mutex
67  m_conditionLock;
68  bool queueEmptyFlag{true};
69  bool queueFullFlag{false};
70  // the condition variable should be keyed of the conditionLock
71  boost::interprocess::interprocess_condition
72  condition_empty;
73  boost::interprocess::interprocess_condition
74  condition_full;
75  unsigned char* data1 = nullptr;
76  unsigned char* data2 = nullptr;
77  size_t data1Size = 0;
78  size_t data2Size = 0;
79  unsigned char* dataPriority = nullptr;
80  size_t dataPrioritySize = 0;
81 
82  public:
84  IpcBlockingPriorityQueueImpl(void* dataBlock, size_t blockSize);
85 
87  void clear();
88 
92 
96  void push(const unsigned char* data, size_t size);
100  void pushPriority(const unsigned char* data, size_t size);
104  bool try_push(const unsigned char* data, size_t size);
105 
109  bool try_pushPriority(const unsigned char* data, size_t size);
110 
115  stx::optional<std::pair<unsigned char*, int>> try_peek() const;
116 
121  bool try_pop(unsigned char* data, int maxSize);
122 
123  int pop(unsigned char* data, int maxSize);
124 
126  void pop(std::chrono::milliseconds timeout, unsigned char* data, int maxSize);
127 
132  bool empty() const;
133  };
134 
135  } // namespace detail
136 } // namespace ipc
137 } // namespace helics
Definition: IpcBlockingPriorityQueueImpl.hpp:58
IpcBlockingPriorityQueueImpl(void *dataBlock, size_t blockSize)
Definition: IpcBlockingPriorityQueueImpl.cpp:35
IpcBlockingPriorityQueueImpl(const IpcBlockingPriorityQueueImpl &)=delete
stx::optional< std::pair< unsigned char *, int > > try_peek() const
void clear()
Definition: IpcBlockingPriorityQueueImpl.cpp:41
void pushPriority(const unsigned char *data, size_t size)
bool try_push(const unsigned char *data, size_t size)
void push(const unsigned char *data, size_t size)
Definition: IpcBlockingPriorityQueueImpl.cpp:55
void pop(std::chrono::milliseconds timeout, unsigned char *data, int maxSize)
bool try_pop(unsigned char *data, int maxSize)
bool try_pushPriority(const unsigned char *data, size_t size)
Definition: IpcBlockingPriorityQueueImpl.hpp:25
void reverse()
Definition: IpcBlockingPriorityQueueImpl.cpp:30
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
Definition: IpcBlockingPriorityQueueImpl.hpp:20