hmbdc
simplify-high-performance-messaging-programming
ContextDetail.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 
5 #include "hmbdc/app/Client.hpp"
6 #include "hmbdc/app/Message.hpp"
7 #include "hmbdc/comm/Topic.hpp"
8 
9 #include "hmbdc/pattern/PoolT.hpp"
10 #include "hmbdc/pattern/LockFreeBufferT.hpp"
11 #include "hmbdc/pattern/MonoLockFreeBuffer.hpp"
12 
13 #include "hmbdc/os/Thread.hpp"
14 #include "hmbdc/time/Timers.hpp"
15 #include "hmbdc/numeric/BitMath.hpp"
16 
17 
18 #include <vector>
19 #include <type_traits>
20 #include <thread>
21 #include <utility>
22 #include <iostream>
23 
24 namespace hmbdc { namespace app { namespace context_detail {
25 using namespace std;
26 using namespace hmbdc::pattern;
27 
28 template <typename... ContextProperties>
32  enum {
33  has_pool = 1,
34  can_start_anytime = 0,
35  create_ipc = 0,
36  attach_ipc = 0,
37  };
38 };
39 
40 template <uint16_t c, typename... ContextProperties>
41 struct context_property_aggregator<context_property::broadcast<c>
42  , ContextProperties...>
43 : context_property_aggregator<ContextProperties...> {
45  enum {
46  has_pool = 1,
47  can_start_anytime = 0
48  };
49 };
50 
51 template <typename... ContextProperties>
52 struct context_property_aggregator<context_property::partition
53  , ContextProperties...>
54 : context_property_aggregator<ContextProperties...> {
56  enum {
57  has_pool = 0,
58  can_start_anytime = 1
59  };
60 };
61 
62 
63 template <typename... ContextProperties>
64 struct context_property_aggregator<context_property::ipc_creator
65  , ContextProperties...>
66 : context_property_aggregator<ContextProperties...> {
68  enum {
69  attach_ipc = 0,
70  create_ipc = 1,
71  };
72 };
73 
74 template <typename... ContextProperties>
75 struct context_property_aggregator<context_property::ipc_attacher
76  , ContextProperties...>
77 : context_property_aggregator<ContextProperties...> {
79  enum {
80  create_ipc = 0,
81  attach_ipc = 1,
82  };
83 };
84 
85 using namespace std;
86 using namespace hmbdc::time;
87 
88 template <bool is_timer_manager>
89 struct tm_runner {
90  template<typename C>
91  void operator()(C&) {}
92 };
93 
94 template <>
95 struct tm_runner<true> {
96  void operator()(TimerManager& tm) {
97  tm.checkTimers(SysTime::now());
98  }
99 };
100 
101 template <typename LFB, typename CcClient>
102 bool runOnceImpl(uint16_t threadSerialNumber, bool& HMBDC_RESTRICT stopped, LFB& HMBDC_RESTRICT lfb, CcClient& HMBDC_RESTRICT c) {
103  typename LFB::iterator begin, end;
104  try {
106  tr(c);
107 
108  const bool clientParticipateInMessaging =
109  std::remove_reference<CcClient>::type::REGISTERED_MESSAGE_SIZE;
110  if (clientParticipateInMessaging) {
111  uint64_t count = lfb.peek(threadSerialNumber, begin, end, c.maxBatchMessageCount());
112  c.CcClient::handleRangeImpl(begin, end, threadSerialNumber);
113  c.CcClient::invokedCb(threadSerialNumber);
114  lfb.wasteAfterPeek(threadSerialNumber, count);
115  } else {
116  c.CcClient::invokedCb(0xffffu - threadSerialNumber);
117  }
118  } catch (std::exception const& e) {
119  if (!stopped) {
120  c.stopped(e);
121  return !c.dropped();
122  }
123  } catch (int code) {
124  if (!stopped) {
125  c.stopped(hmbdc::ExitCode(code));
126  return !c.dropped();
127  }
128  } catch (...) {
129  if (!stopped) {
130  c.stopped(hmbdc::UnknownException());
131  return !c.dropped();
132  }
133  }
134  return true;
135 }
136 
137 template <typename CcClient>
138 bool runOnceImpl(uint16_t threadSerialNumber, bool& HMBDC_RESTRICT stopped, hmbdc::pattern::MonoLockFreeBuffer& HMBDC_RESTRICT lfb, CcClient& HMBDC_RESTRICT c) {
140  try {
142  tr(c);
143 
144  const bool clientParticipateInMessaging =
145  std::remove_reference<CcClient>::type::REGISTERED_MESSAGE_SIZE;
146  if (clientParticipateInMessaging) {
147  uint64_t count = lfb.peek(begin, end, c.maxBatchMessageCount());
148  auto b = begin;
149  c.CcClient::handleRangeImpl(b, end, threadSerialNumber);
150  c.CcClient::invokedCb(threadSerialNumber);
151  lfb.wasteAfterPeek(begin, count);
152  } else {
153  c.CcClient::invokedCb(0xffffu - threadSerialNumber);
154  }
155  } catch (std::exception const& e) {
156  if (!stopped) {
157  c.stopped(e);
158  return !c.dropped();
159  }
160  } catch (int code) {
161  if (!stopped) {
162  c.stopped(hmbdc::ExitCode(code));
163  return !c.dropped();
164  }
165  } catch (...) {
166  if (!stopped) {
167  c.stopped(hmbdc::UnknownException());
168  return !c.dropped();
169  }
170  }
171  return true;
172 }
173 
174 void unblock(MonoLockFreeBuffer& buffer, uint16_t){
175  buffer.reset();
176 }
177 
178 template <typename Buffer>
179 void unblock(Buffer& lfb, uint16_t threadSerialNumber) {
180  lfb.markDead(threadSerialNumber);
181 }
182 
183 }}}
Definition: MonoLockFreeBuffer.hpp:14
Definition: ContextDetail.hpp:89
Definition: TypedString.hpp:74
Definition: Timers.hpp:65
the default vanilla allocate
Definition: Allocators.hpp:116
Unknown excpetion.
Definition: Exception.hpp:17
Definition: GuardedSingleton.hpp:9
Definition: LockFreeBufferT.hpp:18
Definition: Rater.hpp:10
Exception that just has an exit code.
Definition: Exception.hpp:28
helping allocating object and its aggregated objects in a continouse shared memory ...
Definition: Allocators.hpp:88
Definition: Base.hpp:12
Definition: LockFreeBufferMisc.hpp:73