Add shared memory example

- Add shared memory example in examples/MQ/SharedMemory
 - Device/Task termination: try soft first, and abort if it fails
 - Interactive mode: prevent cin from blocking forever (poll)
This commit is contained in:
Alexey Rybalchenko 2016-04-07 17:02:37 +02:00
parent 599d1b3e05
commit 3353e214a7
7 changed files with 90 additions and 59 deletions

View File

@ -18,6 +18,7 @@
#include <cstdlib> #include <cstdlib>
#include <termios.h> // for the InteractiveStateLoop #include <termios.h> // for the InteractiveStateLoop
#include <poll.h>
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/random/mersenne_twister.hpp> // for choosing random port in range #include <boost/random/mersenne_twister.hpp> // for choosing random port in range
@ -59,6 +60,8 @@ FairMQDevice::FairMQDevice()
, fInitialValidationCondition() , fInitialValidationCondition()
, fInitialValidationMutex() , fInitialValidationMutex()
, fCatchingSignals(false) , fCatchingSignals(false)
, fTerminated(false)
, fRunning(false)
{ {
} }
@ -86,11 +89,20 @@ void FairMQDevice::SignalHandler(int signal)
Shutdown(); Shutdown();
fTerminateStateThread.join(); fTerminateStateThread.join();
LOG(INFO) << "Exiting.";
stop(); stop();
fRunning = false;
if (!fTerminated)
{
fTerminated = true;
LOG(INFO) << "Exiting.";
}
else
{
LOG(WARN) << "Repeated termination or bad initialization? Aborting.";
// std::abort(); // std::abort();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
}
void FairMQDevice::ConnectChannels(list<FairMQChannel*>& chans) void FairMQDevice::ConnectChannels(list<FairMQChannel*>& chans)
{ {
@ -728,8 +740,11 @@ void FairMQDevice::LogSocketRates()
void FairMQDevice::InteractiveStateLoop() void FairMQDevice::InteractiveStateLoop()
{ {
bool running = true; fRunning = true;
char c; // hold the user console input char c; // hold the user console input
pollfd cinfd[1];
cinfd[0].fd = fileno(stdin);
cinfd[0].events = POLLIN;
struct termios t; struct termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
@ -738,8 +753,17 @@ void FairMQDevice::InteractiveStateLoop()
PrintInteractiveStateLoopHelp(); PrintInteractiveStateLoopHelp();
while (running && cin >> c) while (fRunning)
{ {
if (poll(cinfd, 1, 500))
{
if (!fRunning)
{
break;
}
cin >> c;
switch (c) switch (c)
{ {
case 'i': case 'i':
@ -783,7 +807,7 @@ void FairMQDevice::InteractiveStateLoop()
ChangeState("END"); ChangeState("END");
if (CheckCurrentState("EXITING")) if (CheckCurrentState("EXITING"))
{ {
running = false; fRunning = false;
} }
break; break;
default: default:
@ -792,6 +816,7 @@ void FairMQDevice::InteractiveStateLoop()
break; break;
} }
} }
}
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag |= ICANON; // re-enable canonical input t.c_lflag |= ICANON; // re-enable canonical input
@ -921,4 +946,6 @@ FairMQDevice::~FairMQDevice()
} }
delete fTransportFactory; delete fTransportFactory;
LOG(DEBUG) << "Device destroyed";
} }

View File

@ -330,6 +330,9 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable
/// Signal handler /// Signal handler
void SignalHandler(int signal); void SignalHandler(int signal);
bool fCatchingSignals; bool fCatchingSignals;
bool fTerminated;
// Interactive state loop helper
std::atomic<bool> fRunning;
}; };
#endif /* FAIRMQDEVICE_H_ */ #endif /* FAIRMQDEVICE_H_ */

View File

@ -58,5 +58,3 @@ ForEach(_file RANGE 0 ${_length})
set(DEPENDENCIES FairMQ dds_intercom_lib) set(DEPENDENCIES FairMQ dds_intercom_lib)
GENERATE_EXECUTABLE() GENERATE_EXECUTABLE()
EndForEach(_file RANGE 0 ${_length}) EndForEach(_file RANGE 0 ${_length})

View File

@ -31,7 +31,8 @@ struct DDSConfig
/// Addresses of binding channels are published via DDS using channels names as keys /// Addresses of binding channels are published via DDS using channels names as keys
/// Addresses of connecting channels are collected from DDS using channels names as keys /// Addresses of connecting channels are collected from DDS using channels names as keys
/// \param device Reference to FairMQDevice whose channels to handle /// \param device Reference to FairMQDevice whose channels to handle
void HandleConfigViaDDS(FairMQDevice& device) template<typename TMQDevice>
void HandleConfigViaDDS(TMQDevice& device)
{ {
// container for binding channels // container for binding channels
vector<FairMQChannel*> bindingChans; vector<FairMQChannel*> bindingChans;

View File

@ -11,7 +11,7 @@
{ {
"type": "push", "type": "push",
"method": "bind", "method": "bind",
"address": "tcp://*:5555", "address": "tcp://127.0.0.1:5555",
"sndBufSize": "1000", "sndBufSize": "1000",
"rcvBufSize": "1000", "rcvBufSize": "1000",
"rateLogging": "1" "rateLogging": "1"
@ -28,7 +28,7 @@
{ {
"type": "pull", "type": "pull",
"method": "connect", "method": "connect",
"address": "tcp://localhost:5555", "address": "tcp://127.0.0.1:5555",
"sndBufSize": "1000", "sndBufSize": "1000",
"rcvBufSize": "1000", "rcvBufSize": "1000",
"rateLogging": "1" "rateLogging": "1"

View File

@ -6,7 +6,7 @@
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
/** /**
* FairMQContext.h * FairMQContextZMQ.h
* *
* @since 2012-12-05 * @since 2012-12-05
* @author D. Klein, A. Rybalchenko * @author D. Klein, A. Rybalchenko

View File

@ -19,6 +19,8 @@
#include <unordered_map> #include <unordered_map>
#include <initializer_list> #include <initializer_list>
#include <zmq.h>
#include "FairMQPoller.h" #include "FairMQPoller.h"
#include "FairMQChannel.h" #include "FairMQChannel.h"
#include "FairMQTransportFactoryZMQ.h" #include "FairMQTransportFactoryZMQ.h"