thread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. shouldStop = true;
  75. }
  76. Thread::~Thread()
  77. {
  78. stop();
  79. if( isAlive() )
  80. join();
  81. delete mData;
  82. }
  83. void Thread::start( void* arg )
  84. {
  85. // cause start to block out other pthreads from using this Thread,
  86. // at least until ThreadRunHandler exits.
  87. mData->mGateway.acquire();
  88. // reset the shouldStop flag, so we'll know when someone asks us to stop.
  89. shouldStop = false;
  90. mData->mDead = false;
  91. if( !mData->mRunArg )
  92. mData->mRunArg = arg;
  93. mData->mSdlThread = SDL_CreateThread(ThreadRunHandler,NULL,mData);
  94. }
  95. bool Thread::join()
  96. {
  97. mData->mGateway.acquire();
  98. AssertFatal( !isAlive(), "Thread::join() - thread not dead after join()" );
  99. mData->mGateway.release();
  100. return true;
  101. }
  102. void Thread::run(void* arg)
  103. {
  104. if(mData->mRunFunc)
  105. mData->mRunFunc(arg);
  106. }
  107. bool Thread::isAlive()
  108. {
  109. return ( !mData->mDead );
  110. }
  111. dsize_t Thread::getId()
  112. {
  113. return (dsize_t)mData->mThreadID;
  114. }
  115. void Thread::_setName( const char* )
  116. {
  117. // Not supported. Wading through endless lists of Thread-1, Thread-2, Thread-3, ... trying to find
  118. // that one thread you are looking for is just so much fun.
  119. }
  120. dsize_t ThreadManager::getCurrentThreadId()
  121. {
  122. return (dsize_t)SDL_ThreadID();
  123. }
  124. bool ThreadManager::compare(dsize_t threadId_1, dsize_t threadId_2)
  125. {
  126. return (threadId_1 == threadId_2);
  127. }