CmThreadDefines.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*-------------------------------------------------------------------------
  2. This source file is a part of OGRE
  3. (Object-oriented Graphics Rendering Engine)
  4. For the latest info, see http://www.ogre3d.org/
  5. Copyright (c) 2000-2011 Torus Knot Software Ltd
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE
  21. -------------------------------------------------------------------------*/
  22. #pragma once
  23. #define CM_AUTO_MUTEX_NAME mutex
  24. #if CM_THREAD_SUPPORT
  25. // TODO - Port this to std threads so we aren't dependent on boost
  26. #include <boost/thread/tss.hpp>
  27. #include <boost/thread/recursive_mutex.hpp>
  28. #include <boost/thread/condition.hpp>
  29. #include <boost/thread/thread.hpp>
  30. #include <boost/thread/shared_mutex.hpp>
  31. #include <boost/thread/locks.hpp>
  32. #include <thread>
  33. #define CM_AUTO_MUTEX mutable boost::recursive_mutex CM_AUTO_MUTEX_NAME;
  34. #define CM_LOCK_AUTO_MUTEX boost::recursive_mutex::scoped_lock cmAutoMutexLock(CM_AUTO_MUTEX_NAME);
  35. #define CM_MUTEX(name) mutable boost::recursive_mutex name;
  36. #define CM_STATIC_MUTEX(name) static boost::recursive_mutex name;
  37. #define CM_STATIC_MUTEX_INSTANCE(name) boost::recursive_mutex name;
  38. #define CM_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName) boost::recursive_mutex classTypeName##::name;
  39. #define CM_LOCK_MUTEX(name) boost::recursive_mutex::scoped_lock cmnameLock(name);
  40. #define CM_LOCK_MUTEX_NAMED(mutexName, lockName) boost::recursive_mutex::scoped_lock lockName(mutexName);
  41. #define CM_LOCK_TYPE boost::recursive_mutex::scoped_lock
  42. // like CM_AUTO_MUTEX but mutex held by pointer
  43. #define CM_AUTO_SHARED_MUTEX mutable boost::recursive_mutex *CM_AUTO_MUTEX_NAME;
  44. #define CM_LOCK_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); boost::recursive_mutex::scoped_lock cmAutoMutexLock(*CM_AUTO_MUTEX_NAME);
  45. #define CM_COPY_AUTO_SHARED_MUTEX(from) assert(!CM_AUTO_MUTEX_NAME); CM_AUTO_MUTEX_NAME = from;
  46. #define CM_SET_AUTO_SHARED_MUTEX_NULL CM_AUTO_MUTEX_NAME = 0;
  47. #define CM_MUTEX_CONDITIONAL(mutex) if (mutex)
  48. #define CM_THREAD_SYNCHRONISER(sync) boost::condition sync;
  49. #define CM_STATIC_THREAD_SYNCHRONISER(sync) static boost::condition sync;
  50. #define CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName) boost::condition classTypeName##::sync;
  51. #define CM_THREAD_WAIT(sync, mutex, lock) sync.wait(lock);
  52. #define CM_THREAD_NOTIFY_ONE(sync) sync.notify_one();
  53. #define CM_THREAD_NOTIFY_ALL(sync) sync.notify_all();
  54. // Read-write mutex
  55. #define CM_RW_MUTEX(name) mutable boost::shared_mutex name
  56. #define CM_LOCK_RW_MUTEX_READ(name) boost::shared_lock<boost::shared_mutex> cmnameLock(name)
  57. #define CM_LOCK_RW_MUTEX_WRITE(name) boost::unique_lock<boost::shared_mutex> cmnameLock(name)
  58. // Thread objects and related functions
  59. #define CM_THREAD_TYPE boost::thread
  60. #define CM_THREAD_CREATE(name, worker) boost::thread* name = new (CamelotFramework::MemoryAllocator<CamelotFramework::GenAlloc>::allocate(sizeof(boost::thread))) boost::thread(worker);
  61. #define CM_THREAD_DESTROY(name) CamelotFramework::cm_delete<CamelotFramework::GenAlloc, boost::thread>(name);
  62. #define CM_THREAD_HARDWARE_CONCURRENCY boost::thread::hardware_concurrency()
  63. #define CM_THREAD_CURRENT_ID std::this_thread::get_id()
  64. #define CM_THREAD_ID_TYPE std::thread::id
  65. #define CM_THREAD_WORKER_INHERIT
  66. // Utility
  67. #define CM_THREAD_SLEEP(ms) boost::this_thread::sleep(boost::posix_time::millisec(ms));
  68. #else
  69. #define CM_AUTO_MUTEX
  70. #define CM_LOCK_AUTO_MUTEX
  71. #define CM_MUTEX(name)
  72. #define CM_STATIC_MUTEX(name)
  73. #define CM_STATIC_MUTEX_INSTANCE(name)
  74. #define CM_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName)
  75. #define CM_LOCK_MUTEX(name)
  76. #define CM_LOCK_MUTEX_NAMED(mutexName, lockName)
  77. #define CM_LOCK_TYPE UINT32
  78. #define CM_AUTO_SHARED_MUTEX
  79. #define CM_LOCK_AUTO_SHARED_MUTEX
  80. #define CM_COPY_AUTO_SHARED_MUTEX(from)
  81. #define CM_SET_AUTO_SHARED_MUTEX_NULL
  82. #define CM_MUTEX_CONDITIONAL(name) if(true)
  83. #define CM_RW_MUTEX(name)
  84. #define CM_LOCK_RW_MUTEX_READ(name)
  85. #define CM_LOCK_RW_MUTEX_WRITE(name)
  86. #define CM_THREAD_SYNCHRONISER(sync)
  87. #define CM_STATIC_THREAD_SYNCHRONISER(sync)
  88. #define CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName)
  89. #define CM_THREAD_WAIT(sync, lock)
  90. #define CM_THREAD_NOTIFY_ONE(sync)
  91. #define CM_THREAD_NOTIFY_ALL(sync)
  92. #define CM_THREAD_SLEEP(ms)
  93. #define CM_THREAD_ID_TYPE UINT32
  94. #define CM_THREAD_WORKER_INHERIT
  95. #endif