2
0

Interprocess.h 1.4 KB

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