Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // threading.cpp
  2. //
  3. // (C) 2006 - 2009 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. // For details on the Threading module and development history see threading.hpp
  22. #include "threading.hpp"
  23. using namespace std; // Introduce std namespace.
  24. namespace CodeDweller {
  25. ThreadManager Threads; // Master thread manager.
  26. void ThreadManager::rememberThread(Thread* T) { // Threads register themselves.
  27. ScopeMutex ThereCanBeOnlyOne(MyMutex); // Protect the known pool.
  28. KnownThreads.insert(T); // Add the new thread pointer.
  29. }
  30. void ThreadManager::forgetThread(Thread* T) { // Threads remove themselves.
  31. ScopeMutex ThereCanBeOnlyOne(MyMutex); // Protect the known pool.
  32. KnownThreads.erase(T); // Add the new thread pointer.
  33. }
  34. ThreadStatusReport ThreadManager::StatusReport() { // Get a status report, All Threads.
  35. ScopeMutex ThereCanBeOnlyOne(MyMutex); // Protect our set -- a moment in time.
  36. ThreadStatusReport Answer; // Create our vector to hold the report.
  37. for( // Loop through all of the Threads.
  38. set<Thread*>::iterator iT = KnownThreads.begin();
  39. iT != KnownThreads.end(); iT++
  40. ) { // Grab each Threads' report.
  41. Thread& X = *(*iT); // Handy reference to the Thread.
  42. Answer.push_back(X.StatusReport()); // Push back each Thread's report.
  43. }
  44. return Answer; // Return the finished report.
  45. }
  46. bool ThreadManager::lockExistingThread(Thread* T) { // Locks ThreadManager if T exists.
  47. MyMutex.lock(); // Lock the mutex for everyone.
  48. if(KnownThreads.end() == KnownThreads.find(T)) { // If we do not find T in our set
  49. MyMutex.unlock(); // then unlock the mutex and return
  50. return false; // false.
  51. } // If we did find it then
  52. LockedThread = T; // set our locked thread and
  53. return true; // return true;
  54. }
  55. const RuntimeCheck ThreadingCheck1("ThreadManager::unlockExistingThread():ThreadingCheck1(0 != LockedThread)");
  56. const RuntimeCheck ThreadingCheck2("ThreadManager::unlockExistingThread():ThreadingCheck2(T == LockedThread)");
  57. void ThreadManager::unlockExistingThread(Thread* T) { // Unlocks ThreadManager if T locked.
  58. ThreadingCheck1(0 != LockedThread); // We had better have a locked thread.
  59. ThreadingCheck2(T == LockedThread); // The locked thread had better match.
  60. LockedThread = 0; // Clear the locked thread.
  61. MyMutex.unlock(); // Unlock the mutex.
  62. }
  63. //// Scope Thread Lock allows for a safe way to lock threads through the Threads
  64. //// object for delivering short messages. Just like a ScopeMutex, when the object
  65. //// goes away the lock is released.
  66. ScopeThreadLock::ScopeThreadLock(Thread* T) : // Construct a scope lock on a Thread.
  67. MyLockedThread(0) { // To star with we have no lock.
  68. if(Threads.lockExistingThread(T)) { // If we achieve a lock then we
  69. MyLockedThread = T; // remember it. Our destructor will
  70. } // unlock it if we were successful.
  71. }
  72. ScopeThreadLock::~ScopeThreadLock() { // Destruct a scope lock on a Thread.
  73. if(0 != MyLockedThread) { // If we were successfully constructed
  74. Threads.unlockExistingThread(MyLockedThread); // we can unlock the thread and
  75. MyLockedThread = 0; // forget about it before we go away.
  76. }
  77. }
  78. bool ScopeThreadLock::isGood() { // If we have successfully locked T
  79. return (0 != MyLockedThread) ? true:false; // it will NOT be 0, so return true.
  80. }
  81. bool ScopeThreadLock::isBad() { // If we did not successfully lock T
  82. return (0 == MyLockedThread) ? false:true; // it will be 0, so return false.
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. // Thread
  86. const ThreadType Thread::Type("Generic Thread");
  87. const ThreadState Thread::ThreadInitialized("Thread Initialized");
  88. const ThreadState Thread::ThreadStarted("Thread Started");
  89. const ThreadState Thread::ThreadFailed("Thread Failed");
  90. const ThreadState Thread::ThreadStopped("Thread Stopped");
  91. const ThreadState Thread::ThreadDestroyed("Thread Destroyed");
  92. bool Thread::isRunning() { return RunningFlag; } // Return RunningFlag state.
  93. bool Thread::isBad() { return BadFlag; } // Return BadFlag state.
  94. const string Thread::MyFault() { return BadWhat; } // Return exception Bad fault if any.
  95. const string Thread::MyName() const { return MyThreadName; } // Return the instance name if any.
  96. const ThreadType& Thread::MyType() { return MyThreadType; } // Return the instance Thread Type.
  97. const ThreadState& Thread::MyState() { return (*MyThreadState); } // Thread state for this instance.
  98. void Thread::CurrentThreadState(const ThreadState& TS) { // Set Current Thread State.
  99. MyThreadState = const_cast<ThreadState*>(&TS);
  100. }
  101. const ThreadState& Thread::CurrentThreadState() { return (*MyThreadState); } // Get Current Thread State.
  102. ThreadStatusRecord Thread::StatusReport() { // Get a status report from this thread.
  103. return
  104. ThreadStatusRecord( // Status record.
  105. this,
  106. const_cast<ThreadType&>(MyThreadType),
  107. *MyThreadState,
  108. RunningFlag,
  109. BadFlag,
  110. BadWhat,
  111. MyThreadName
  112. );
  113. }
  114. // launchTask() calls and monitors myTask for exceptions and set's the correct
  115. // states for the isBad and isRunning flags.
  116. void Thread::launchTask() { // Launch and watch myTask()
  117. try { // Do this safely.
  118. RunningFlag = true; // Now we are running.
  119. CurrentThreadState(ThreadStarted); // Set the running state.
  120. myTask(); // myTask() is called.
  121. } // myTask() should handle exceptions.
  122. catch(exception& e) { // Unhandled exceptions are informative:
  123. BadFlag = true; // They mean the thread went bad but
  124. BadWhat = e.what(); // we have an idea what went wrong.
  125. } // We shouldn't get other kinds of
  126. catch(...) { // exceptions because if things go
  127. BadFlag = true; // wrong and one gets through this
  128. BadWhat = "Unkown Exception(...)"; // is all we can say about it.
  129. }
  130. RunningFlag = false; // When we're done, we're done.
  131. if(BadFlag) CurrentThreadState(ThreadFailed); // If we're bad we failed.
  132. else CurrentThreadState(ThreadStopped); // If we're not bad we stopped.
  133. }
  134. // getMyThread() returns the local thread primative.
  135. thread_primative Thread::getMyThread() { return MyThread; } // Return my thread primative.
  136. // runThreadTask() is a helper function to start threads. It is the function
  137. // that is acutally launched as a new thread. It's whole job is to call the
  138. // myTask() method on the object passed to it as it is launched.
  139. // The run() method creates a new thread with ThreadRunner() as the main
  140. // function, having passed it's object.
  141. // WIN32 and POSIX have different versions of both the main thread function
  142. // and the way to launch it.
  143. #ifdef WIN32
  144. Thread::Thread() : // When constructing a WIN32 thread
  145. MyThreadType(Thread::Type), // Use generic Thread Type.
  146. MyThreadName("UnNamed Thread"), // Use a generic Thread Name.
  147. MyThread(NULL), // Null the thread handle.
  148. RunningFlag(false), // Couldn't be running yet.
  149. BadFlag(false) { // Couldn't be bad yet.
  150. Threads.rememberThread(this); // Remember this thread.
  151. CurrentThreadState(ThreadInitialized); // Set our initialized state.
  152. }
  153. Thread::Thread(const ThreadType& T, const string N) : // Construct with specific Type/Name
  154. MyThreadType(T), // Use generic Thread Type.
  155. MyThreadName(N), // Use a generic Thread Name.
  156. MyThread(NULL), // Null the thread handle.
  157. RunningFlag(false), // Couldn't be running yet.
  158. BadFlag(false) { // Couldn't be bad yet.
  159. Threads.rememberThread(this); // Remember this thread.
  160. CurrentThreadState(ThreadInitialized); // Set our initialized state.
  161. }
  162. Thread::~Thread() { // In WIN32 land when we destroy the
  163. if(NULL != MyThread) { // thread object check for a valid
  164. CloseHandle(MyThread); // thread handle and destroy it if
  165. } // it exists.
  166. RunningFlag = false; // The thread is not running.
  167. Threads.forgetThread(this); // Forget this thread.
  168. CurrentThreadState(ThreadDestroyed); // The Thread has left the building.
  169. }
  170. unsigned __stdcall runThreadTask(void* thread_object) { // The WIN32 version has this form.
  171. ((Thread*)thread_object)->launchTask(); // Run the task.
  172. _endthreadex(0); // Signal the thread is finished.
  173. return 0; // Satisfy the unsigned return.
  174. }
  175. void Thread::run() { // Run a WIN32 thread...
  176. unsigned tid; // Thread id to toss. Only need Handle.
  177. MyThread = (HANDLE) _beginthreadex(NULL,0,runThreadTask,this,0,&tid); // Create a thread calling ThreadRunner
  178. if(NULL == MyThread) BadFlag = true; // and test that the resutl was valid.
  179. }
  180. void Thread::join() { // To join in WIN32
  181. WaitForSingleObject(MyThread, INFINITE); // Wait for the thread by handle.
  182. }
  183. #else
  184. Thread::Thread() : // POSIX Thread constructor.
  185. MyThreadType(Thread::Type), // Use a generic Thread Type.
  186. MyThreadName("UnNamed Thread"), // Use a generic Thread Name.
  187. RunningFlag(false), // Can't be running yet.
  188. BadFlag(false) { // Can't be bad yet.
  189. Threads.rememberThread(this); // Remember this thread.
  190. CurrentThreadState(ThreadInitialized); // Set our initialized state.
  191. }
  192. Thread::Thread(const ThreadType& T, const string N) : // POSIX Specific Thread Constructor.
  193. MyThreadType(T), // Use a generic Thread Type.
  194. MyThreadName(N), // Use a generic Thread Name.
  195. RunningFlag(false), // Can't be running yet.
  196. BadFlag(false) { // Can't be bad yet.
  197. Threads.rememberThread(this); // Remember this thread.
  198. CurrentThreadState(ThreadInitialized); // Set our initialized state.
  199. }
  200. Thread::~Thread() { // POSIX destructor.
  201. RunningFlag = false; // Not running now for sure.
  202. Threads.forgetThread(this); // Forget this thread.
  203. CurrentThreadState(ThreadDestroyed); // The Thread has left the building.
  204. }
  205. void* runThreadTask(void* thread_object) { // The POSIX version has this form.
  206. ((Thread*)thread_object)->launchTask();
  207. return NULL;
  208. }
  209. void Thread::run() { // Run a POSIX thread...
  210. int result = pthread_create(&MyThread, NULL, runThreadTask, this); // Create a thread calling ThreadRunner
  211. if(0 != result) BadFlag = true; // and test that there was no error.
  212. }
  213. void Thread::join() { // To join in POSIX
  214. pthread_join(MyThread, NULL); // call pthread_join with MyThread.
  215. }
  216. #endif
  217. // End Thread
  218. ////////////////////////////////////////////////////////////////////////////////
  219. ////////////////////////////////////////////////////////////////////////////////
  220. // Mutex
  221. #ifdef WIN32
  222. // WIN32 Mutex Implementation //////////////////////////////////////////////////
  223. // The original design of the WIN32 Mutex used critical sections. However after
  224. // additional research it was determined that the use of a Semaphore with an
  225. // initial count of 1 would work better overall on multiple Winx platforms -
  226. // especially SMP systems.
  227. const RuntimeCheck ThreadingCheck3("Mutex::Mutex():ThreadingCheck3(NULL != MyMutex)");
  228. Mutex::Mutex() : // Creating a WIN32 Mutex means
  229. IAmLocked(false) { // Setting IAmLocked to false and
  230. MyMutex = CreateSemaphore(NULL, 1, 1, NULL); // create a semaphore object with
  231. ThreadingCheck3(NULL != MyMutex); // a count of 1.
  232. }
  233. const ExitCheck ThreadingCheck4("Mutex::~Mutex():");
  234. Mutex::~Mutex() { // Destroying a WIN32 Mutex means
  235. ThreadingCheck4(false == IAmLocked); // Make sure we're not in use and
  236. CloseHandle(MyMutex); // destroy the semaphore object.
  237. }
  238. bool Mutex::tryLock() { // Trying to lock WIN32 Mutex means
  239. bool DoIHaveIt = false; // Start with a pessimistic assumption
  240. if(
  241. false == IAmLocked && // If we have a shot at this and
  242. WAIT_OBJECT_0 == WaitForSingleObject(MyMutex, 0) // we actually get hold of the semaphore
  243. ) { // then we can set our flags...
  244. IAmLocked = true; // Set IAmLocked, because we are and
  245. DoIHaveIt = true; // set our result to true.
  246. }
  247. return DoIHaveIt; // Return true if we got it (see above).
  248. }
  249. const RuntimeCheck ThreadingCheck5("Mutex::lock():ThreadingCheck5(WAIT_OBJECT_0 == WaitForSingleObject(MyMutex, INFINITE))");
  250. void Mutex::lock() { // Locking the WIN32 Mutex means
  251. ThreadingCheck5(WAIT_OBJECT_0 == WaitForSingleObject(MyMutex, INFINITE)); // Wait on the semaphore - only 1 will
  252. IAmLocked = true; // get through or we have a big problem.
  253. }
  254. const LogicCheck ThreadingCheck6("Mutex::unlock():ThreadingCheck6(true == IAmLocked)");
  255. void Mutex::unlock() { // Unlocking the WIN32 Mutex means
  256. ThreadingCheck6(true == IAmLocked); // making sure we're really locked then
  257. IAmLocked = false; // reset the IAmLocked flag and
  258. ReleaseSemaphore(MyMutex, 1, NULL); // release the semaphore.
  259. }
  260. bool Mutex::isLocked() { return IAmLocked; } // Return the IAmLocked flag.
  261. #else
  262. // POSIX Mutex Implementation //////////////////////////////////////////////////
  263. const RuntimeCheck ThreadingCheck7("Mutex::Mutex():ThreadingCheck7(0 == pthread_mutex_init(&MyMutex,NULL))");
  264. Mutex::Mutex() : // Constructing a POSIX mutex means
  265. IAmLocked(false) { // setting the IAmLocked flag to false and
  266. ThreadingCheck7(0 == pthread_mutex_init(&MyMutex,NULL)); // initializing the mutex_t object.
  267. }
  268. const ExitCheck ThreadingCheck8("Mutex::~Mutex():ThreadingCheck8(false == IAmLocked)");
  269. const ExitCheck ThreadingCheck9("Mutex::~Mutex():ThreadingCheck9(0 == pthread_mutex_destroy(&MyMutex))");
  270. Mutex::~Mutex() { // Before we destroy our mutex we check
  271. ThreadingCheck8(false == IAmLocked); // to see that it is not locked and
  272. ThreadingCheck9(0 == pthread_mutex_destroy(&MyMutex)); // destroy the primative.
  273. }
  274. const RuntimeCheck ThreadingCheck10("Mutex::lock():ThreadingCheck10(0 == pthread_mutex_lock(&MyMutex));");
  275. void Mutex::lock() { // Locking a POSIX mutex means
  276. ThreadingCheck10(0 == pthread_mutex_lock(&MyMutex)); // asserting our lock was successful and
  277. IAmLocked = true; // setting the IAmLocked flag.
  278. }
  279. const LogicCheck ThreadingCheck11("Mutex::unlock():ThreadingCheck11(true == IAmLocked)");
  280. const RuntimeCheck ThreadingCheck12("Mutex::unlock():ThreadingCheck12(0 == pthread_mutex_unlock(&MyMutex))");
  281. void Mutex::unlock() { // Unlocking a POSIX mutex means
  282. ThreadingCheck11(true == IAmLocked); // asserting that we are locked,
  283. IAmLocked = false; // clearing the IAmLocked flag, and
  284. ThreadingCheck12(0 == pthread_mutex_unlock(&MyMutex)); // unlocking the actual mutex.
  285. }
  286. bool Mutex::tryLock() { // Trying to lock a POSIX mutex means
  287. bool DoIHaveIt = false; // starting off pessimistically.
  288. if(false == IAmLocked) { // If we are not locked yet then we
  289. if(0 == pthread_mutex_trylock(&MyMutex)) { // try to lock the mutex. If we succeed
  290. IAmLocked = true; // we set our IAmLocked flag and our
  291. DoIHaveIt = true; // DoIHaveIt flag to true;
  292. }
  293. }
  294. return DoIHaveIt; // In any case we return the result.
  295. }
  296. bool Mutex::isLocked() { return IAmLocked; } // Return the IAmLocked flag.
  297. #endif
  298. // End Mutex
  299. ////////////////////////////////////////////////////////////////////////////////
  300. ////////////////////////////////////////////////////////////////////////////////
  301. // ScopeMutex
  302. ScopeMutex::ScopeMutex(Mutex& M) : // When constructing a ScopeMutex,
  303. MyMutex(M) { // Initialize MyMutex with what we are given
  304. MyMutex.lock(); // and then immediately lock it.
  305. }
  306. ScopeMutex::~ScopeMutex() { // When a ScopeMutex is destroyed,
  307. MyMutex.unlock(); // it first unlocks it's mutex.
  308. }
  309. // End ScopeMutex
  310. ////////////////////////////////////////////////////////////////////////////////
  311. ////////////////////////////////////////////////////////////////////////////////
  312. // Production Gateway
  313. #ifdef WIN32
  314. // Win32 Implementation ////////////////////////////////////////////////////////
  315. const RuntimeCheck ThreadingCheck13("ProductionGateway::ProductionGateway():ThreadingCheck13(NULL != MySemaphore)");
  316. ProductionGateway::ProductionGateway() { // Construct in Windows like this:
  317. const int HUGENUMBER = 0x7fffffL; // Work without any real limits.
  318. MySemaphore = CreateSemaphore(NULL, 0, HUGENUMBER, NULL); // Create a Semaphore for signalling.
  319. ThreadingCheck13(NULL != MySemaphore); // That should always work.
  320. }
  321. ProductionGateway::~ProductionGateway() { // Be sure to close it when we're done.
  322. CloseHandle(MySemaphore);
  323. }
  324. void ProductionGateway::produce() { // To produce() in WIN32 we
  325. ReleaseSemaphore(MySemaphore, 1, NULL); // release 1 count into the semaphore.
  326. }
  327. void ProductionGateway::consume() { // To consume() in WIN32 we
  328. WaitForSingleObject(MySemaphore, INFINITE); // wait for a count in the semaphore.
  329. }
  330. #else
  331. // POSIX Implementation ////////////////////////////////////////////////////////
  332. const RuntimeCheck ThreadingCheck14("ProductionGateway::ProductionGateway():ThreadingCheck14(0 == pthread_mutex_init(&MyMutex, NULL));");
  333. const RuntimeCheck ThreadingCheck15("ProductionGateway::ProductionGateway():ThreadingCheck15(0 == pthread_cond_init(&MyConditionVariable, NULL))");
  334. ProductionGateway::ProductionGateway() : // Construct in POSIX like this:
  335. Product(0), // All of our counts start at zero.
  336. Waiting(0),
  337. Signaled(0) {
  338. ThreadingCheck14(0 == pthread_mutex_init(&MyMutex, NULL)); // Initialize our mutex.
  339. ThreadingCheck15(0 == pthread_cond_init(&MyConditionVariable, NULL)); // Initialize our condition variable.
  340. }
  341. const ExitCheck ThreadingCheck16("ProductionGateway::~ProductionGateway():ThreadingCheck16(0 == pthread_mutex_destroy(&MyMutex))");
  342. const ExitCheck ThreadingCheck17("ProductionGateway::~ProductionGateway():ThreadingCheck17(0 == pthread_cond_destroy(&MyConditionVariable))");
  343. ProductionGateway::~ProductionGateway() { // When we're done we must destroy
  344. ThreadingCheck16(0 == pthread_mutex_destroy(&MyMutex)); // our local mutex and
  345. ThreadingCheck17(0 == pthread_cond_destroy(&MyConditionVariable)); // our condition variable.
  346. }
  347. const RuntimeCheck ThreadingCheck18("ProductionGateway::produce():ThreadingCheck18(0 == pthread_mutex_lock(&MyMutex))");
  348. const RuntimeCheck ThreadingCheck19("ProductionGateway::produce():ThreadingCheck19(0 == pthread_cond_signal(&MyConditionVariable))");
  349. const RuntimeCheck ThreadingCheck20("ProductionGateway::produce():ThreadingCheck20(0 == pthread_mutex_unlock(&MyMutex))");
  350. void ProductionGateway::produce() { // To produce in POSIX
  351. ThreadingCheck18(0 == pthread_mutex_lock(&MyMutex)); // Lock our mutex.
  352. ++Product; // Add an item to our product count.
  353. if(Signaled < Waiting) { // If anybody is waiting that has not
  354. ThreadingCheck19(0 == pthread_cond_signal(&MyConditionVariable)); // yet been signaled then signal them
  355. ++Signaled; // and keep track. They will count this
  356. } // down as they awaken.
  357. ThreadingCheck20(0 == pthread_mutex_unlock(&MyMutex)); // At the end unlock our mutex so
  358. } // waiting threads can fly free :-)
  359. const RuntimeCheck ThreadingCheck21("ProductionGateway::consume():ThreadingCheck21(0 == pthread_mutex_lock(&MyMutex))");
  360. const RuntimeCheck ThreadingCheck22("ProductionGateway::consume():ThreadingCheck22(0 == pthread_cond_wait(&MyConditionVariable, &MyMutex))");
  361. const RuntimeCheck ThreadingCheck23("ProductionGateway::consume():ThreadingCheck23(0 == pthread_mutex_unlock(&MyMutex))");
  362. void ProductionGateway::consume() { // To consume in POSIX
  363. ThreadingCheck21(0 == pthread_mutex_lock(&MyMutex)); // Lock our mutex.
  364. while(0 >= Product) { // Until we have something to consume,
  365. ++Waiting; // wait for a signal from
  366. ThreadingCheck22(0 == pthread_cond_wait(&MyConditionVariable, &MyMutex)); // our producer. When we have a signal
  367. --Waiting; // we are done waiting and we have
  368. --Signaled; // been signaled. Of course, somebody
  369. } // may have beaten us to it so check.
  370. --Product; // If we have product then take it.
  371. ThreadingCheck23(0 == pthread_mutex_unlock(&MyMutex)); // At the end unlock our mutex so
  372. }
  373. #endif
  374. // End Production Gateway
  375. ////////////////////////////////////////////////////////////////////////////////
  376. }