Guest User

Untitled

a guest
Jun 15th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. // Mutex class which provides the ability to check whether an sf::Mutex is locked for recursion protection.
  2. class LockDetectionSFMutex : public sf::Mutex
  3. {
  4. public:
  5.  
  6. LockDetectionSFMutex(void)
  7. {
  8. _bIsLocked = false;
  9. }
  10.  
  11. virtual ~LockDetectionSFMutex(void)
  12. {
  13. }
  14.  
  15. void lock(void)
  16. {
  17. sf::Mutex::lock();
  18.  
  19. //***
  20. // At this point, either a single thread owns the lock, or a thread
  21. // that already owned the lock has just locked it for the second time.
  22. // Since no more than one thread owns this lock, it is now safe to read
  23. // and write _bIsLocked.
  24. //***
  25.  
  26. if(_bIsLocked)
  27. {
  28. LogError("Mutex recursively acquired! Fatal error!");
  29.  
  30. // Immediately crash since this error should be logically impossible,
  31. // and it should be VERY visible if it happens.
  32. exit(-1);
  33. }
  34.  
  35. _bIsLocked = true;
  36. }
  37.  
  38. void unlock(void)
  39. {
  40. _bIsLocked = false;
  41. sf::Mutex::unlock();
  42. }
  43.  
  44. // Returns true if the mutex is locked, and false otherwise.
  45. bool IsLocked(void)
  46. {
  47. return _bIsLocked;
  48. };
  49.  
  50. private:
  51.  
  52. bool _bIsLocked;
  53. };
Advertisement
Add Comment
Please, Sign In to add comment