thread.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/threads/mutex.h"
  25. #include "platform/platformIntrinsics.h"
  26. #include <stdlib.h>
  27. #include <SDL.h>
  28. #include <SDL_thread.h>
  29. class PlatformThreadData
  30. {
  31. public:
  32. ThreadRunFunction mRunFunc;
  33. void* mRunArg;
  34. Thread* mThread;
  35. Semaphore mGateway;
  36. SDL_threadID mThreadID;
  37. SDL_Thread* mSdlThread;
  38. U32 mDead;
  39. PlatformThreadData()
  40. {
  41. mRunFunc = NULL;
  42. mRunArg = 0;
  43. mThread = 0;
  44. mThreadID = 0;
  45. mSdlThread = NULL;
  46. mDead = false;
  47. }
  48. };
  49. ThreadManager::MainThreadId ThreadManager::smMainThreadId;
  50. //-----------------------------------------------------------------------------
  51. // Function: ThreadRunHandler
  52. // Summary: Calls Thread::run() with the thread's specified run argument.
  53. // Neccesary because Thread::run() is provided as a non-threaded
  54. // way to execute the thread's run function. So we have to keep
  55. // track of the thread's lock here.
  56. static int ThreadRunHandler(void * arg)
  57. {
  58. PlatformThreadData *mData = reinterpret_cast<PlatformThreadData*>(arg);
  59. mData->mThreadID = SDL_ThreadID();
  60. ThreadManager::addThread(mData->mThread);
  61. mData->mThread->run(mData->mRunArg);
  62. ThreadManager::removeThread(mData->mThread);
  63. bool autoDelete = mData->mThread->autoDelete;
  64. dCompareAndSwap(mData->mDead, false, true);
  65. mData->mGateway.release();
  66. if( autoDelete )
  67. delete mData->mThread;
  68. return 0;
  69. }
  70. //-----------------------------------------------------------------------------
  71. Thread::Thread(ThreadRunFunction func, void* arg, bool start_thread, bool autodelete)
  72. {
  73. AssertFatal( !start_thread, "Thread::Thread() - auto-starting threads from ctor has been disallowed since the run() method is virtual" );
  74. mData = new PlatformThreadData;
  75. mData->mRunFunc = func;
  76. mData->mRunArg = arg;
  77. mData->mThread = this;
  78. mData->mThreadID = 0;
  79. mData->mDead = false;
  80. mData->mSdlThread = NULL;
  81. autoDelete = autodelete;
  82. shouldStop = true;
  83. }
  84. Thread::~Thread()
  85. {
  86. stop();
  87. if( isAlive() )
  88. join();
  89. delete mData;
  90. }
  91. void Thread::start( void* arg )
  92. {
  93. // cause start to block out other pthreads from using this Thread,
  94. // at least until ThreadRunHandler exits.
  95. mData->mGateway.acquire();
  96. // reset the shouldStop flag, so we'll know when someone asks us to stop.
  97. shouldStop = false;
  98. mData->mDead = false;
  99. if( !mData->mRunArg )
  100. mData->mRunArg = arg;
  101. mData->mSdlThread = SDL_CreateThread(ThreadRunHandler,NULL,mData);
  102. }
  103. bool Thread::join()
  104. {
  105. mData->mGateway.acquire();
  106. AssertFatal( !isAlive(), "Thread::join() - thread not dead after join()" );
  107. mData->mGateway.release();
  108. return true;
  109. }
  110. void Thread::run(void* arg)
  111. {
  112. if(mData->mRunFunc)
  113. mData->mRunFunc(arg);
  114. }
  115. bool Thread::isAlive()
  116. {
  117. return ( !mData->mDead );
  118. }
  119. dsize_t Thread::getId()
  120. {
  121. return (dsize_t)mData->mThreadID;
  122. }
  123. void Thread::_setName( const char* )
  124. {
  125. // Not supported. Wading through endless lists of Thread-1, Thread-2, Thread-3, ... trying to find
  126. // that one thread you are looking for is just so much fun.
  127. }
  128. dsize_t ThreadManager::getCurrentThreadId()
  129. {
  130. return (dsize_t)SDL_ThreadID();
  131. }
  132. bool ThreadManager::compare(dsize_t threadId_1, dsize_t threadId_2)
  133. {
  134. return (threadId_1 == threadId_2);
  135. }