/work/openh264/codec/common/src/WelsThread.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 | | |
41 | | #include "WelsThread.h" |
42 | | |
43 | | namespace WelsCommon { |
44 | | |
45 | | CWelsThread::CWelsThread() : |
46 | 0 | m_hThread (0), |
47 | 0 | m_bRunning (false), |
48 | 0 | m_bEndFlag (false) { |
49 | |
|
50 | 0 | WelsEventOpen (&m_hEvent); |
51 | 0 | WelsMutexInit(&m_hMutex); |
52 | 0 | m_iConVar = 1; |
53 | 0 | } |
54 | | |
55 | 0 | CWelsThread::~CWelsThread() { |
56 | 0 | Kill(); |
57 | 0 | WelsEventClose (&m_hEvent); |
58 | 0 | WelsMutexDestroy(&m_hMutex); |
59 | 0 | } |
60 | | |
61 | 0 | void CWelsThread::Thread() { |
62 | 0 | while (true) { |
63 | 0 | WelsEventWait (&m_hEvent,&m_hMutex,m_iConVar); |
64 | |
|
65 | 0 | if (GetEndFlag()) { |
66 | 0 | break; |
67 | 0 | } |
68 | | |
69 | 0 | m_iConVar = 1; |
70 | 0 | 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 | 0 | } |
72 | |
|
73 | 0 | SetRunning (false); |
74 | 0 | } |
75 | | |
76 | 0 | WELS_THREAD_ERROR_CODE CWelsThread::Start() { |
77 | 0 | #ifndef __APPLE__ |
78 | 0 | if (NULL == m_hEvent) { |
79 | 0 | return WELS_THREAD_ERROR_GENERAL; |
80 | 0 | } |
81 | 0 | #endif |
82 | 0 | if (GetRunning()) { |
83 | 0 | return WELS_THREAD_ERROR_OK; |
84 | 0 | } |
85 | | |
86 | 0 | SetEndFlag (false); |
87 | |
|
88 | 0 | WELS_THREAD_ERROR_CODE rc = WelsThreadCreate (&m_hThread, |
89 | 0 | (LPWELS_THREAD_ROUTINE)TheThread, this, 0); |
90 | |
|
91 | 0 | if (WELS_THREAD_ERROR_OK != rc) { |
92 | 0 | return rc; |
93 | 0 | } |
94 | | |
95 | 0 | while (!GetRunning()) { |
96 | 0 | WelsSleep (1); |
97 | 0 | } |
98 | |
|
99 | 0 | return WELS_THREAD_ERROR_OK; |
100 | 0 | } |
101 | | |
102 | 0 | void CWelsThread::Kill() { |
103 | 0 | if (!GetRunning()) { |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | 0 | SetEndFlag (true); |
108 | |
|
109 | 0 | WelsEventSignal (&m_hEvent,&m_hMutex,&m_iConVar); |
110 | 0 | WelsThreadJoin (m_hThread); |
111 | 0 | return; |
112 | 0 | } |
113 | | |
114 | 0 | WELS_THREAD_ROUTINE_TYPE CWelsThread::TheThread (void* pParam) { |
115 | 0 | CWelsThread* pThis = static_cast<CWelsThread*> (pParam); |
116 | |
|
117 | 0 | pThis->SetRunning (true); |
118 | |
|
119 | 0 | pThis->Thread(); |
120 | |
|
121 | 0 | WELS_THREAD_ROUTINE_RETURN (NULL); |
122 | 0 | } |
123 | | |
124 | | } |
125 | | |
126 | | |