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 detail {
25 template <typename... ContextProperties>
29  enum {
30  has_pool = 1,
31  can_start_anytime = 0,
32  create_ipc = 0,
33  attach_ipc = 0,
34  };
35 };
36 
37 template <uint16_t c, typename... ContextProperties>
38 struct context_property_aggregator<context_property::broadcast<c>
39  , ContextProperties...>
40 : context_property_aggregator<ContextProperties...> {
42  enum {
43  has_pool = 1,
44  can_start_anytime = 0
45  };
46 };
47 
48 template <typename... ContextProperties>
49 struct context_property_aggregator<context_property::partition
50  , ContextProperties...>
51 : context_property_aggregator<ContextProperties...> {
53  enum {
54  has_pool = 0,
55  can_start_anytime = 1
56  };
57 };
58 
59 
60 template <typename... ContextProperties>
61 struct context_property_aggregator<context_property::ipc_creator
62  , ContextProperties...>
63 : context_property_aggregator<ContextProperties...> {
65  enum {
66  attach_ipc = 0,
67  create_ipc = 1,
68  };
69 };
70 
71 template <typename... ContextProperties>
72 struct context_property_aggregator<context_property::ipc_attacher
73  , ContextProperties...>
74 : context_property_aggregator<ContextProperties...> {
76  enum {
77  create_ipc = 0,
78  attach_ipc = 1,
79  };
80 };
81 
82 using namespace std;
83 using namespace hmbdc::time;
84 
85 template <bool is_timer_manager>
86 struct tm_runner {
87  template<typename C>
88  void operator()(C&) {}
89 };
90 
91 template <>
92 struct tm_runner<true> {
93  void operator()(TimerManager& tm) {
94  tm.checkTimers(SysTime::now());
95  }
96 };
97 
98 template <typename LFB, typename Client>
99 bool runOnceImpl(uint16_t threadSerialNumber, bool& __restrict__ stopped, LFB& __restrict__ lfb, Client& __restrict__ c) {
100  typename LFB::iterator begin, end;
101  try {
103  tr(c);
104 
105  const bool clientParticipateInMessaging =
106  std::remove_reference<Client>::type::REGISTERED_MESSAGE_SIZE;
107  if (clientParticipateInMessaging) {
108  uint64_t count = lfb.peek(threadSerialNumber, begin, end, c.maxBatchMessageCount());
109  c.Client::handleRangeImpl(begin, end, threadSerialNumber);
110  c.Client::invokedCb(threadSerialNumber);
111  lfb.wasteAfterPeek(threadSerialNumber, count);
112  } else {
113  c.Client::invokedCb(0xffffu - threadSerialNumber);
114  }
115  } catch (std::exception const& e) {
116  if (!stopped) {
117  c.stopped(e);
118  return !c.dropped();
119  }
120  } catch (int code) {
121  if (!stopped) {
122  c.stopped(hmbdc::ExitCode(code));
123  return !c.dropped();
124  }
125  } catch (...) {
126  if (!stopped) {
127  c.stopped(hmbdc::UnknownException());
128  return !c.dropped();
129  }
130  }
131  return true;
132 }
133 
134 template <typename Client>
135 bool runOnceImpl(uint16_t threadSerialNumber, bool& __restrict__ stopped, hmbdc::pattern::MonoLockFreeBuffer& __restrict__ lfb, Client& __restrict__ c) {
137  try {
139  tr(c);
140 
141  const bool clientParticipateInMessaging =
142  std::remove_reference<Client>::type::REGISTERED_MESSAGE_SIZE;
143  if (clientParticipateInMessaging) {
144  uint64_t count = lfb.peek(begin, end, c.maxBatchMessageCount());
145  auto b = begin;
146  c.Client::handleRangeImpl(b, end, threadSerialNumber);
147  c.Client::invokedCb(threadSerialNumber);
148  lfb.wasteAfterPeek(begin, count);
149  } else {
150  c.Client::invokedCb(0xffffu - threadSerialNumber);
151  }
152  } catch (std::exception const& e) {
153  if (!stopped) {
154  c.stopped(e);
155  return !c.dropped();
156  }
157  } catch (int code) {
158  if (!stopped) {
159  c.stopped(hmbdc::ExitCode(code));
160  return !c.dropped();
161  }
162  } catch (...) {
163  if (!stopped) {
164  c.stopped(hmbdc::UnknownException());
165  return !c.dropped();
166  }
167  }
168  return true;
169 }
170 
171 void unblock(MonoLockFreeBuffer& buffer, uint16_t){
172  buffer.reset();
173 }
174 
175 template <typename Buffer>
176 void unblock(Buffer& lfb, uint16_t threadSerialNumber) {
177  lfb.markDead(threadSerialNumber);
178 }
179 
180 }}}
Definition: MonoLockFreeBuffer.hpp:14
Definition: ContextDetail.hpp:86
Definition: TypedString.hpp:74
Definition: Timers.hpp:69
the default vanilla allocate
Definition: Allocators.hpp:114
Definition: Exception.hpp:14
Definition: LockFreeBufferT.hpp:18
Definition: Rater.hpp:10
Definition: ContextDetail.hpp:26
Definition: Client.hpp:39
Definition: Exception.hpp:22
helping allocating object and its aggregated objects in a continouse shared memory ...
Definition: Allocators.hpp:86
Definition: Client.hpp:11
Definition: LockFreeBufferMisc.hpp:73