1 ////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // Nestopia - NES/Famicom emulator written in C++
4 //
5 // Copyright (C) 2003-2008 Martin Freij
6 //
7 // This file is part of Nestopia.
8 //
9 // Nestopia is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Nestopia is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Nestopia; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 //
23 ////////////////////////////////////////////////////////////////////////////////////////
24 
25 #include <process.h>
26 #include "language/resource.h"
27 #include "NstApplicationException.hpp"
28 #include "NstSystemThread.hpp"
29 
30 namespace Nestopia
31 {
32 	namespace System
33 	{
Thread()34 		Thread::Thread()
35 		: hEnter(NULL), hExit(NULL), hAbort(NULL) {}
36 
~Thread()37 		Thread::~Thread()
38 		{
39 			Stop();
40 		}
41 
Terminator(HANDLE handle)42 		inline Thread::Terminator::Terminator(HANDLE handle)
43 		: hAbort(handle) {}
44 
Start(const Callback & callback,const int priority)45 		void Thread::Start(const Callback& callback,const int priority)
46 		{
47 			Stop();
48 
49 			if
50 			(
51 				NULL == (hEnter = ::CreateEvent( NULL, false, false, NULL )) ||
52 				NULL == (hExit  = ::CreateEvent( NULL, false, false, NULL )) ||
53 				NULL == (hAbort = ::CreateEvent( NULL, false, false, NULL ))
54 			)
55 				throw Application::Exception( IDS_ERR_FAILED, L"CreateEvent()" );
56 
57 			class Entry
58 			{
59 				const Callback callback;
60 				const int priority;
61 				HANDLE const hEnter;
62 				HANDLE const hExit;
63 				const Terminator terminator;
64 
65 			public:
66 
67 				Entry(const Callback& c,int p,HANDLE e,HANDLE x,Terminator t)
68 				: callback(c), priority(p), hEnter(e), hExit(x), terminator(t) {}
69 
70 				static void WINAPIV Run(void* data)
71 				{
72 					const Entry local( *static_cast<Entry*>(data) );
73 
74 					NST_ASSERT( local.hEnter && local.hExit );
75 
76 					if (local.priority)
77 						::SetThreadPriority( ::GetCurrentThread(), local.priority );
78 
79 					::SetEvent( local.hEnter );
80 
81 					try
82 					{
83 						local.callback( local.terminator );
84 					}
85 					catch (...)
86 					{
87 					}
88 
89 					::SetEvent( local.hExit );
90 				}
91 			};
92 
93 			Entry entry( callback, priority, hEnter, hExit, Terminator(hAbort) );
94 
95 			if (!::_beginthread( Entry::Run, 0, &entry ))
96 				throw Application::Exception( IDS_ERR_FAILED, L"_beginthread()" );
97 
98 			::WaitForSingleObject( hEnter, INFINITE );
99 		}
100 
Stop()101 		void Thread::Stop()
102 		{
103 			if (hAbort)
104 				::SetEvent( hAbort );
105 
106 			if (HANDLE const handle = hExit)
107 			{
108 				hExit = NULL;
109 				::WaitForSingleObject( handle, INFINITE );
110 				::CloseHandle( handle );
111 			}
112 
113 			if (HANDLE const handle = hAbort)
114 			{
115 				hAbort = NULL;
116 				::CloseHandle( handle );
117 			}
118 
119 			if (HANDLE const handle = hEnter)
120 			{
121 				hEnter = NULL;
122 				::CloseHandle( handle );
123 			}
124 		}
125 	}
126 }
127