/work/openh264/codec/common/inc/WelsThreadPool.h
Line | Count | Source |
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 | | // After RemoveInstance() returns, the pool will not call into any sink or |
63 | | // touch any task that was queued by the caller. Sinks must therefore not |
64 | | // call AddReference()/RemoveInstance()/IsReferenced() from within callbacks. |
65 | | void RemoveInstance(); |
66 | | |
67 | | static bool IsReferenced(); |
68 | | |
69 | | //IWelsTaskThreadSink |
70 | | virtual WELS_THREAD_ERROR_CODE OnTaskStart (CWelsTaskThread* pThread, IWelsTask* pTask); |
71 | | virtual WELS_THREAD_ERROR_CODE OnTaskStop (CWelsTaskThread* pThread, IWelsTask* pTask); |
72 | | |
73 | | // CWelsThread |
74 | | virtual void ExecuteTask(); |
75 | | |
76 | | WELS_THREAD_ERROR_CODE QueueTask (IWelsTask* pTask); |
77 | 0 | int32_t GetThreadNum() const { |
78 | 0 | return m_iMaxThreadNum; |
79 | 0 | } |
80 | | |
81 | | |
82 | | protected: |
83 | | WELS_THREAD_ERROR_CODE Init(); |
84 | | WELS_THREAD_ERROR_CODE Uninit(); |
85 | | |
86 | | WELS_THREAD_ERROR_CODE CreateIdleThread(); |
87 | | void DestroyThread (CWelsTaskThread* pThread); |
88 | | WELS_THREAD_ERROR_CODE AddThreadToIdleQueue (CWelsTaskThread* pThread); |
89 | | WELS_THREAD_ERROR_CODE AddThreadToBusyList (CWelsTaskThread* pThread); |
90 | | bool AddTaskToWaitedList (IWelsTask* pTask); |
91 | | CWelsTaskThread* GetIdleThread(); |
92 | | IWelsTask* GetWaitedTask(); |
93 | | int32_t GetIdleThreadNum(); |
94 | | int32_t GetBusyThreadNum(); |
95 | | int32_t GetWaitedTaskNum(); |
96 | | void ClearWaitedTasks(); |
97 | | |
98 | | private: |
99 | | CWelsThreadPool(); |
100 | | virtual ~CWelsThreadPool(); |
101 | | |
102 | | WELS_THREAD_ERROR_CODE StopAllRunning(); |
103 | | |
104 | | static int32_t m_iRefCount; |
105 | | static int32_t m_iMaxThreadNum; |
106 | | static CWelsThreadPool* m_pThreadPoolSelf; |
107 | | |
108 | | CWelsNonDuplicatedList<IWelsTask>* m_cWaitedTasks; |
109 | | CWelsNonDuplicatedList<CWelsTaskThread>* m_cIdleThreads; |
110 | | CWelsList<CWelsTaskThread>* m_cBusyThreads; |
111 | | |
112 | | CWelsLock m_cLockPool; |
113 | | CWelsLock m_cLockWaitedTasks; |
114 | | CWelsLock m_cLockIdleTasks; |
115 | | CWelsLock m_cLockBusyTasks; // guards m_cBusyThreads and serializes sink callbacks |
116 | | |
117 | | DISALLOW_COPY_AND_ASSIGN (CWelsThreadPool); |
118 | | }; |
119 | | |
120 | | } |
121 | | |
122 | | |
123 | | #endif |
124 | | |
125 | | |
126 | | |