1 /*
2 DSFML - The Simple and Fast Multimedia Library for D
3 
4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5 
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial applications,
10 and to alter it and redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 
15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
16 
17 3. This notice may not be removed or altered from any source distribution
18 */
19 
20 ///A module containing the DSFML Thread class
21 module dsfml.system.thread;
22 
23 import core = core.thread;
24 
25 /**
26  *Utility class to manipulate threads.
27  *
28  *Threads provide a way to run multiple parts of the code in parallel.
29  *
30  *When you launch a new thread, the execution is split and both the new thread and the caller run in parallel. 
31  *
32  *To use a Thread, you construct it directly with the function to execute as the entry point of the thread.
33  */
34 class Thread
35 {
36 	private core.Thread m_thread;
37 
38 	///Construct the thread from a functor with no argument
39 	///
40 	///Params:
41 	///		fn  = The function to use as the entry point of the thread.
42 	///		sz  = The size of the stack.
43 	this(void function() fn, size_t sz = 0)
44 	{
45 		m_thread = new core.Thread(fn,sz);
46 	}
47 
48 	///Construct the thread from a delegate with no argument
49 	///
50 	///Params:
51 	///		dg  = The delegate to use as the entry point of the thread.
52 	///		sz  = The size of the stack.
53 	this(void delegate() dg, size_t sz = 0)
54 	{
55 		m_thread = new core.Thread(dg, sz);
56 	}
57 
58 	///Destructor
59 	~this()
60 	{
61 		import dsfml.system.config;
62 		mixin(destructorOutput);
63 	}
64 
65 	///Run the thread.
66 	void launch()
67 	{
68 		m_thread.start();
69 	}
70 
71 	///Wait until the thread finishes.
72 	void wait()
73 	{
74 		if(m_thread.isRunning())
75 		{
76 			m_thread.join(true);
77 		}
78 	}
79 
80 	version(linux)
81 	{
82 		static void XInitThreads()
83 		{
84             linux_XInitThreads();
85 		}
86 	}
87 
88 }
89 
90 unittest
91 {
92 
93 
94 	version(DSFML_Unittest_System)
95 	{
96 		import std.stdio;
97 		import dsfml.system.sleep;
98 		import core.time;
99 		
100 
101 		void secondThreadHello()
102 		{
103 			for(int i = 0; i < 10; ++i)
104 			{
105 				writeln("Hello from the second thread!");
106 			}
107 		}
108 
109 
110 
111 
112 		writeln("Unit test for Thread class");
113 		writeln();
114 
115 		writeln("Running two functions at once.");
116 
117 		auto secondThread = new Thread(&secondThreadHello);
118 
119 
120 		secondThread.launch();
121 
122 		for(int i = 0; i < 10; ++i)
123 		{
124 			writeln("Hello from the main thread!");
125 		}
126 
127 		sleep(seconds(1));
128 
129 		//writeln("Letting a thread run completely before going back to the main thread.");
130 
131 		//secondThread = new Thread(&secondThreadHello);//To prevent threading errors, create a new thread before calling launch again
132 
133 		//secondThread.launch();
134 
135 		//secondThread.wait();
136 
137 		//for(int i = 0; i < 10; ++i)
138 		//{
139 		//	writeln("Hello from the main thread!");
140 		//}
141 	}
142 
143 }
144 
145 
146 version(linux) package extern(C) void linux_XInitThreads();