Belle II Software light-2607-kasei
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
18
20{
21 if (m_context) {
22 m_context->close();
23 m_context.reset();
24 }
25}
26
28{
29 // Deliberately give up ownership without destructing: running the ZMQ destructor
30 // in a forked process would tear down file descriptors owned by the parent.
31 // cppcheck-suppress ignoredReturnValue
32 m_context.release();
33}
34
35std::string ZMQParent::createIdentity(unsigned int pid)
36{
37 char hostname[HOST_NAME_MAX];
38 gethostname(hostname, HOST_NAME_MAX);
39
40 if (pid == 0) {
41 pid = getpid();
42 }
43
44 return std::string(hostname) + "_" + std::to_string(pid);
45}
46
48{
49 if (m_context) {
50 return;
51 }
52 m_context = std::make_unique<zmq::context_t>(1);
53}
54
55#if defined(__GNUC__) && !defined(__clang__)
56#pragma GCC diagnostic push
57#pragma GCC diagnostic ignored "-Wstack-usage="
58#endif
59unsigned int ZMQParent::poll(const std::vector<zmq::socket_t*>& socketList, int timeout)
60{
61 B2ASSERT("Only allow to poll on maximal 8 sockets at the same time!", socketList.size() <= 8);
62 std::vector<zmq::pollitem_t> items(socketList.size());
63
64 for (unsigned int i = 0; i < socketList.size(); i++) {
65 items[i].socket = static_cast<void*>(*socketList[i]);
66 items[i].events = ZMQ_POLLIN;
67 items[i].revents = 0;
68 }
69
70 try {
71 zmq::poll(items.data(), socketList.size(), timeout);
72 std::bitset<8> return_bitmask;
73
74 for (unsigned int i = 0; i < socketList.size(); i++) {
75 return_bitmask[i] = static_cast<bool>(items[i].revents & ZMQ_POLLIN);
76 }
77 return return_bitmask.to_ulong();
78 } catch (zmq::error_t& error) {
79 if (error.num() == EINTR) {
80 return 0;
81 } else {
82 // cannot handle, rethrow exception
83 throw;
84 }
85 }
86}
87#if defined(__GNUC__) && !defined(__clang__)
88#pragma GCC diagnostic pop
89#endif
void initialize()
Initialize the parent by creating the context.
Definition ZMQParent.cc:47
static unsigned int poll(const std::vector< zmq::socket_t * > &socketList, int timeout)
Poll function.
Definition ZMQParent.cc:59
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:35
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.
STL namespace.