thread.cpp 4.5 KB

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