Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/openh264/codec/common/src/WelsThreadPool.cpp
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.cpp
33
 *
34
 * \brief   functions for Thread Pool
35
 *
36
 * \date    5/09/2012 Created
37
 *
38
 *************************************************************************************
39
 */
40
#include "typedefs.h"
41
#include "memory_align.h"
42
#include "WelsThreadPool.h"
43
44
namespace WelsCommon {
45
46
namespace {
47
48
0
CWelsLock& GetInitLock() {
49
0
  static CWelsLock *initLock = new CWelsLock;
50
0
  return *initLock;
51
0
}
52
53
}
54
55
int32_t CWelsThreadPool::m_iRefCount = 0;
56
int32_t CWelsThreadPool::m_iMaxThreadNum = DEFAULT_THREAD_NUM;
57
CWelsThreadPool* CWelsThreadPool::m_pThreadPoolSelf = NULL;
58
59
CWelsThreadPool::CWelsThreadPool() :
60
0
  m_cWaitedTasks (NULL), m_cIdleThreads (NULL), m_cBusyThreads (NULL) {
61
0
}
62
63
64
0
CWelsThreadPool::~CWelsThreadPool() {
65
  //fprintf(stdout, "CWelsThreadPool::~CWelsThreadPool: delete %x, %x, %x\n", m_cWaitedTasks, m_cIdleThreads, m_cBusyThreads);
66
0
  if (0 != m_iRefCount) {
67
0
    m_iRefCount = 0;
68
0
    Uninit();
69
0
  }
70
0
}
71
72
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::SetThreadNum (int32_t iMaxThreadNum) {
73
0
  CWelsAutoLock  cLock (GetInitLock());
74
75
0
  if (m_iRefCount != 0) {
76
0
    return WELS_THREAD_ERROR_GENERAL;
77
0
  }
78
79
0
  if (iMaxThreadNum <= 0) {
80
0
    iMaxThreadNum = 1;
81
0
  }
82
0
  m_iMaxThreadNum = iMaxThreadNum;
83
0
  return WELS_THREAD_ERROR_OK;
84
0
}
85
86
87
0
CWelsThreadPool* CWelsThreadPool::AddReference() {
88
0
  CWelsAutoLock  cLock (GetInitLock());
89
0
  if (m_pThreadPoolSelf == NULL) {
90
0
    m_pThreadPoolSelf = new CWelsThreadPool();
91
0
    if (!m_pThreadPoolSelf) {
92
0
      return NULL;
93
0
    }
94
0
  }
95
96
0
  if (m_iRefCount == 0) {
97
0
    if (WELS_THREAD_ERROR_OK != m_pThreadPoolSelf->Init()) {
98
0
      m_pThreadPoolSelf->Uninit();
99
0
      delete m_pThreadPoolSelf;
100
0
      m_pThreadPoolSelf = NULL;
101
0
      return NULL;
102
0
    }
103
0
  }
104
105
  ////fprintf(stdout, "m_iRefCount=%d, iMaxThreadNum=%d\n", m_iRefCount, m_iMaxThreadNum);
106
107
0
  ++ m_iRefCount;
108
  //fprintf(stdout, "m_iRefCount2=%d\n", m_iRefCount);
109
0
  return m_pThreadPoolSelf;
110
0
}
111
112
0
void CWelsThreadPool::RemoveInstance() {
113
0
  CWelsAutoLock  cLock (GetInitLock());
114
  //fprintf(stdout, "m_iRefCount=%d\n", m_iRefCount);
115
0
  -- m_iRefCount;
116
0
  if (0 == m_iRefCount) {
117
0
    StopAllRunning();
118
0
    Uninit();
119
0
    if (m_pThreadPoolSelf) {
120
0
      delete m_pThreadPoolSelf;
121
0
      m_pThreadPoolSelf = NULL;
122
0
    }
123
    //fprintf(stdout, "m_iRefCount=%d, IdleThreadNum=%d, BusyThreadNum=%d, WaitedTask=%d\n", m_iRefCount, GetIdleThreadNum(), GetBusyThreadNum(), GetWaitedTaskNum());
124
0
  } else {
125
    // Synchronize with OnTaskStop(): if a worker thread just signaled the last task
126
    // of the caller being torn down, it still holds m_cLockBusyTasks until
127
    // OnTaskExecuted() returns. Acquiring the lock here ensures the worker has
128
    // exited OnTaskExecuted() before the caller destroys the task sink and tasks.
129
0
    CWelsAutoLock cBusyLock (m_cLockBusyTasks);
130
0
  }
131
0
}
132
133
134
0
bool CWelsThreadPool::IsReferenced() {
135
0
  CWelsAutoLock  cLock (GetInitLock());
136
0
  return (m_iRefCount > 0);
137
0
}
138
139
140
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStart (CWelsTaskThread* pThread, IWelsTask* pTask) {
141
0
  AddThreadToBusyList (pThread);
142
  //fprintf(stdout, "CWelsThreadPool::AddThreadToBusyList: Task %x at Thread %x\n", pTask, pThread);
143
0
  return WELS_THREAD_ERROR_OK;
144
0
}
145
146
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStop (CWelsTaskThread* pThread, IWelsTask* pTask) {
147
  //fprintf(stdout, "CWelsThreadPool::OnTaskStop 0: Task %x at Thread %x Finished\n", pTask, pThread);
148
149
  // Return pThread to the idle queue before removing it from m_cBusyThreads so
150
  // StopAllRunning() sees it in m_cIdleThreads as soon as GetBusyThreadNum() == 0.
151
  // The pool thread cannot re-dispatch pThread until CWelsTaskThread::ExecuteTask()
152
  // releases m_cLockTask after OnTaskStop() returns.
153
0
  AddThreadToIdleQueue (pThread);
154
155
0
  {
156
    // Hold the pool-owned m_cLockBusyTasks across OnTaskExecuted() and busy-list
157
    // removal so that RemoveInstance() / StopAllRunning() will wait until the
158
    // worker is completely done using pTask and its sink.
159
0
    CWelsAutoLock cLock (m_cLockBusyTasks);
160
0
    if (pTask && pTask->GetSink()) {
161
      //fprintf(stdout, "CWelsThreadPool::OnTaskStop 1: Task %x at Thread %x Finished, m_pSink=%x\n", pTask, pThread, pTask->GetSink());
162
0
      pTask->GetSink()->OnTaskExecuted();
163
      ////fprintf(stdout, "CWelsThreadPool::OnTaskStop 1: Task %x at Thread %x Finished, m_pSink=%x\n", pTask, pThread, pTask->GetSink());
164
0
    }
165
0
    m_cBusyThreads->erase (pThread);
166
0
  }
167
  //if (m_pSink) {
168
  //  m_pSink->OnTaskExecuted (pTask);
169
  //}
170
  //fprintf(stdout, "CWelsThreadPool::OnTaskStop 2: Task %x at Thread %x Finished\n", pTask, pThread);
171
172
0
  SignalThread();
173
174
  //fprintf(stdout, "ThreadPool: Task %x at Thread %x Finished\n", pTask, pThread);
175
0
  return WELS_THREAD_ERROR_OK;
176
0
}
177
178
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::Init() {
179
  //fprintf(stdout, "Enter WelsThreadPool Init\n");
180
181
0
  CWelsAutoLock  cLock (m_cLockPool);
182
183
0
  m_cWaitedTasks = new CWelsNonDuplicatedList<IWelsTask>();
184
0
  m_cIdleThreads = new CWelsNonDuplicatedList<CWelsTaskThread>();
185
0
  m_cBusyThreads = new CWelsList<CWelsTaskThread>();
186
0
  if (NULL == m_cWaitedTasks || NULL == m_cIdleThreads || NULL == m_cBusyThreads) {
187
0
    return WELS_THREAD_ERROR_GENERAL;
188
0
  }
189
190
0
  for (int32_t i = 0; i < m_iMaxThreadNum; i++) {
191
0
    if (WELS_THREAD_ERROR_OK != CreateIdleThread()) {
192
0
      return WELS_THREAD_ERROR_GENERAL;
193
0
    }
194
0
  }
195
196
0
  if (WELS_THREAD_ERROR_OK != Start()) {
197
0
    return WELS_THREAD_ERROR_GENERAL;
198
0
  }
199
200
0
  return WELS_THREAD_ERROR_OK;
201
0
}
202
203
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::StopAllRunning() {
204
0
  WELS_THREAD_ERROR_CODE iReturn = WELS_THREAD_ERROR_OK;
205
206
0
  ClearWaitedTasks();
207
208
0
  while (GetBusyThreadNum() > 0) {
209
    //WELS_INFO_TRACE ("CWelsThreadPool::Uninit - Waiting all thread to exit");
210
0
    WelsSleep (10);
211
0
  }
212
213
0
  if (GetIdleThreadNum() != m_iMaxThreadNum) {
214
0
    iReturn = WELS_THREAD_ERROR_GENERAL;
215
0
  }
216
217
0
  return iReturn;
218
0
}
219
220
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::Uninit() {
221
0
  WELS_THREAD_ERROR_CODE iReturn = WELS_THREAD_ERROR_OK;
222
0
  CWelsAutoLock  cLock (m_cLockPool);
223
224
0
  iReturn = StopAllRunning();
225
0
  assert (0 == GetBusyThreadNum());
226
227
0
  m_cLockIdleTasks.Lock();
228
0
  while (m_cIdleThreads && m_cIdleThreads->size() > 0) {
229
0
    DestroyThread (m_cIdleThreads->begin());
230
0
    m_cIdleThreads->pop_front();
231
0
  }
232
0
  m_cLockIdleTasks.Unlock();
233
234
0
  Kill();
235
236
0
  WELS_DELETE_OP (m_cWaitedTasks);
237
0
  WELS_DELETE_OP (m_cIdleThreads);
238
0
  WELS_DELETE_OP (m_cBusyThreads);
239
240
0
  return iReturn;
241
0
}
242
243
0
void CWelsThreadPool::ExecuteTask() {
244
  //fprintf(stdout, "ThreadPool: scheduled tasks: ExecuteTask\n");
245
0
  CWelsTaskThread* pThread = NULL;
246
0
  IWelsTask*    pTask = NULL;
247
0
  while (GetWaitedTaskNum() > 0) {
248
    //fprintf(stdout, "ThreadPool:  ExecuteTask: waiting task %d\n", GetWaitedTaskNum());
249
0
    pThread = GetIdleThread();
250
0
    if (pThread == NULL) {
251
      //fprintf(stdout, "ThreadPool:  ExecuteTask: no IdleThread\n");
252
253
0
      break;
254
0
    }
255
0
    pTask = GetWaitedTask();
256
    //fprintf(stdout, "ThreadPool:  ExecuteTask = %x at thread %x\n", pTask, pThread);
257
0
    if (pTask) {
258
0
      pThread->SetTask (pTask);
259
0
    } else {
260
0
      AddThreadToIdleQueue (pThread);
261
0
    }
262
0
  }
263
0
}
264
265
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::QueueTask (IWelsTask* pTask) {
266
0
  CWelsAutoLock  cLock (m_cLockPool);
267
268
  //fprintf(stdout, "CWelsThreadPool::QueueTask: %d, pTask=%x\n", m_iRefCount, pTask);
269
0
  if (GetWaitedTaskNum() == 0) {
270
0
    CWelsTaskThread* pThread = GetIdleThread();
271
272
0
    if (pThread != NULL) {
273
      //fprintf(stdout, "ThreadPool:  ExecuteTask = %x at thread %x\n", pTask, pThread);
274
0
      pThread->SetTask (pTask);
275
276
0
      return WELS_THREAD_ERROR_OK;
277
0
    }
278
0
  }
279
  //fprintf(stdout, "ThreadPool:  AddTaskToWaitedList: %x\n", pTask);
280
0
  if (false == AddTaskToWaitedList (pTask)) {
281
0
    return WELS_THREAD_ERROR_GENERAL;
282
0
  }
283
284
  //fprintf(stdout, "ThreadPool:  SignalThread: %x\n", pTask);
285
0
  SignalThread();
286
0
  return WELS_THREAD_ERROR_OK;
287
0
}
288
289
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::CreateIdleThread() {
290
0
  CWelsTaskThread* pThread = new CWelsTaskThread (this);
291
292
0
  if (NULL == pThread) {
293
0
    return WELS_THREAD_ERROR_GENERAL;
294
0
  }
295
296
0
  if (WELS_THREAD_ERROR_OK != pThread->Start()) {
297
0
    WELS_DELETE_OP (pThread);
298
0
    return WELS_THREAD_ERROR_GENERAL;
299
0
  }
300
  //fprintf(stdout, "ThreadPool:  AddThreadToIdleQueue: %x\n", pThread);
301
0
  AddThreadToIdleQueue (pThread);
302
303
0
  return WELS_THREAD_ERROR_OK;
304
0
}
305
306
0
void  CWelsThreadPool::DestroyThread (CWelsTaskThread* pThread) {
307
0
  pThread->Kill();
308
0
  WELS_DELETE_OP (pThread);
309
310
0
  return;
311
0
}
312
313
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::AddThreadToIdleQueue (CWelsTaskThread* pThread) {
314
0
  CWelsAutoLock cLock (m_cLockIdleTasks);
315
0
  m_cIdleThreads->push_back (pThread);
316
0
  return WELS_THREAD_ERROR_OK;
317
0
}
318
319
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::AddThreadToBusyList (CWelsTaskThread* pThread) {
320
0
  CWelsAutoLock cLock (m_cLockBusyTasks);
321
0
  m_cBusyThreads->push_back (pThread);
322
0
  return WELS_THREAD_ERROR_OK;
323
0
}
324
325
0
bool  CWelsThreadPool::AddTaskToWaitedList (IWelsTask* pTask) {
326
0
  CWelsAutoLock  cLock (m_cLockWaitedTasks);
327
328
0
  return m_cWaitedTasks->push_back (pTask);
329
0
}
330
331
0
CWelsTaskThread*   CWelsThreadPool::GetIdleThread() {
332
0
  CWelsAutoLock cLock (m_cLockIdleTasks);
333
334
0
  if (NULL == m_cIdleThreads || m_cIdleThreads->size() == 0) {
335
0
    return NULL;
336
0
  }
337
338
  //fprintf(stdout, "CWelsThreadPool::GetIdleThread=%d\n", m_cIdleThreads->size());
339
340
0
  CWelsTaskThread* pThread = m_cIdleThreads->begin();
341
0
  m_cIdleThreads->pop_front();
342
0
  return pThread;
343
0
}
344
345
0
int32_t  CWelsThreadPool::GetBusyThreadNum() {
346
0
  CWelsAutoLock cLock (m_cLockBusyTasks);
347
0
  return (m_cBusyThreads?m_cBusyThreads->size():0);
348
0
}
349
350
0
int32_t  CWelsThreadPool::GetIdleThreadNum() {
351
0
  return (m_cIdleThreads?m_cIdleThreads->size():0);
352
0
}
353
354
0
int32_t  CWelsThreadPool::GetWaitedTaskNum() {
355
0
  return (m_cWaitedTasks?m_cWaitedTasks->size():0);
356
0
}
357
358
0
IWelsTask* CWelsThreadPool::GetWaitedTask() {
359
0
  CWelsAutoLock lock (m_cLockWaitedTasks);
360
361
0
  if (NULL==m_cWaitedTasks || m_cWaitedTasks->size() == 0) {
362
0
    return NULL;
363
0
  }
364
365
0
  IWelsTask* pTask = m_cWaitedTasks->begin();
366
367
0
  m_cWaitedTasks->pop_front();
368
369
0
  return pTask;
370
0
}
371
372
0
void  CWelsThreadPool::ClearWaitedTasks() {
373
0
  CWelsAutoLock cLock (m_cLockWaitedTasks);
374
0
  if (NULL == m_cWaitedTasks) {
375
0
    return;
376
0
  }
377
0
  IWelsTask* pTask = NULL;
378
0
  while (0 != m_cWaitedTasks->size()) {
379
0
    pTask = m_cWaitedTasks->begin();
380
0
    if (pTask->GetSink()) {
381
0
      CWelsAutoLock cBusyLock (m_cLockBusyTasks);
382
0
      pTask->GetSink()->OnTaskCancelled();
383
0
    }
384
0
    m_cWaitedTasks->pop_front();
385
0
  }
386
0
}
387
388
}