-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4001 from IvanSavenko/stabilization
[1.5.2] Stabilization
- Loading branch information
Showing
37 changed files
with
237 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* ConditionalWait.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 | ||
* | ||
*/ | ||
#pragma once | ||
|
||
#include <condition_variable> | ||
|
||
VCMI_LIB_NAMESPACE_BEGIN | ||
|
||
class TerminationRequestedException : public std::exception | ||
{ | ||
public: | ||
using exception::exception; | ||
|
||
const char* what() const noexcept override | ||
{ | ||
return "Thread termination requested"; | ||
} | ||
}; | ||
|
||
class ConditionalWait | ||
{ | ||
bool isBusyValue = false; | ||
bool isTerminating = false; | ||
std::condition_variable cond; | ||
std::mutex mx; | ||
|
||
void set(bool value) | ||
{ | ||
boost::unique_lock<std::mutex> lock(mx); | ||
isBusyValue = value; | ||
} | ||
|
||
public: | ||
ConditionalWait() = default; | ||
|
||
void setBusy() | ||
{ | ||
set(true); | ||
} | ||
|
||
void setFree() | ||
{ | ||
set(false); | ||
cond.notify_all(); | ||
} | ||
|
||
void requestTermination() | ||
{ | ||
isTerminating = true; | ||
setFree(); | ||
} | ||
|
||
bool isBusy() | ||
{ | ||
std::unique_lock<std::mutex> lock(mx); | ||
return isBusyValue; | ||
} | ||
|
||
void waitWhileBusy() | ||
{ | ||
std::unique_lock<std::mutex> un(mx); | ||
while(isBusyValue) | ||
cond.wait(un); | ||
|
||
if (isTerminating) | ||
throw TerminationRequestedException(); | ||
} | ||
}; | ||
|
||
VCMI_LIB_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.