Initialize shared memory only if it is used.

This commit is contained in:
Alexey Rybalchenko 2017-01-09 13:37:39 +01:00 committed by Mohammad Al-Turany
parent ac1da4db66
commit 8cf1cbb930
5 changed files with 43 additions and 35 deletions

View File

@ -50,20 +50,21 @@ FairMQMessageSHM::FairMQMessageSHM(void* data, const size_t size, fairmq_free_fn
, fReceiving(false)
, fQueued(false)
{
InitializeChunk(size);
memcpy(fOwner->fPtr->GetData(), data, size);
if (ffn)
if (InitializeChunk(size))
{
ffn(data, hint);
}
else
{
free(data);
memcpy(fOwner->fPtr->GetData(), data, size);
if (ffn)
{
ffn(data, hint);
}
else
{
free(data);
}
}
}
void FairMQMessageSHM::InitializeChunk(const size_t size)
bool FairMQMessageSHM::InitializeChunk(const size_t size)
{
string chunkID = fDeviceID + "c" + to_string(fMessageID);
string* ownerID = new string(fDeviceID + "o" + to_string(fMessageID));
@ -94,12 +95,17 @@ void FairMQMessageSHM::InitializeChunk(const size_t size)
}
}
if (zmq_msg_init_data(&fMessage, const_cast<char*>(ownerID->c_str()), ownerID->length(), StringDeleter, ownerID) != 0)
if (success)
{
LOG(ERROR) << "failed initializing meta message, reason: " << zmq_strerror(errno);
if (zmq_msg_init_data(&fMessage, const_cast<char*>(ownerID->c_str()), ownerID->length(), StringDeleter, ownerID) != 0)
{
LOG(ERROR) << "failed initializing meta message, reason: " << zmq_strerror(errno);
}
++fMessageID;
}
++fMessageID;
return success;
}
void FairMQMessageSHM::Rebuild()
@ -132,16 +138,17 @@ void FairMQMessageSHM::Rebuild(void* data, const size_t size, fairmq_free_fn* ff
fReceiving = false;
fQueued = false;
InitializeChunk(size);
memcpy(fOwner->fPtr->GetData(), data, size);
if (ffn)
if (InitializeChunk(size))
{
ffn(data, hint);
}
else
{
free(data);
memcpy(fOwner->fPtr->GetData(), data, size);
if (ffn)
{
ffn(data, hint);
}
else
{
free(data);
}
}
}
@ -192,9 +199,10 @@ void FairMQMessageSHM::Copy(const unique_ptr<FairMQMessage>& msg)
FairMQ::shmem::ShPtrOwner* otherOwner = static_cast<FairMQMessageSHM*>(msg.get())->fOwner;
if (otherOwner)
{
InitializeChunk(otherOwner->fPtr->GetSize());
memcpy(fOwner->fPtr->GetData(), otherOwner->fPtr->GetData(), otherOwner->fPtr->GetSize());
if (InitializeChunk(otherOwner->fPtr->GetSize()))
{
memcpy(fOwner->fPtr->GetData(), otherOwner->fPtr->GetData(), otherOwner->fPtr->GetSize());
}
}
else
{

View File

@ -28,7 +28,7 @@ class FairMQMessageSHM : public FairMQMessage
FairMQMessageSHM(const FairMQMessageSHM&) = delete;
FairMQMessageSHM operator=(const FairMQMessageSHM&) = delete;
void InitializeChunk(const size_t size);
bool InitializeChunk(const size_t size);
virtual void Rebuild();
virtual void Rebuild(const size_t size);

View File

@ -17,8 +17,8 @@ using namespace std;
using namespace FairMQ::shmem;
// Context to hold the ZeroMQ sockets
unique_ptr<FairMQContextSHM> FairMQSocketSHM::fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
// bool FairMQSocketSHM::fContextInitialized = false;
unique_ptr<FairMQContextSHM> FairMQSocketSHM::fContext; // = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
bool FairMQSocketSHM::fContextInitialized = false;
FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/)
: FairMQSocket(ZMQ_SNDMORE, ZMQ_RCVMORE, ZMQ_DONTWAIT)
@ -31,11 +31,11 @@ FairMQSocketSHM::FairMQSocketSHM(const string& type, const string& name, const i
{
fId = id + "." + name + "." + type;
// if (!fContextInitialized)
// {
// fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
// fContextInitialized = true;
// }
if (!fContextInitialized)
{
fContext = unique_ptr<FairMQContextSHM>(new FairMQContextSHM(1));
fContextInitialized = true;
}
if (zmq_ctx_set(fContext->GetContext(), ZMQ_IO_THREADS, numIoThreads) != 0)
{

View File

@ -70,7 +70,7 @@ class FairMQSocketSHM : public FairMQSocket
std::atomic<unsigned long> fMessagesRx;
static std::unique_ptr<FairMQContextSHM> fContext;
// static bool fContextInitialized;
static bool fContextInitialized;
};
#endif /* FAIRMQSOCKETSHM_H_ */

View File

@ -115,7 +115,7 @@ FairMQPollerZMQ::FairMQPollerZMQ(const unordered_map<string, vector<FairMQChanne
{
LOG(ERROR) << "At least one of the provided channel keys for poller initialization is invalid";
LOG(ERROR) << "Out of Range error: " << oor.what() << '\n';
exit(EXIT_FAILURE);
throw std::out_of_range("Invalid channel during poller initialization");
}
}