/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 | WelsMutexLock (&m_hMutex); |
70 | 0 | m_iConVar = 1; |
71 | 0 | WelsMutexUnlock (&m_hMutex); |
72 | 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() |
73 | 0 | } |
74 | |
|
75 | 0 | SetRunning (false); |
76 | 0 | } |
77 | | |
78 | 0 | WELS_THREAD_ERROR_CODE CWelsThread::Start() { |
79 | 0 | #ifndef __APPLE__ |
80 | 0 | if (NULL == m_hEvent) { |
81 | 0 | return WELS_THREAD_ERROR_GENERAL; |
82 | 0 | } |
83 | 0 | #endif |
84 | 0 | if (GetRunning()) { |
85 | 0 | return WELS_THREAD_ERROR_OK; |
86 | 0 | } |
87 | | |
88 | 0 | SetEndFlag (false); |
89 | |
|
90 | 0 | WELS_THREAD_ERROR_CODE rc = WelsThreadCreate (&m_hThread, |
91 | 0 | (LPWELS_THREAD_ROUTINE)TheThread, this, 0); |
92 | |
|
93 | 0 | if (WELS_THREAD_ERROR_OK != rc) { |
94 | 0 | return rc; |
95 | 0 | } |
96 | | |
97 | 0 | while (!GetRunning()) { |
98 | 0 | WelsSleep (1); |
99 | 0 | } |
100 | |
|
101 | 0 | return WELS_THREAD_ERROR_OK; |
102 | 0 | } |
103 | | |
104 | 0 | void CWelsThread::Kill() { |
105 | 0 | if (!GetRunning()) { |
106 | 0 | return; |
107 | 0 | } |
108 | | |
109 | 0 | SetEndFlag (true); |
110 | |
|
111 | 0 | WelsEventSignal (&m_hEvent,&m_hMutex,&m_iConVar); |
112 | 0 | WelsThreadJoin (m_hThread); |
113 | 0 | return; |
114 | 0 | } |
115 | | |
116 | 0 | WELS_THREAD_ROUTINE_TYPE CWelsThread::TheThread (void* pParam) { |
117 | 0 | CWelsThread* pThis = static_cast<CWelsThread*> (pParam); |
118 | |
|
119 | 0 | pThis->SetRunning (true); |
120 | |
|
121 | 0 | pThis->Thread(); |
122 | |
|
123 | 0 | WELS_THREAD_ROUTINE_RETURN (NULL); |
124 | 0 | } |
125 | | |
126 | | } |
127 | | |
128 | | |