Belle II Software light-2406-ragdoll
ZMQParent.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/pcore/zmq/utils/ZMQParent.h>
9#include <bitset>
10
11using namespace std;
12using namespace Belle2;
13
15{
16 terminate();
17}
18
20{
21 if (m_context) {
22 m_context->close();
23 m_context.reset();
24 }
25}
26
28{
29 m_context.release();
30}
31
32std::string ZMQParent::createIdentity(unsigned int pid)
33{
34 char hostname[HOST_NAME_MAX];
35 gethostname(hostname, HOST_NAME_MAX);
36
37 if (pid == 0) {
38 pid = getpid();
39 }
40
41 return std::string(hostname) + "_" + std::to_string(pid);
42}
43
45{
46 if (m_context) {
47 return;
48 }
49 m_context = std::make_unique<zmq::context_t>(1);
50}
51
52#if defined(__GNUC__) && !defined(__clang__)
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wstack-usage="
55#endif
56unsigned int ZMQParent::poll(const std::vector<zmq::socket_t*>& socketList, int timeout)
57{
58 B2ASSERT("Only allow to poll on maximal 8 sockets at the same time!", socketList.size() <= 8);
59 std::bitset<8> return_bitmask;
60 zmq::pollitem_t items[socketList.size()];
61
62 for (unsigned int i = 0; i < socketList.size(); i++) {
63 items[i].socket = static_cast<void*>(*socketList[i]);
64 items[i].events = ZMQ_POLLIN;
65 items[i].revents = 0;
66 }
67
68 try {
69 zmq::poll(items, socketList.size(), timeout);
70
71 for (unsigned int i = 0; i < socketList.size(); i++) {
72 return_bitmask[i] = static_cast<bool>(items[i].revents & ZMQ_POLLIN);
73 }
74 return return_bitmask.to_ulong();
75 } catch (zmq::error_t& error) {
76 if (error.num() == EINTR) {
77 return 0;
78 } else {
79 // cannot handle, rethrow exception
80 throw;
81 }
82 }
83}
84#if defined(__GNUC__) && !defined(__clang__)
85#pragma GCC diagnostic pop
86#endif
void initialize()
Initialize the parent by creating the context.
Definition: ZMQParent.cc:44
static unsigned int poll(const std::vector< zmq::socket_t * > &socketList, int timeout)
Poll function.
Definition: ZMQParent.cc:56
void terminate()
Terminate the parent manually (before calling its destructor). You probably do not need to do this.
Definition: ZMQParent.cc:19
static std::string createIdentity(unsigned int pid=0)
Create a unique ZMQ identity in the form <hostname>_<pid> (if pid is 0, use the current processes PID...
Definition: ZMQParent.cc:32
std::unique_ptr< zmq::context_t > m_context
ZMQ context.
Definition: ZMQParent.h:98
~ZMQParent()
Destroy the parent by terminating the ZMQ context.
Definition: ZMQParent.cc:14
void reset()
Expert function: Reset the parent without context closing. ATTENTION: which will not clean up properl...
Definition: ZMQParent.cc:27
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24
STL namespace.