Showing the changes to codec/common/src/WelsThread.cpp (1 of 14 files). Show all
Unified · Split
| @@ -0,0 +1,125 @@ | ||
| 1 | +/*! | |
| 2 | + * \copy | |
| 3 | + * Copyright (c) 2009-2015, Cisco Systems | |
| 4 | + * All rights reserved. | |
| 5 | + * | |
| 6 | + * Redistribution and use in source and binary forms, with or without | |
| 7 | + * modification, are permitted provided that the following conditions | |
| 8 | + * are met: | |
| 9 | + * | |
| 10 | + * * Redistributions of source code must retain the above copyright | |
| 11 | + * notice, this list of conditions and the following disclaimer. | |
| 12 | + * | |
| 13 | + * * Redistributions in binary form must reproduce the above copyright | |
| 14 | + * notice, this list of conditions and the following disclaimer in | |
| 15 | + * the documentation and/or other materials provided with the | |
| 16 | + * distribution. | |
| 17 | + * | |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 21 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 22 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 23 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 24 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 27 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| 28 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. | |
| 30 | + * | |
| 31 | + * | |
| 32 | + * \file WelsThreadPool.cpp | |
| 33 | + * | |
| 34 | + * \brief functions for Thread Pool | |
| 35 | + * | |
| 36 | + * \date 5/09/2012 Created | |
| 37 | + * | |
| 38 | + ************************************************************************************* | |
| 39 | + */ | |
| 40 | + | |
| 41 | +#include "WelsThread.h" | |
| 42 | + | |
| 43 | +namespace WelsCommon { | |
| 44 | + | |
| 45 | +CWelsThread::CWelsThread() : | |
| 46 | + m_hThread (0), | |
| 47 | + m_bRunning (false), | |
| 48 | + m_bEndFlag (false) { | |
| 49 | + WELS_THREAD_ERROR_CODE rc = WelsEventOpen (&m_hEvent); | |
| 50 | + | |
| 51 | + if (WELS_THREAD_ERROR_OK != rc) { | |
| 52 | + m_hEvent = NULL; | |
| 53 | + } | |
| 54 | +} | |
| 55 | + | |
| 56 | +CWelsThread::~CWelsThread() { | |
| 57 | + Kill(); | |
| 58 | + WelsEventClose (&m_hEvent); | |
| 59 | + m_hEvent = NULL; | |
| 60 | +} | |
| 61 | + | |
| 62 | +void CWelsThread::Thread() { | |
| 63 | + while (true) { | |
| 64 | + WelsEventWait (&m_hEvent); | |
| 65 | + | |
| 66 | + if (GetEndFlag()) { | |
| 67 | + break; | |
| 68 | + } | |
| 69 | + | |
| 70 | + ExecuteTask(); | |
| 71 | + } | |
| 72 | + | |
| 73 | + SetRunning (false); | |
| 74 | +} | |
| 75 | + | |
| 76 | +WELS_THREAD_ERROR_CODE CWelsThread::Start() { | |
| 77 | + if (NULL == m_hEvent) { | |
| 78 | + return WELS_THREAD_ERROR_GENERAL; | |
| 79 | + } | |
| 80 | + | |
| 81 | + if (GetRunning()) { | |
| 82 | + return WELS_THREAD_ERROR_OK; | |
| 83 | + } | |
| 84 | + | |
| 85 | + SetEndFlag (false); | |
| 86 | + | |
| 87 | + WELS_THREAD_ERROR_CODE rc = WelsThreadCreate (&m_hThread, | |
| 88 | + (LPWELS_THREAD_ROUTINE)TheThread, this, 0); | |
| 89 | + | |
| 90 | + if (WELS_THREAD_ERROR_OK != rc) { | |
| 91 | + return rc; | |
| 92 | + } | |
| 93 | + | |
| 94 | + while (!GetRunning()) { | |
| 95 | + WelsSleep (1); | |
| 96 | + } | |
| 97 | + | |
| 98 | + return WELS_THREAD_ERROR_OK; | |
| 99 | +} | |
| 100 | + | |
| 101 | +void CWelsThread::Kill() { | |
| 102 | + if (!GetRunning()) { | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + | |
| 106 | + SetEndFlag (true); | |
| 107 | + | |
| 108 | + WelsEventSignal (&m_hEvent); | |
| 109 | + WelsThreadJoin (m_hThread); | |
| 110 | + return; | |
| 111 | +} | |
| 112 | + | |
| 113 | +WELS_THREAD_ROUTINE_TYPE CWelsThread::TheThread (void* pParam) { | |
| 114 | + CWelsThread* pThis = static_cast<CWelsThread*> (pParam); | |
| 115 | + | |
| 116 | + pThis->SetRunning (true); | |
| 117 | + | |
| 118 | + pThis->Thread(); | |
| 119 | + | |
| 120 | + WELS_THREAD_ROUTINE_RETURN (NULL); | |
| 121 | +} | |
| 122 | + | |
| 123 | +} | |
| 124 | + | |
| 125 | + | |