as_criticalsection.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2017 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. //
  24. // as_criticalsection.h
  25. //
  26. // Classes for multi threading support
  27. //
  28. #ifndef AS_CRITICALSECTION_H
  29. #define AS_CRITICALSECTION_H
  30. #include "as_config.h"
  31. BEGIN_AS_NAMESPACE
  32. #ifdef AS_NO_THREADS
  33. #define DECLARECRITICALSECTION(x)
  34. #define ENTERCRITICALSECTION(x)
  35. #define LEAVECRITICALSECTION(x)
  36. inline bool tryEnter() { return true; }
  37. #define TRYENTERCRITICALSECTION(x) tryEnter()
  38. #define DECLAREREADWRITELOCK(x)
  39. #define ACQUIREEXCLUSIVE(x)
  40. #define RELEASEEXCLUSIVE(x)
  41. #define ACQUIRESHARED(x)
  42. #define RELEASESHARED(x)
  43. #else
  44. #define DECLARECRITICALSECTION(x) asCThreadCriticalSection x;
  45. #define ENTERCRITICALSECTION(x) x.Enter()
  46. #define LEAVECRITICALSECTION(x) x.Leave()
  47. #define TRYENTERCRITICALSECTION(x) x.TryEnter()
  48. #define DECLAREREADWRITELOCK(x) asCThreadReadWriteLock x;
  49. #define ACQUIREEXCLUSIVE(x) x.AcquireExclusive()
  50. #define RELEASEEXCLUSIVE(x) x.ReleaseExclusive()
  51. #define ACQUIRESHARED(x) x.AcquireShared()
  52. #define RELEASESHARED(x) x.ReleaseShared()
  53. #ifdef AS_POSIX_THREADS
  54. END_AS_NAMESPACE
  55. #include <pthread.h>
  56. BEGIN_AS_NAMESPACE
  57. class asCThreadCriticalSection
  58. {
  59. public:
  60. asCThreadCriticalSection();
  61. ~asCThreadCriticalSection();
  62. void Enter();
  63. void Leave();
  64. bool TryEnter();
  65. protected:
  66. pthread_mutex_t cs;
  67. };
  68. class asCThreadReadWriteLock
  69. {
  70. public:
  71. asCThreadReadWriteLock();
  72. ~asCThreadReadWriteLock();
  73. void AcquireExclusive();
  74. void ReleaseExclusive();
  75. bool TryAcquireExclusive();
  76. void AcquireShared();
  77. void ReleaseShared();
  78. bool TryAcquireShared();
  79. protected:
  80. pthread_rwlock_t lock;
  81. };
  82. #elif defined(AS_WINDOWS_THREADS)
  83. END_AS_NAMESPACE
  84. #ifdef AS_XBOX360
  85. #include <xtl.h>
  86. #else
  87. #ifndef WIN32_LEAN_AND_MEAN
  88. #define WIN32_LEAN_AND_MEAN
  89. #endif
  90. #ifndef _WIN32_WINNT
  91. #define _WIN32_WINNT 0x0600 // We need this to get the declaration for Windows Phone compatible Ex functions
  92. #endif
  93. #include <windows.h>
  94. #endif
  95. BEGIN_AS_NAMESPACE
  96. // Undefine macros that cause problems in our code
  97. #undef GetObject
  98. #undef RegisterClass
  99. class asCThreadCriticalSection
  100. {
  101. public:
  102. asCThreadCriticalSection();
  103. ~asCThreadCriticalSection();
  104. void Enter();
  105. void Leave();
  106. bool TryEnter();
  107. protected:
  108. CRITICAL_SECTION cs;
  109. };
  110. class asCThreadReadWriteLock
  111. {
  112. public:
  113. asCThreadReadWriteLock();
  114. ~asCThreadReadWriteLock();
  115. void AcquireExclusive();
  116. void ReleaseExclusive();
  117. void AcquireShared();
  118. void ReleaseShared();
  119. protected:
  120. // The Slim Read Write Lock object, SRWLOCK, is more efficient
  121. // but it is only available from Windows Vista so we cannot use it and
  122. // maintain compatibility with olders versions of Windows.
  123. // Critical sections and semaphores are available on Windows XP and onwards.
  124. // Windows XP is oldest version we support with multithreading.
  125. // The implementation is based on the following article, that shows
  126. // how to implement a fair read/write lock that doesn't risk starving
  127. // the writers:
  128. // http://doc.qt.nokia.com/qq/qq11-mutex.html
  129. // TODO: Allow use of SRWLOCK through configuration in as_config.h
  130. CRITICAL_SECTION writeLock;
  131. HANDLE readLocks;
  132. };
  133. // This constant really should be a member of asCThreadReadWriteLock,
  134. // but it gives a compiler error on MSVC6 so I'm leaving it outside
  135. static const asUINT maxReaders = 10;
  136. #endif
  137. #endif
  138. END_AS_NAMESPACE
  139. #endif