thread.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/threads/thread.h"
  23. #include "platform/threads/semaphore.h"
  24. #include "platformWin32/platformWin32.h"
  25. #include "memory/safeDelete.h"
  26. #include <process.h> // [tom, 4/20/2006] for _beginthread()
  27. //////////////////////////////////////////////////////////////////////////
  28. // Thread data
  29. //////////////////////////////////////////////////////////////////////////
  30. struct PlatformThreadData
  31. {
  32. ThreadRunFunction mRunFunc;
  33. void* mRunArg;
  34. Thread* mThread;
  35. HANDLE mThreadHnd;
  36. Semaphore mGateway;
  37. ThreadIdent mThreadID;
  38. PlatformThreadData()
  39. {
  40. mRunFunc = NULL;
  41. mRunArg = 0;
  42. mThread = 0;
  43. mThreadHnd = 0;
  44. };
  45. };
  46. //////////////////////////////////////////////////////////////////////////
  47. // Static Functions/Methods
  48. //////////////////////////////////////////////////////////////////////////
  49. //-----------------------------------------------------------------------------
  50. // Function: ThreadRunHandler
  51. // Summary: Calls Thread::run() with the thread's specified run argument.
  52. // Neccesary because Thread::run() is provided as a non-threaded
  53. // way to execute the thread's run function. So we have to keep
  54. // track of the thread's lock here.
  55. static void ThreadRunHandler(void * arg)
  56. {
  57. PlatformThreadData* mData = reinterpret_cast<PlatformThreadData*>(arg);
  58. mData->mThreadID = ThreadManager::getCurrentThreadId();
  59. ThreadManager::addThread(mData->mThread);
  60. mData->mThread->run(mData->mRunArg);
  61. ThreadManager::removeThread(mData->mThread);
  62. // we could delete the Thread here, if it wants to be auto-deleted...
  63. mData->mGateway.release();
  64. // the end of this function is where the created win32 thread will die.
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. // Constructor/Destructor
  68. //////////////////////////////////////////////////////////////////////////
  69. Thread::Thread(ThreadRunFunction func /* = 0 */, void *arg /* = 0 */, bool start_thread /* = true */, bool autodelete /*= false*/)
  70. {
  71. mData = new PlatformThreadData;
  72. mData->mRunFunc = func;
  73. mData->mRunArg = arg;
  74. mData->mThread = this;
  75. if(start_thread)
  76. start();
  77. }
  78. Thread::~Thread()
  79. {
  80. stop();
  81. join();
  82. SAFE_DELETE(mData);
  83. }
  84. //////////////////////////////////////////////////////////////////////////
  85. // Public Methods
  86. //////////////////////////////////////////////////////////////////////////
  87. void Thread::start()
  88. {
  89. if(isAlive())
  90. return;
  91. // cause start to block out other pthreads from using this Thread,
  92. // at least until ThreadRunHandler exits.
  93. mData->mGateway.acquire();
  94. // reset the shouldStop flag, so we'll know when someone asks us to stop.
  95. shouldStop = false;
  96. mData->mThreadHnd = (HANDLE)_beginthread(ThreadRunHandler, 0, mData);
  97. }
  98. bool Thread::join()
  99. {
  100. if(!isAlive())
  101. return true;
  102. return WaitForSingleObject(mData->mThreadHnd, INFINITE) != WAIT_FAILED;
  103. }
  104. void Thread::run(void *arg /* = 0 */)
  105. {
  106. if(mData->mRunFunc)
  107. mData->mRunFunc(arg);
  108. }
  109. bool Thread::isAlive()
  110. {
  111. if(mData->mThreadHnd == 0)
  112. return false;
  113. DWORD res = WaitForSingleObject(mData->mThreadHnd, 0);
  114. return res != WAIT_OBJECT_0 && res != WAIT_FAILED;
  115. }
  116. ThreadIdent Thread::getId()
  117. {
  118. return mData->mThreadID;
  119. }
  120. ThreadIdent ThreadManager::getCurrentThreadId()
  121. {
  122. return GetCurrentThreadId();
  123. }
  124. bool ThreadManager::compare(ThreadIdent threadId_1, ThreadIdent threadId_2)
  125. {
  126. return (threadId_1 == threadId_2);
  127. }