iOSThread.mm 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. ThreadIdent 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. @autoreleasepool {
  48. mData->mThreadID = ThreadManager::getCurrentThreadId();
  49. ThreadManager::addThread(thread);
  50. thread->run(mData->mRunArg);
  51. }
  52. mData->mGateway.release();
  53. // we could delete the Thread here, if it wants to be auto-deleted...
  54. if(thread->autoDelete)
  55. {
  56. ThreadManager::removeThread(thread);
  57. delete thread;
  58. }
  59. // return value for pthread lib's benefit
  60. return NULL;
  61. // the end of this function is where the created pthread will die.
  62. }
  63. //-----------------------------------------------------------------------------
  64. Thread::Thread(ThreadRunFunction func, void* arg, bool start_thread, bool autodelete)
  65. {
  66. mData = new PlatformThreadData;
  67. mData->mRunFunc = func;
  68. mData->mRunArg = arg;
  69. mData->mThread = this;
  70. mData->mThreadID = 0;
  71. autoDelete = autodelete;
  72. if(start_thread)
  73. start();
  74. }
  75. Thread::~Thread()
  76. {
  77. stop();
  78. join();
  79. SAFE_DELETE(mData);
  80. }
  81. void Thread::start()
  82. {
  83. if(isAlive())
  84. return;
  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. pthread_create((pthread_t*)(&mData->mThreadID), NULL, ThreadRunHandler, mData);
  91. }
  92. bool Thread::join()
  93. {
  94. if(!isAlive())
  95. return true;
  96. // not using pthread_join here because pthread_join cannot deal
  97. // with multiple simultaneous calls.
  98. mData->mGateway.acquire();
  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. if(mData->mThreadID == 0)
  110. return false;
  111. if( mData->mGateway.acquire(false) )
  112. {
  113. mData->mGateway.release();
  114. return false; // we got the lock, it aint alive.
  115. }
  116. else
  117. return true; // we could not get the lock, it must be alive.
  118. }
  119. ThreadIdent Thread::getId()
  120. {
  121. return mData->mThreadID;
  122. }
  123. ThreadIdent ThreadManager::getCurrentThreadId()
  124. {
  125. return (ThreadIdent)pthread_self();
  126. }
  127. bool ThreadManager::compare(ThreadIdent threadId_1, ThreadIdent threadId_2)
  128. {
  129. return (bool)pthread_equal((pthread_t)threadId_1, (pthread_t)threadId_2);
  130. }
  131. class PlatformThreadStorage
  132. {
  133. public:
  134. pthread_key_t mThreadKey;
  135. };
  136. ThreadStorage::ThreadStorage()
  137. {
  138. mThreadStorage = (PlatformThreadStorage *) mStorage;
  139. constructInPlace(mThreadStorage);
  140. pthread_key_create(&mThreadStorage->mThreadKey, NULL);
  141. }
  142. ThreadStorage::~ThreadStorage()
  143. {
  144. pthread_key_delete(mThreadStorage->mThreadKey);
  145. }
  146. void *ThreadStorage::get()
  147. {
  148. return pthread_getspecific(mThreadStorage->mThreadKey);
  149. }
  150. void ThreadStorage::set(void *value)
  151. {
  152. pthread_setspecific(mThreadStorage->mThreadKey, value);
  153. }