CCAllocatorMutex.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. Author: Justin Graham (https://github.com/mannewalis)
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef CC_ALLOCATOR_MUTEX_H
  22. #define CC_ALLOCATOR_MUTEX_H
  23. /// @cond DO_NOT_SHOW
  24. #include "platform/CCPlatformMacros.h"
  25. #if CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX
  26. #include "pthread.h"
  27. #define MUTEX pthread_mutex_t
  28. #define MUTEX_INIT(m) \
  29. pthread_mutexattr_t mta; \
  30. pthread_mutexattr_init(&mta); \
  31. pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE); \
  32. pthread_mutex_init(&m, &mta)
  33. #define MUTEX_LOCK(m) \
  34. pthread_mutex_lock(&m);
  35. #define MUTEX_UNLOCK(m) \
  36. pthread_mutex_unlock(&m);
  37. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  38. #include "windows.h"
  39. #define MUTEX HANDLE
  40. #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
  41. #define MUTEX_INIT(m) \
  42. m = CreateMutex(0, FALSE, 0)
  43. #define MUTEX_LOCK(m) \
  44. WaitForSingleObject(m, INFINITE)
  45. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  46. #define MUTEX_INIT(m) \
  47. m = CreateMutexEx(NULL,FALSE,0,NULL);
  48. #define MUTEX_LOCK(m) \
  49. WaitForSingleObjectEx(m, INFINITE, false)
  50. #endif
  51. #define MUTEX_UNLOCK(m) \
  52. ReleaseMutex(m)
  53. #else
  54. #message "Unsupported platform for AllocatorMutex, Locking semantics will not be supported"
  55. #define MUTEX
  56. #define MUTEX_INIT(...)
  57. #define MUTEX_LOCK(...)
  58. #define MUTEX_UNLOCK(...)
  59. #endif
  60. NS_CC_BEGIN
  61. NS_CC_ALLOCATOR_BEGIN
  62. // so we cannot use std::mutex because it allocates memory
  63. // which causes an infinite loop of death and exceptions.
  64. class AllocatorMutex
  65. {
  66. public:
  67. AllocatorMutex()
  68. {
  69. MUTEX_INIT(_mutex);
  70. }
  71. void lock()
  72. {
  73. MUTEX_LOCK(_mutex);
  74. }
  75. void unlock()
  76. {
  77. MUTEX_UNLOCK(_mutex);
  78. }
  79. protected:
  80. MUTEX _mutex;
  81. };
  82. #define LOCK(m) m.lock()
  83. #define UNLOCK(m) m.unlock()
  84. // @param implementation that provides a mutex with locking semantics.
  85. struct locking_semantics
  86. {
  87. AllocatorMutex _mutex;
  88. CC_ALLOCATOR_INLINE void lock()
  89. {
  90. LOCK(_mutex);
  91. }
  92. CC_ALLOCATOR_INLINE void unlock()
  93. {
  94. UNLOCK(_mutex);
  95. }
  96. };
  97. // @param implementation that provides lockless semantics that should optimize away.
  98. struct lockless_semantics
  99. {
  100. CC_ALLOCATOR_INLINE void lock() {}
  101. CC_ALLOCATOR_INLINE void unlock() {}
  102. };
  103. NS_CC_ALLOCATOR_END
  104. NS_CC_END
  105. /// @endcond
  106. #endif//CC_ALLOCATOR_MUTEX_H