as_criticalsection.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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. [email protected]
  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. #define TRYENTERCRITICALSECTION(x) true
  37. #else
  38. #define DECLARECRITICALSECTION(x) asCThreadCriticalSection x
  39. #define ENTERCRITICALSECTION(x) x.Enter()
  40. #define LEAVECRITICALSECTION(x) x.Leave()
  41. #define TRYENTERCRITICALSECTION(x) x.TryEnter()
  42. #ifdef AS_POSIX_THREADS
  43. END_AS_NAMESPACE
  44. #include <pthread.h>
  45. BEGIN_AS_NAMESPACE
  46. class asCThreadCriticalSection
  47. {
  48. public:
  49. asCThreadCriticalSection();
  50. ~asCThreadCriticalSection();
  51. void Enter();
  52. void Leave();
  53. bool TryEnter();
  54. protected:
  55. pthread_mutex_t criticalSection;
  56. };
  57. #elif defined(AS_WINDOWS_THREADS)
  58. END_AS_NAMESPACE
  59. #ifdef AS_XBOX360
  60. #include <xtl.h>
  61. #else
  62. #define WIN32_LEAN_AND_MEAN
  63. #ifndef _WIN32_WINNT
  64. #define _WIN32_WINNT 0x0400 // We need this to get the declaration for TryEnterCriticalSection
  65. #endif
  66. #include <windows.h>
  67. #endif
  68. BEGIN_AS_NAMESPACE
  69. // Undefine macros that cause problems in our code
  70. #undef GetObject
  71. #undef RegisterClass
  72. class asCThreadCriticalSection
  73. {
  74. public:
  75. asCThreadCriticalSection();
  76. ~asCThreadCriticalSection();
  77. void Enter();
  78. void Leave();
  79. bool TryEnter();
  80. protected:
  81. CRITICAL_SECTION criticalSection;
  82. };
  83. #endif
  84. #endif
  85. END_AS_NAMESPACE
  86. #endif