ConditionalWait.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * ConditionalWait.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <condition_variable>
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class TerminationRequestedException : public std::exception
  14. {
  15. public:
  16. using exception::exception;
  17. const char* what() const noexcept override
  18. {
  19. return "Thread termination requested";
  20. }
  21. };
  22. class InterruptionRequestedException : public std::exception
  23. {
  24. public:
  25. using exception::exception;
  26. const char* what() const noexcept override
  27. {
  28. return "Thread termination requested";
  29. }
  30. };
  31. class ThreadInterruption
  32. {
  33. std::atomic<bool> interruptionRequested = false;
  34. public:
  35. void interruptionPoint()
  36. {
  37. bool result = interruptionRequested.exchange(false);
  38. if (result)
  39. throw InterruptionRequestedException();
  40. }
  41. void interruptThread()
  42. {
  43. interruptionRequested.store(true);
  44. }
  45. void reset()
  46. {
  47. interruptionRequested.store(false);
  48. }
  49. };
  50. class ConditionalWait
  51. {
  52. bool isBusyValue = false;
  53. bool isTerminating = false;
  54. std::condition_variable cond;
  55. std::mutex mx;
  56. void set(bool value)
  57. {
  58. std::unique_lock lock(mx);
  59. isBusyValue = value;
  60. }
  61. public:
  62. ConditionalWait() = default;
  63. void setBusy()
  64. {
  65. set(true);
  66. }
  67. void setFree()
  68. {
  69. set(false);
  70. cond.notify_all();
  71. }
  72. void requestTermination()
  73. {
  74. isTerminating = true;
  75. setFree();
  76. }
  77. bool isBusy()
  78. {
  79. std::unique_lock lock(mx);
  80. return isBusyValue;
  81. }
  82. void waitWhileBusy()
  83. {
  84. std::unique_lock un(mx);
  85. cond.wait(un, [this](){ return !isBusyValue;});
  86. if (isTerminating)
  87. throw TerminationRequestedException();
  88. }
  89. };
  90. VCMI_LIB_NAMESPACE_END