PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/synch/thread.d

http://github.com/wilkie/djehuty
D | 297 lines | 202 code | 69 blank | 26 comment | 17 complexity | c34cb2f64ba99c8374ef220c1cee74fe MD5 | raw file
  1. module synch.thread;
  2. import gui.window;
  3. // Awww... my only runtime dependency
  4. version(Tango) {
  5. import Tango = tango.core.Thread;
  6. }
  7. else {
  8. import Phobos = std.thread;
  9. }
  10. import platform.vars.thread;
  11. import scaffold.thread;
  12. // access to exception handler
  13. import analyzing.debugger;
  14. // Access to the threads array
  15. import core.main;
  16. import core.system;
  17. import io.console;
  18. // Section: Core/Synchronization
  19. // Description: This class represents a thread. You can create and override the call function to use, or use a delegate to specify an external function to call.
  20. class Thread {
  21. // Description: Will create a normal thread that does not have any external callback functions.
  22. this() {
  23. stdThread = new overrideThread();
  24. stdThread.thread = this;
  25. startTime = time = System.time;
  26. }
  27. // Description: Will create a thread using the given delegate as the callback function.
  28. this(void delegate(bool) callback) {
  29. _thread_callback = callback;
  30. _thread_f_callback = null;
  31. stdThread = new overrideThread();
  32. stdThread.thread = this;
  33. startTime = time = System.time;
  34. }
  35. // Description: Will create a thread using the given function as the callback function.
  36. this(void function(bool) callback) {
  37. _thread_f_callback = callback;
  38. _thread_callback = null;
  39. stdThread = new overrideThread();
  40. stdThread.thread = this;
  41. }
  42. ~this() {
  43. stop();
  44. }
  45. // the common function for the thread
  46. // Description: This will be called upon execution of the thread. Normally, it will call the delegate, but if overriden, you can provide a function within the class to use as the execution space.
  47. void run() {
  48. if (_thread_callback !is null) {
  49. _thread_callback(false);
  50. }
  51. else if (_thread_f_callback !is null) {
  52. _thread_f_callback(false);
  53. }
  54. _inited = false;
  55. }
  56. // Description: This will allow an arbitrary member function to be used as the execution space.
  57. // callback: An address to a member function or a delegate literal.
  58. void callback(void delegate(bool) callback) {
  59. _thread_callback = callback;
  60. _thread_f_callback = null;
  61. }
  62. // Description: This will allow an arbitrary function to be used as the execution space.
  63. // callback: An address to a function or a function literal.
  64. void callback(void function(bool) callback) {
  65. _thread_f_callback = callback;
  66. _thread_callback = null;
  67. }
  68. // Description: This function will tell whether or not the current thread being executed is the thread created via this class.
  69. // Returns: Will return true when this thread is the current thread executing and false otherwise.
  70. bool isCurrentThread() {
  71. if (_inited) {
  72. return this is this.current(); //return ThreadIsCurrent(_pfvars);
  73. }
  74. return false;
  75. }
  76. // Description: This function will yield the thread for a certain amount of time.
  77. // milliseconds: The number of milliseconds to yield.
  78. void sleep(ulong milliseconds) {
  79. // we are given a long for length, windows only has an int function
  80. if (_inited) {
  81. ThreadSleep(_pfvars, milliseconds);
  82. }
  83. }
  84. // Description: This function will start the thread and call the threadProc() function, which will in turn execute an external delegate if provided.
  85. void start() {
  86. if (!_inited) {
  87. RegisterThread(this);
  88. //ThreadStart(_pfvars, this);
  89. startTime = time = System.time;
  90. if (stdThread is null) {
  91. stdThread = new overrideThread();
  92. }
  93. _inited = true;
  94. stdThread.start();
  95. }
  96. }
  97. // Description: This function will stop the thread prematurely.
  98. void stop() {
  99. if (_inited) {
  100. //ThreadStop(_pfvars);
  101. stdThread = null;
  102. UnregisterThread(this);
  103. }
  104. _inited = false;
  105. }
  106. void pleaseStop() {
  107. if (_thread_callback !is null) {
  108. _thread_callback(true);
  109. }
  110. else if (_thread_f_callback !is null) {
  111. _thread_f_callback(true);
  112. }
  113. }
  114. long getElapsed() {
  115. return System.time - time;
  116. }
  117. long getDelta() {
  118. long oldTime = time;
  119. time = System.time;
  120. return time - oldTime;
  121. }
  122. static Thread current() {
  123. Thread ret;
  124. version(LDC) {
  125. if (Tango.Thread.getThis() in threadById) {
  126. ret = threadById[Tango.Thread.getThis()];
  127. }
  128. }
  129. else {
  130. if (Phobos.Thread.getThis() in threadById) {
  131. ret = threadById[Phobos.Thread.getThis()];
  132. }
  133. }
  134. if (ret is null) {
  135. }
  136. return ret;
  137. }
  138. protected:
  139. void delegate (bool) _thread_callback = null;
  140. void function (bool) _thread_f_callback = null;
  141. int _threadProc() {
  142. run();
  143. stdThread = null;
  144. return 0;
  145. }
  146. bool _inited;
  147. long startTime;
  148. long time;
  149. Window wnd;
  150. ThreadPlatformVars _pfvars;
  151. overrideThread stdThread;
  152. version(GNU)
  153. {
  154. }
  155. version(LDC)
  156. {
  157. alias Tango.Thread RuntimeThread;
  158. class overrideThread : Tango.Thread
  159. {
  160. Thread thread;
  161. RuntimeThread runtimeThread;
  162. this() {
  163. super(&run);
  164. }
  165. void run()
  166. {
  167. threadById[Tango.Thread.getThis()] = thread;
  168. try {
  169. thread.run();
  170. }
  171. catch (Object o) {
  172. // Catch any unhandled exceptions
  173. Debugger.raiseException(cast(Exception)o, thread.wnd, thread);
  174. }
  175. stdThread = null;
  176. UnregisterThread(thread);
  177. return;
  178. }
  179. }
  180. }
  181. else
  182. {
  183. alias Phobos.Thread RuntimeThread;
  184. class overrideThread
  185. {
  186. Thread thread;
  187. RuntimeThread runtimeThread;
  188. this() {
  189. runtimeThread = new Phobos.Thread(&run);
  190. }
  191. void start() {
  192. runtimeThread.start();
  193. }
  194. int run() {
  195. threadById[Phobos.Thread.getThis()] = thread;
  196. try {
  197. thread.run();
  198. }
  199. catch (Object o) {
  200. // Catch any unhandled exceptions
  201. Debugger.raiseException(cast(Exception)o, thread.wnd, thread);
  202. }
  203. stdThread = null;
  204. UnregisterThread(thread);
  205. return 0;
  206. }
  207. }
  208. }
  209. static Thread[RuntimeThread] threadById;
  210. }
  211. void ThreadModuleInit() {
  212. // create a Thread for the main thread
  213. Thread mainThread = new Thread();
  214. mainThread._inited = true;
  215. version(Tango) {
  216. mainThread.stdThread.runtimeThread = Tango.Thread.getThis();
  217. }
  218. else {
  219. mainThread.stdThread.runtimeThread = Phobos.Thread.getThis();
  220. }
  221. Thread.threadById[mainThread.stdThread.runtimeThread] = mainThread;
  222. }
  223. void ThreadUninit(ref Thread t) {
  224. t._inited = false;
  225. }
  226. void ThreadSetWindow(ref Thread t, Window w) {
  227. t.wnd = w;
  228. }