Critical.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include <condition_variable>
  3. #include <initializer_list>
  4. #include "OperatingSystem.h"
  5. namespace Framework
  6. {
  7. class Thread;
  8. class Lock
  9. {
  10. public:
  11. virtual void lock() = 0;
  12. virtual bool tryLock() = 0;
  13. virtual void unlock() = 0;
  14. };
  15. class Critical : public Lock
  16. {
  17. private:
  18. CRITICAL_SECTION cs;
  19. int lockCount;
  20. public:
  21. //! Constructor
  22. DLLEXPORT Critical();
  23. //! Destructor
  24. DLLEXPORT ~Critical();
  25. //! Locks the object
  26. DLLEXPORT void lock() override;
  27. //! Tries to lock the object
  28. DLLEXPORT bool tryLock() override;
  29. //! Unlocks the object
  30. DLLEXPORT void unlock() override;
  31. //! Returns true if the object is locked
  32. DLLEXPORT bool isLocked() const;
  33. };
  34. class StackLock
  35. {
  36. private:
  37. Lock** locks;
  38. int size;
  39. public:
  40. DLLEXPORT StackLock(std::initializer_list<Lock*> locks);
  41. DLLEXPORT ~StackLock();
  42. };
  43. #define LOCK(x) Framework::StackLock _lock(x)
  44. #define LOCKN(x, i) Framework::StackLock _lock_##i(x)
  45. class Synchronizer
  46. {
  47. private:
  48. std::condition_variable block;
  49. std::mutex mutex;
  50. int storedNotifications;
  51. int numWaiting;
  52. bool skip;
  53. public:
  54. DLLEXPORT Synchronizer();
  55. DLLEXPORT ~Synchronizer();
  56. DLLEXPORT bool wait();
  57. DLLEXPORT bool wait(int milisec);
  58. DLLEXPORT void notify(bool store = 0);
  59. DLLEXPORT void notify(int amount, bool store = 0);
  60. DLLEXPORT void notifyAll(bool store = 0);
  61. DLLEXPORT int getNumberOfWaitingThreads() const;
  62. };
  63. class InternalReadLock;
  64. class InternalWriteLock;
  65. class ReadWriteLock
  66. {
  67. private:
  68. int* readerThreads;
  69. int* readCounters;
  70. int maxSize;
  71. int readerThreadCount;
  72. int writerThread;
  73. int writerCount;
  74. int waitingReaders;
  75. int waitingWriters;
  76. Critical cs;
  77. Synchronizer readerBlock;
  78. Synchronizer writerBlock;
  79. InternalReadLock* readLock;
  80. InternalWriteLock* writeLock;
  81. public:
  82. DLLEXPORT ReadWriteLock(int initialMaxSize = 10);
  83. DLLEXPORT ~ReadWriteLock();
  84. DLLEXPORT void lockRead();
  85. DLLEXPORT void lockWrite();
  86. DLLEXPORT void unlockRead();
  87. DLLEXPORT void unlockWrite();
  88. DLLEXPORT bool tryLockRead();
  89. DLLEXPORT bool tryLockWrite();
  90. DLLEXPORT Lock& getReadLock() const;
  91. DLLEXPORT Lock& getWriteLock() const;
  92. DLLEXPORT int getReadThreadCount() const;
  93. DLLEXPORT bool isWriteLocked() const;
  94. DLLEXPORT int getWriteLockThread() const;
  95. DLLEXPORT int getReadThread(int index) const;
  96. DLLEXPORT int getWriteLockCount() const;
  97. DLLEXPORT int getReadLockCount(int threadId) const;
  98. };
  99. } // namespace Framework