2011-12-14 00:23:17 +03:00
|
|
|
#pragma once
|
|
|
|
|
2012-04-02 14:14:38 +03:00
|
|
|
#include <boost/interprocess/sync/scoped_lock.hpp>
|
2008-09-12 11:51:46 +03:00
|
|
|
#include <boost/interprocess/sync/interprocess_mutex.hpp>
|
|
|
|
#include <boost/interprocess/sync/interprocess_condition.hpp>
|
2009-01-11 00:08:18 +02:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
|
|
|
#include <boost/interprocess/shared_memory_object.hpp>
|
2008-09-12 11:51:46 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* Interprocess.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-09-12 11:51:46 +03:00
|
|
|
struct ServerReady
|
|
|
|
{
|
|
|
|
bool ready;
|
2016-12-04 11:54:26 +02:00
|
|
|
uint16_t port; //ui16?
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::interprocess_mutex mutex;
|
|
|
|
boost::interprocess::interprocess_condition cond;
|
2008-09-12 11:51:46 +03:00
|
|
|
|
|
|
|
ServerReady()
|
|
|
|
{
|
|
|
|
ready = false;
|
2016-12-04 11:54:26 +02:00
|
|
|
port = 0;
|
2008-09-12 11:51:46 +03:00
|
|
|
}
|
|
|
|
|
2017-06-04 14:59:11 +02:00
|
|
|
void waitTillReady()
|
2008-09-12 11:51:46 +03:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> slock(mutex);
|
|
|
|
while(!ready)
|
2012-02-20 00:03:43 +03:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
cond.wait(slock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setToReadyAndNotify(const uint16_t Port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::interprocess::interprocess_mutex> lock(mutex);
|
2012-02-20 00:03:43 +03:00
|
|
|
ready = true;
|
2016-12-04 11:54:26 +02:00
|
|
|
port = Port;
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
2008-09-12 11:51:46 +03:00
|
|
|
cond.notify_all();
|
|
|
|
}
|
|
|
|
};
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2017-06-04 14:59:11 +02:00
|
|
|
struct SharedMemory
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
const char * name;
|
2009-01-11 00:08:18 +02:00
|
|
|
boost::interprocess::shared_memory_object smo;
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::mapped_region * mr;
|
|
|
|
ServerReady * sr;
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2017-06-04 14:59:11 +02:00
|
|
|
SharedMemory(std::string Name, bool initialize = false)
|
|
|
|
: name(Name.c_str())
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
if(initialize)
|
|
|
|
{
|
|
|
|
//if the application has previously crashed, the memory may not have been removed. to avoid problems - try to destroy it
|
|
|
|
boost::interprocess::shared_memory_object::remove(name);
|
|
|
|
}
|
|
|
|
smo = boost::interprocess::shared_memory_object(boost::interprocess::open_or_create, name, boost::interprocess::read_write);
|
2009-01-11 00:08:18 +02:00
|
|
|
smo.truncate(sizeof(ServerReady));
|
2009-01-31 00:23:13 +02:00
|
|
|
mr = new boost::interprocess::mapped_region(smo,boost::interprocess::read_write);
|
2017-06-04 14:59:11 +02:00
|
|
|
if(initialize)
|
|
|
|
sr = new(mr->get_address())ServerReady();
|
|
|
|
else
|
|
|
|
sr = reinterpret_cast<ServerReady*>(mr->get_address());
|
2009-01-11 00:08:18 +02:00
|
|
|
};
|
2017-06-04 14:59:11 +02:00
|
|
|
|
|
|
|
~SharedMemory()
|
2009-01-30 23:28:02 +02:00
|
|
|
{
|
2009-01-31 00:23:13 +02:00
|
|
|
delete mr;
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::shared_memory_object::remove(name);
|
2009-01-30 23:28:02 +02:00
|
|
|
}
|
2016-01-31 17:01:58 +02:00
|
|
|
};
|