Interprocess.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  2. #include <boost/interprocess/sync/interprocess_condition.hpp>
  3. #include <boost/interprocess/mapped_region.hpp>
  4. #include <boost/interprocess/shared_memory_object.hpp>
  5. /*
  6. * Interprocess.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. struct ServerReady
  15. {
  16. bool ready;
  17. boost::interprocess::interprocess_mutex mutex;
  18. boost::interprocess::interprocess_condition cond;
  19. ServerReady()
  20. {
  21. ready = false;
  22. }
  23. void setToTrueAndNotify()
  24. {
  25. mutex.lock();
  26. ready = true;
  27. mutex.unlock();
  28. cond.notify_all();
  29. }
  30. };
  31. struct SharedMem
  32. {
  33. boost::interprocess::shared_memory_object smo;
  34. boost::interprocess::mapped_region *mr;
  35. ServerReady *sr;
  36. SharedMem() //c-tor
  37. :smo(boost::interprocess::open_or_create,"vcmi_memory",boost::interprocess::read_write)
  38. {
  39. smo.truncate(sizeof(ServerReady));
  40. mr = new boost::interprocess::mapped_region(smo,boost::interprocess::read_write);
  41. sr = new(mr->get_address())ServerReady();
  42. };
  43. ~SharedMem() //d-tor
  44. {
  45. delete mr;
  46. boost::interprocess::shared_memory_object::remove("vcmi_memory");
  47. }
  48. };