]> git.sesse.net Git - casparcg/blob - protocol/util/Thread.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / protocol / util / Thread.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20  \r
21 #include "../StdAfx.h"\r
22 \r
23 #include "thread.h"\r
24 #include <common/exception/win32_exception.h>\r
25 \r
26 namespace caspar {\r
27         \r
28 Event::Event(bool bManualReset, bool bInitialState) : handle_(0)\r
29 {\r
30         handle_ = CreateEvent(0, bManualReset, bInitialState, 0);\r
31         if(handle_ == 0) {\r
32                 throw std::exception("Failed to create event");\r
33         }\r
34 }\r
35 \r
36 void Event::Set() {\r
37         SetEvent(handle_);\r
38         // TODO: huh?\r
39         //BOOL res = SetEvent(handle_);\r
40         //if(res == FALSE) {\r
41         //      DWORD error = GetLastError();\r
42         //}\r
43 }\r
44 void Event::Reset() {\r
45         ResetEvent(handle_);\r
46 }\r
47 \r
48 Event::~Event()\r
49 {\r
50         CloseHandle(handle_);\r
51 }\r
52 \r
53 bool Thread::static_bInstallWin32ExceptionHandler_ = true;\r
54 \r
55 Thread::Thread() : pRunnable_(0), hThread_(0), stopEvent_(TRUE, FALSE), timeout_(10000)  {\r
56 }\r
57 \r
58 Thread::~Thread() {\r
59         Stop();\r
60 }\r
61 \r
62 bool Thread::IsRunning() {\r
63         if(hThread_ != 0) {\r
64                 if(WaitForSingleObject(hThread_, 0) == WAIT_OBJECT_0) {\r
65                         CloseHandle(hThread_);\r
66                         hThread_ = 0;\r
67                         pRunnable_ = 0;\r
68                 }\r
69         }\r
70 \r
71         return (hThread_ != 0);\r
72 }\r
73 \r
74 bool Thread::Start(IRunnable* pRunnable) {\r
75         if(hThread_ == 0) {\r
76                 if(pRunnable != 0) {\r
77                         pRunnable_ = pRunnable;\r
78                         stopEvent_.Reset();\r
79                         hThread_ = CreateThread(0, 0, ThreadEntrypoint, this, 0, 0);\r
80 \r
81                         if(hThread_ == 0)\r
82                                 pRunnable_ = 0;\r
83 \r
84                         return (hThread_ != 0);\r
85                 }\r
86         }\r
87         return false;\r
88 }\r
89 \r
90 bool Thread::Stop(bool bWait) {\r
91         bool returnValue = true;\r
92 \r
93         if(hThread_ != 0) {\r
94                 stopEvent_.Set();\r
95 \r
96                 if(bWait) {\r
97                         DWORD successCode = WaitForSingleObject(hThread_, timeout_);\r
98                         if(successCode != WAIT_OBJECT_0)\r
99                                 returnValue = false;\r
100                 }\r
101                 CloseHandle(hThread_);\r
102 \r
103                 hThread_ = 0;\r
104                 pRunnable_ = 0;\r
105         }\r
106 \r
107         return returnValue;\r
108 }\r
109 \r
110 void Thread::EnableWin32ExceptionHandler(bool bEnable) {\r
111         static_bInstallWin32ExceptionHandler_ = bEnable;\r
112 }\r
113 \r
114 DWORD WINAPI Thread::ThreadEntrypoint(LPVOID pParam) {\r
115         Thread* pThis = reinterpret_cast<Thread*>(pParam);\r
116         \r
117         if(Thread::static_bInstallWin32ExceptionHandler_)\r
118                 win32_exception::install_handler();\r
119 \r
120         _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);\r
121 \r
122         pThis->Run();\r
123 \r
124         return 0;\r
125 }\r
126 \r
127 void Thread::Run() {\r
128         bool bDoRestart = false;\r
129 \r
130         do {\r
131                 try {\r
132                         bDoRestart = false;\r
133                         stopEvent_.Reset();\r
134                         pRunnable_->Run(stopEvent_);\r
135                 }\r
136                 catch(const std::exception& e) {\r
137                         bDoRestart = pRunnable_->OnUnhandledException(e);\r
138                 }\r
139         }while(bDoRestart);\r
140 }\r
141 \r
142 }       //namespace caspar