as_thread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_thread.h
  25. //
  26. // Classes for multi threading support
  27. //
  28. #ifndef AS_THREAD_H
  29. #define AS_THREAD_H
  30. #include "as_config.h"
  31. #include "as_string.h"
  32. #include "as_array.h"
  33. #include "as_map.h"
  34. #include "as_criticalsection.h"
  35. BEGIN_AS_NAMESPACE
  36. class asCThreadLocalData;
  37. class asCThreadManager : public asIThreadManager
  38. {
  39. public:
  40. static asCThreadLocalData *GetLocalData();
  41. static int CleanupLocalData();
  42. static int Prepare(asIThreadManager *externalThreadMgr);
  43. static void Unprepare();
  44. // This read/write lock can be used by the application to provide simple synchronization
  45. DECLAREREADWRITELOCK(appRWLock)
  46. protected:
  47. asCThreadManager();
  48. ~asCThreadManager();
  49. // No need to use the atomic int here, as it will only be
  50. // updated within the thread manager's critical section
  51. int refCount;
  52. #ifndef AS_NO_THREADS
  53. #if defined(_MSC_VER) && defined(AS_WINDOWS_THREADS) && (WINAPI_FAMILY & WINAPI_FAMILY_PHONE_APP)
  54. // On Windows Store we must use MSVC specific thread variables for thread
  55. // local storage, as the TLS API isn't available. On desktop we can't use
  56. // this as it may cause problems if the library is used in a dll.
  57. // ref: http://msdn.microsoft.com/en-us/library/2s9wt68x.aspx
  58. // ref: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
  59. __declspec(thread) static asCThreadLocalData *tld;
  60. #else
  61. asDWORD tlsKey;
  62. #endif
  63. DECLARECRITICALSECTION(criticalSection)
  64. #else
  65. asCThreadLocalData *tld;
  66. #endif
  67. };
  68. //======================================================================
  69. class asIScriptContext;
  70. class asCThreadLocalData
  71. {
  72. public:
  73. asCArray<asIScriptContext *> activeContexts;
  74. asCString string;
  75. protected:
  76. friend class asCThreadManager;
  77. asCThreadLocalData();
  78. ~asCThreadLocalData();
  79. };
  80. END_AS_NAMESPACE
  81. #endif