CmThreadDefines.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #define CM_AUTO_MUTEX mutable boost::recursive_mutex CM_AUTO_MUTEX_NAME;
  33. #define CM_LOCK_AUTO_MUTEX boost::recursive_mutex::scoped_lock cmAutoMutexLock(CM_AUTO_MUTEX_NAME);
  34. #define CM_MUTEX(name) mutable boost::recursive_mutex name;
  35. #define CM_STATIC_MUTEX(name) static boost::recursive_mutex name;
  36. #define CM_STATIC_MUTEX_INSTANCE(name) boost::recursive_mutex name;
  37. #define CM_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName) boost::recursive_mutex classTypeName##::name;
  38. #define CM_LOCK_MUTEX(name) boost::recursive_mutex::scoped_lock cmnameLock(name);
  39. #define CM_LOCK_MUTEX_NAMED(mutexName, lockName) boost::recursive_mutex::scoped_lock lockName(mutexName);
  40. // like CM_AUTO_MUTEX but mutex held by pointer
  41. #define CM_AUTO_SHARED_MUTEX mutable boost::recursive_mutex *CM_AUTO_MUTEX_NAME;
  42. #define CM_LOCK_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); boost::recursive_mutex::scoped_lock cmAutoMutexLock(*CM_AUTO_MUTEX_NAME);
  43. #define CM_NEW_AUTO_SHARED_MUTEX assert(!CM_AUTO_MUTEX_NAME); CM_AUTO_MUTEX_NAME = new boost::recursive_mutex();
  44. #define CM_DELETE_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); delete 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-local pointer
  59. #define CM_THREAD_POINTER(T, var) boost::thread_specific_ptr<T> var
  60. #define CM_THREAD_POINTER_INIT(var) var(&deletePtr)
  61. #define CM_THREAD_POINTER_VAR(T, var) boost::thread_specific_ptr<T> var (&deletePtr<T>)
  62. #define CM_THREAD_POINTER_SET(var, expr) var.reset(expr)
  63. #define CM_THREAD_POINTER_GET(var) var.get()
  64. #define CM_THREAD_POINTER_DELETE(var) var.reset(0)
  65. // Thread objects and related functions
  66. #define CM_THREAD_TYPE boost::thread
  67. #define CM_THREAD_CREATE(name, worker) boost::thread* name = new boost::thread(worker);
  68. #define CM_THREAD_DESTROY(name) delete name;
  69. #define CM_THREAD_HARDWARE_CONCURRENCY boost::thread::hardware_concurrency()
  70. #define CM_THREAD_CURRENT_ID boost::this_thread::get_id()
  71. #define CM_THREAD_ID_TYPE boost::thread::id
  72. #define CM_THREAD_WORKER_INHERIT
  73. // Utility
  74. #define CM_THREAD_SLEEP(ms) boost::this_thread::sleep(boost::posix_time::millisec(ms));
  75. #else
  76. #define CM_AUTO_MUTEX
  77. #define CM_LOCK_AUTO_MUTEX
  78. #define CM_MUTEX(name)
  79. #define CM_STATIC_MUTEX(name)
  80. #define CM_STATIC_MUTEX_INSTANCE(name)
  81. #define CM_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName)
  82. #define CM_LOCK_MUTEX(name)
  83. #define CM_LOCK_MUTEX_NAMED(mutexName, lockName)
  84. #define CM_AUTO_SHARED_MUTEX
  85. #define CM_LOCK_AUTO_SHARED_MUTEX
  86. #define CM_NEW_AUTO_SHARED_MUTEX
  87. #define CM_DELETE_AUTO_SHARED_MUTEX
  88. #define CM_COPY_AUTO_SHARED_MUTEX(from)
  89. #define CM_SET_AUTO_SHARED_MUTEX_NULL
  90. #define CM_MUTEX_CONDITIONAL(name) if(true)
  91. #define CM_RW_MUTEX(name)
  92. #define CM_LOCK_RW_MUTEX_READ(name)
  93. #define CM_LOCK_RW_MUTEX_WRITE(name)
  94. #define CM_THREAD_SYNCHRONISER(sync)
  95. #define CM_STATIC_THREAD_SYNCHRONISER(sync)
  96. #define CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName)
  97. #define CM_THREAD_WAIT(sync, lock)
  98. #define CM_THREAD_NOTIFY_ONE(sync)
  99. #define CM_THREAD_NOTIFY_ALL(sync)
  100. #define CM_THREAD_POINTER(T, var) T* var
  101. #define CM_THREAD_POINTER_INIT(var) var(0)
  102. #define CM_THREAD_POINTER_VAR(T, var) T* var = 0
  103. #define CM_THREAD_POINTER_SET(var, expr) var = expr
  104. #define CM_THREAD_POINTER_GET(var) var
  105. #define CM_THREAD_POINTER_DELETE(var) { delete var; var = 0; }
  106. #define CM_THREAD_SLEEP(ms)
  107. #define CM_THREAD_ID_TYPE UINT32
  108. #define CM_THREAD_WORKER_INHERIT
  109. #endif