AndroidThread.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "platform/platformSemaphore.h"
  25. #include "platform/threads/mutex.h"
  26. #include "platform/platformTLS.h"
  27. #include "memory/safeDelete.h"
  28. #include <stdlib.h>
  29. struct PlatformThreadData
  30. {
  31. ThreadRunFunction mRunFunc;
  32. void* mRunArg;
  33. Thread* mThread;
  34. Semaphore mGateway; // default count is 1
  35. U32 mThreadID;
  36. };
  37. //-----------------------------------------------------------------------------
  38. // Function: ThreadRunHandler
  39. // Summary: Calls Thread::run() with the thread's specified run argument.
  40. // Neccesary because Thread::run() is provided as a non-threaded
  41. // way to execute the thread's run function. So we have to keep
  42. // track of the thread's lock here.
  43. static void *ThreadRunHandler(void * arg)
  44. {
  45. PlatformThreadData *mData = reinterpret_cast<PlatformThreadData*>(arg);
  46. Thread *thread = mData->mThread;
  47. mData->mThreadID = ThreadManager::getCurrentThreadId();
  48. ThreadManager::addThread(thread);
  49. thread->run(mData->mRunArg);
  50. mData->mGateway.release();
  51. // we could delete the Thread here, if it wants to be auto-deleted...
  52. if(thread->autoDelete)
  53. {
  54. ThreadManager::removeThread(thread);
  55. delete thread;
  56. }
  57. // return value for pthread lib's benefit
  58. return NULL;
  59. // the end of this function is where the created pthread will die.
  60. }
  61. //-----------------------------------------------------------------------------
  62. Thread::Thread(ThreadRunFunction func, void* arg, bool start_thread, bool autodelete)
  63. {
  64. mData = new PlatformThreadData;
  65. mData->mRunFunc = func;
  66. mData->mRunArg = arg;
  67. mData->mThread = this;
  68. mData->mThreadID = 0;
  69. autoDelete = autodelete;
  70. if(start_thread)
  71. start();
  72. }
  73. Thread::~Thread()
  74. {
  75. stop();
  76. join();
  77. SAFE_DELETE(mData);
  78. }
  79. void Thread::start()
  80. {
  81. if(isAlive())
  82. return;
  83. // cause start to block out other pthreads from using this Thread,
  84. // at least until ThreadRunHandler exits.
  85. mData->mGateway.acquire();
  86. // reset the shouldStop flag, so we'll know when someone asks us to stop.
  87. shouldStop = false;
  88. pthread_create((pthread_t*)(&mData->mThreadID), NULL, ThreadRunHandler, mData);
  89. }
  90. bool Thread::join()
  91. {
  92. if(!isAlive())
  93. return true;
  94. // not using pthread_join here because pthread_join cannot deal
  95. // with multiple simultaneous calls.
  96. mData->mGateway.acquire();
  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. if(mData->mThreadID == 0)
  108. return false;
  109. if( mData->mGateway.acquire(false) )
  110. {
  111. mData->mGateway.release();
  112. return false; // we got the lock, it aint alive.
  113. }
  114. else
  115. return true; // we could not get the lock, it must be alive.
  116. }
  117. ThreadIdent Thread::getId()
  118. {
  119. return mData->mThreadID;
  120. }
  121. ThreadIdent ThreadManager::getCurrentThreadId()
  122. {
  123. return (U32)pthread_self();
  124. }
  125. bool ThreadManager::compare(ThreadIdent threadId_1, ThreadIdent threadId_2)
  126. {
  127. return (bool)pthread_equal((pthread_t)threadId_1, (pthread_t)threadId_2);
  128. }
  129. class PlatformThreadStorage
  130. {
  131. public:
  132. pthread_key_t mThreadKey;
  133. };
  134. ThreadStorage::ThreadStorage()
  135. {
  136. mThreadStorage = (PlatformThreadStorage *) mStorage;
  137. constructInPlace(mThreadStorage);
  138. pthread_key_create(&mThreadStorage->mThreadKey, NULL);
  139. }
  140. ThreadStorage::~ThreadStorage()
  141. {
  142. pthread_key_delete(mThreadStorage->mThreadKey);
  143. }
  144. void *ThreadStorage::get()
  145. {
  146. return pthread_getspecific(mThreadStorage->mThreadKey);
  147. }
  148. void ThreadStorage::set(void *value)
  149. {
  150. pthread_setspecific(mThreadStorage->mThreadKey, value);
  151. }