• 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.h
33  *
34  * \brief   Interfaces introduced in thread pool
35  *
36  * \date    5/09/2012 Created
37  *
38  *************************************************************************************
39  */
40 
41 
42 #ifndef _WELS_THREAD_POOL_H_
43 #define _WELS_THREAD_POOL_H_
44 
45 #include <stdio.h>
46 #include "WelsTask.h"
47 #include "WelsTaskThread.h"
48 #include "WelsList.h"
49 
50 namespace WelsCommon {
51 
52 
53 class  CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink {
54  public:
55   enum {
56     DEFAULT_THREAD_NUM = 4,
57   };
58 
59   static WELS_THREAD_ERROR_CODE SetThreadNum (int32_t iMaxThreadNum);
60 
61   static CWelsThreadPool* AddReference();
62   void RemoveInstance();
63 
64   static bool IsReferenced();
65 
66   //IWelsTaskThreadSink
67   virtual WELS_THREAD_ERROR_CODE OnTaskStart (CWelsTaskThread* pThread,  IWelsTask* pTask);
68   virtual WELS_THREAD_ERROR_CODE OnTaskStop (CWelsTaskThread* pThread,  IWelsTask* pTask);
69 
70   //  CWelsThread
71   virtual void ExecuteTask();
72 
73   WELS_THREAD_ERROR_CODE  QueueTask (IWelsTask* pTask);
GetThreadNum()74   int32_t        GetThreadNum() const {
75     return m_iMaxThreadNum;
76   }
77 
78 
79  protected:
80   WELS_THREAD_ERROR_CODE Init();
81   WELS_THREAD_ERROR_CODE Uninit();
82 
83   WELS_THREAD_ERROR_CODE CreateIdleThread();
84   void           DestroyThread (CWelsTaskThread* pThread);
85   WELS_THREAD_ERROR_CODE AddThreadToIdleQueue (CWelsTaskThread* pThread);
86   WELS_THREAD_ERROR_CODE AddThreadToBusyList (CWelsTaskThread* pThread);
87   WELS_THREAD_ERROR_CODE RemoveThreadFromBusyList (CWelsTaskThread* pThread);
88   bool           AddTaskToWaitedList (IWelsTask* pTask);
89   CWelsTaskThread*   GetIdleThread();
90   IWelsTask*         GetWaitedTask();
91   int32_t            GetIdleThreadNum();
92   int32_t            GetBusyThreadNum();
93   int32_t            GetWaitedTaskNum();
94   void               ClearWaitedTasks();
95 
96  private:
97   CWelsThreadPool();
98   virtual ~CWelsThreadPool();
99 
100   WELS_THREAD_ERROR_CODE StopAllRunning();
101 
102   static int32_t   m_iRefCount;
103   static int32_t   m_iMaxThreadNum;
104   static CWelsThreadPool* m_pThreadPoolSelf;
105 
106   CWelsNonDuplicatedList<IWelsTask>* m_cWaitedTasks;
107   CWelsNonDuplicatedList<CWelsTaskThread>* m_cIdleThreads;
108   CWelsList<CWelsTaskThread>* m_cBusyThreads;
109 
110   CWelsLock   m_cLockPool;
111   CWelsLock   m_cLockWaitedTasks;
112   CWelsLock   m_cLockIdleTasks;
113   CWelsLock   m_cLockBusyTasks;
114 
115   DISALLOW_COPY_AND_ASSIGN (CWelsThreadPool);
116 };
117 
118 }
119 
120 
121 #endif
122 
123 
124 
125