]> git.sesse.net Git - casparcg/blob - protocol/util/Thread.cpp
2.0.2: Is now locked, only bug fixes should be added. New features go into 2.1.0.
[casparcg] / protocol / util / Thread.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Nicklas P Andersson\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "thread.h"\r
25 #include <common/exception/win32_exception.h>\r
26 \r
27 namespace caspar {\r
28         \r
29 Event::Event(bool bManualReset, bool bInitialState) : handle_(0)\r
30 {\r
31         handle_ = CreateEvent(0, bManualReset, bInitialState, 0);\r
32         if(handle_ == 0) {\r
33                 throw std::exception("Failed to create event");\r
34         }\r
35 }\r
36 \r
37 void Event::Set() {\r
38         SetEvent(handle_);\r
39         // TODO: huh?\r
40         //BOOL res = SetEvent(handle_);\r
41         //if(res == FALSE) {\r
42         //      DWORD error = GetLastError();\r
43         //}\r
44 }\r
45 void Event::Reset() {\r
46         ResetEvent(handle_);\r
47 }\r
48 \r
49 Event::~Event()\r
50 {\r
51         CloseHandle(handle_);\r
52 }\r
53 \r
54 bool Thread::static_bInstallWin32ExceptionHandler_ = true;\r
55 \r
56 Thread::Thread() : pRunnable_(0), hThread_(0), stopEvent_(TRUE, FALSE), timeout_(10000)  {\r
57 }\r
58 \r
59 Thread::~Thread() {\r
60         Stop();\r
61 }\r
62 \r
63 bool Thread::IsRunning() {\r
64         if(hThread_ != 0) {\r
65                 if(WaitForSingleObject(hThread_, 0) == WAIT_OBJECT_0) {\r
66                         CloseHandle(hThread_);\r
67                         hThread_ = 0;\r
68                         pRunnable_ = 0;\r
69                 }\r
70         }\r
71 \r
72         return (hThread_ != 0);\r
73 }\r
74 \r
75 bool Thread::Start(IRunnable* pRunnable) {\r
76         if(hThread_ == 0) {\r
77                 if(pRunnable != 0) {\r
78                         pRunnable_ = pRunnable;\r
79                         stopEvent_.Reset();\r
80                         hThread_ = CreateThread(0, 0, ThreadEntrypoint, this, 0, 0);\r
81 \r
82                         if(hThread_ == 0)\r
83                                 pRunnable_ = 0;\r
84 \r
85                         return (hThread_ != 0);\r
86                 }\r
87         }\r
88         return false;\r
89 }\r
90 \r
91 bool Thread::Stop(bool bWait) {\r
92         bool returnValue = true;\r
93 \r
94         if(hThread_ != 0) {\r
95                 stopEvent_.Set();\r
96 \r
97                 if(bWait) {\r
98                         DWORD successCode = WaitForSingleObject(hThread_, timeout_);\r
99                         if(successCode != WAIT_OBJECT_0)\r
100                                 returnValue = false;\r
101                 }\r
102                 CloseHandle(hThread_);\r
103 \r
104                 hThread_ = 0;\r
105                 pRunnable_ = 0;\r
106         }\r
107 \r
108         return returnValue;\r
109 }\r
110 \r
111 void Thread::EnableWin32ExceptionHandler(bool bEnable) {\r
112         static_bInstallWin32ExceptionHandler_ = bEnable;\r
113 }\r
114 \r
115 DWORD WINAPI Thread::ThreadEntrypoint(LPVOID pParam) {\r
116         Thread* pThis = reinterpret_cast<Thread*>(pParam);\r
117         \r
118         if(Thread::static_bInstallWin32ExceptionHandler_)\r
119                 win32_exception::install_handler();\r
120 \r
121         _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);\r
122 \r
123         pThis->Run();\r
124 \r
125         return 0;\r
126 }\r
127 \r
128 void Thread::Run() {\r
129         bool bDoRestart = false;\r
130 \r
131         do {\r
132                 try {\r
133                         bDoRestart = false;\r
134                         stopEvent_.Reset();\r
135                         pRunnable_->Run(stopEvent_);\r
136                 }\r
137                 catch(const std::exception& e) {\r
138                         bDoRestart = pRunnable_->OnUnhandledException(e);\r
139                 }\r
140         }while(bDoRestart);\r
141 }\r
142 \r
143 }       //namespace caspar