• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CWelsThread()45 CWelsThread::CWelsThread() :
46   m_hThread (0),
47   m_bRunning (false),
48   m_bEndFlag (false) {
49 
50   WelsEventOpen (&m_hEvent);
51   WelsMutexInit(&m_hMutex);
52   m_iConVar = 1;
53 }
54 
~CWelsThread()55 CWelsThread::~CWelsThread() {
56   Kill();
57   WelsEventClose (&m_hEvent);
58   WelsMutexDestroy(&m_hMutex);
59 }
60 
Thread()61 void CWelsThread::Thread() {
62   while (true) {
63     WelsEventWait (&m_hEvent,&m_hMutex,m_iConVar);
64 
65     if (GetEndFlag()) {
66       break;
67     }
68 
69     m_iConVar = 1;
70     ExecuteTask();//in ExecuteTask there will be OnTaskStop which opens the potential new Signaling of next run, so the setting of m_iConVar = 1 should be before ExecuteTask()
71   }
72 
73   SetRunning (false);
74 }
75 
Start()76 WELS_THREAD_ERROR_CODE CWelsThread::Start() {
77 #ifndef __APPLE__
78   if (NULL == m_hEvent) {
79     return WELS_THREAD_ERROR_GENERAL;
80   }
81 #endif
82   if (GetRunning()) {
83     return WELS_THREAD_ERROR_OK;
84   }
85 
86   SetEndFlag (false);
87 
88   WELS_THREAD_ERROR_CODE rc = WelsThreadCreate (&m_hThread,
89                               (LPWELS_THREAD_ROUTINE)TheThread, this, 0);
90 
91   if (WELS_THREAD_ERROR_OK != rc) {
92     return rc;
93   }
94 
95   while (!GetRunning()) {
96     WelsSleep (1);
97   }
98 
99   return WELS_THREAD_ERROR_OK;
100 }
101 
Kill()102 void CWelsThread::Kill() {
103   if (!GetRunning()) {
104     return;
105   }
106 
107   SetEndFlag (true);
108 
109   WelsEventSignal (&m_hEvent,&m_hMutex,&m_iConVar);
110   WelsThreadJoin (m_hThread);
111   return;
112 }
113 
TheThread(void * pParam)114 WELS_THREAD_ROUTINE_TYPE  CWelsThread::TheThread (void* pParam) {
115   CWelsThread* pThis = static_cast<CWelsThread*> (pParam);
116 
117   pThis->SetRunning (true);
118 
119   pThis->Thread();
120 
121   WELS_THREAD_ROUTINE_RETURN (NULL);
122 }
123 
124 }
125 
126 
127