Interprocess.h 1.3 KB

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