x86UNIXThread.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <pthread.h>
  23. #include "platform/threads/thread.h"
  24. #include "platformX86UNIX/platformX86UNIX.h"
  25. #include "platform/platformSemaphore.h"
  26. //--------------------------------------------------------------------------
  27. struct PlatformThreadData
  28. {
  29. ThreadRunFunction mRunFunc;
  30. void* mRunArg;
  31. Thread* mThread;
  32. Semaphore mGateway;
  33. pthread_t mThreadID;
  34. bool mDead;
  35. };
  36. //--------------------------------------------------------------------------
  37. Thread::Thread(ThreadRunFunction func, void* arg, bool start_thread, bool autodelete)
  38. {
  39. mData = new PlatformThreadData;
  40. mData->mRunFunc = func;
  41. mData->mRunArg = arg;
  42. mData->mThread = this;
  43. mData->mThreadID = 0;
  44. mData->mDead = false;
  45. autoDelete = autodelete;
  46. if (start_thread)
  47. start();
  48. }
  49. Thread::~Thread()
  50. {
  51. stop();
  52. if (isAlive())
  53. join();
  54. delete mData;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Function: ThreadRunHandler
  58. // Summary: Calls Thread::run() with the thread's specified run argument.
  59. // Neccesary because Thread::run() is provided as a non-threaded
  60. // way to execute the thread's run function. So we have to keep
  61. // track of the thread's lock here.
  62. static void *ThreadRunHandler(void * arg)
  63. {
  64. PlatformThreadData *mData = reinterpret_cast<PlatformThreadData*>(arg);
  65. Thread *thread = mData->mThread;
  66. // mThreadID is filled in twice, once here and once in pthread_create().
  67. // We fill in mThreadID here as well as in pthread_create() because addThread()
  68. // can execute before pthread_create() returns and sets mThreadID.
  69. // The value from pthread_create() and pthread_self() are guaranteed to be equivalent (but not identical)
  70. mData->mThreadID = pthread_self();
  71. ThreadManager::addThread(thread);
  72. thread->run(mData->mRunArg);
  73. ThreadManager::removeThread(thread);
  74. bool autoDelete = thread->autoDelete;
  75. mData->mThreadID = 0;
  76. mData->mDead = true;
  77. mData->mGateway.release();
  78. if( autoDelete )
  79. delete thread;
  80. // return value for pthread lib's benefit
  81. return NULL;
  82. // the end of this function is where the created pthread will die.
  83. }
  84. void Thread::start()
  85. {
  86. mData->mGateway.acquire();
  87. shouldStop = false;
  88. mData->mDead = false;
  89. pthread_create(&mData->mThreadID, NULL, ThreadRunHandler, mData);
  90. }
  91. bool Thread::join()
  92. {
  93. // not using pthread_join here because pthread_join cannot deal
  94. // with multiple simultaneous calls.
  95. mData->mGateway.acquire();
  96. AssertFatal( !isAlive(), "Thread::join() - thread not dead after join()" );
  97. mData->mGateway.release();
  98. return true;
  99. }
  100. void Thread::run(void* arg)
  101. {
  102. if(mData->mRunFunc)
  103. mData->mRunFunc(arg);
  104. }
  105. bool Thread::isAlive()
  106. {
  107. return ( !mData->mDead );
  108. }
  109. ThreadIdent Thread::getId()
  110. {
  111. return (ThreadIdent)mData->mThreadID;
  112. }
  113. ThreadIdent ThreadManager::getCurrentThreadId()
  114. {
  115. return pthread_self();
  116. }
  117. bool ThreadManager::compare(ThreadIdent threadId_1, ThreadIdent threadId_2)
  118. {
  119. return pthread_equal((pthread_t)threadId_1, (pthread_t)threadId_2);
  120. }
  121. class PlatformThreadStorage
  122. {
  123. public:
  124. pthread_key_t mThreadKey;
  125. };
  126. /*ThreadStorage::ThreadStorage()
  127. {
  128. mThreadStorage = (PlatformThreadStorage *) mStorage;
  129. constructInPlace(mThreadStorage);
  130. pthread_key_create(&mThreadStorage->mThreadKey, NULL);
  131. }
  132. ThreadStorage::~ThreadStorage()
  133. {
  134. pthread_key_delete(mThreadStorage->mThreadKey);
  135. }
  136. void *ThreadStorage::get()
  137. {
  138. return pthread_getspecific(mThreadStorage->mThreadKey);
  139. }
  140. void ThreadStorage::set(void *value)
  141. {
  142. pthread_setspecific(mThreadStorage->mThreadKey, value);
  143. }
  144. */