thread.cpp 4.9 KB

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