/platform/win/scaffold/thread.d

http://github.com/wilkie/djehuty · D · 159 lines · 58 code · 43 blank · 58 comment · 1 complexity · a43b24ff3a671a1ee772ed29171aee2c MD5 · raw file

  1. /*
  2. * thread.d
  3. *
  4. * This file implements the Scaffold for platform specific Thread
  5. * operations in Windows.
  6. *
  7. * Author: Dave Wilkinson
  8. *
  9. */
  10. module scaffold.thread;
  11. import core.main;
  12. import core.definitions;
  13. import core.string;
  14. import synch.thread;
  15. import platform.win.main;
  16. import platform.win.common;
  17. import platform.vars.mutex;
  18. import platform.vars.semaphore;
  19. import platform.vars.thread;
  20. import platform.vars.condition;
  21. /*extern(Windows)
  22. DWORD _win_djehuty_thread_proc(void* udata)
  23. {
  24. Thread t = cast(Thread)udata;
  25. t.run();
  26. ThreadPlatformVars* threadVars = ThreadGetPlatformVars(t);
  27. threadVars.thread = null;
  28. threadVars.thread_id = 0;
  29. ThreadUninit(t);
  30. return 0;
  31. }
  32. void ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread)
  33. {
  34. threadVars.thread = CreateThread(null, 0, &_win_djehuty_thread_proc, cast(void*)thread, 0, &threadVars.thread_id);
  35. }
  36. void ThreadStop(ref ThreadPlatformVars threadVars)
  37. {
  38. if (threadVars.thread_id == GetCurrentThreadId())
  39. { // soft exit if called from the created thread
  40. ExitThread(0);
  41. }
  42. else
  43. { // hard exit if called from another thread
  44. TerminateThread(threadVars.thread, 0);
  45. }
  46. threadVars.thread = null;
  47. threadVars.thread_id = 0;
  48. }*/
  49. void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) {
  50. while (milliseconds > 0xFFFFFFFF) {
  51. .Sleep(0xFFFFFFFF);
  52. milliseconds -= 0xFFFFFFFF;
  53. }
  54. .Sleep(cast(uint)milliseconds);
  55. }
  56. //bool ThreadIsCurrent(ref ThreadPlatformVars threadVars)
  57. //{
  58. // return threadVars.thread_id == GetCurrentThreadId();
  59. //}
  60. // Semaphores
  61. void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue) {
  62. semVars._semaphore = CreateSemaphoreA(null, (initialValue), 0xFFFFFFF, null);
  63. }
  64. void SemaphoreUninit(ref SemaphorePlatformVars semVars) {
  65. CloseHandle(semVars._semaphore);
  66. }
  67. void SemaphoreUp(ref SemaphorePlatformVars semVars) {
  68. ReleaseSemaphore(semVars._semaphore, 1, null);
  69. }
  70. void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms) {
  71. WaitForSingleObject(semVars._semaphore, ms);
  72. }
  73. void SemaphoreDown(ref SemaphorePlatformVars semVars) {
  74. WaitForSingleObject(semVars._semaphore, INFINITE);
  75. }
  76. // Mutexes
  77. void MutexInit(ref MutexPlatformVars mutVars) {
  78. // InitializeCriticalSection(mutVars._mutex);
  79. mutVars._semaphore = CreateSemaphoreA(null, (1), 0xFFFFFFF, null);
  80. }
  81. void MutexUninit(ref MutexPlatformVars mutVars) {
  82. //DeleteCriticalSection(mutVars._mutex);
  83. CloseHandle(mutVars._semaphore);
  84. }
  85. void MutexLock(ref MutexPlatformVars mutVars) {
  86. // EnterCriticalSection(mutVars._mutex);
  87. WaitForSingleObject(mutVars._semaphore, INFINITE);
  88. }
  89. void MutexLock(ref MutexPlatformVars mutVars, ref uint ms) {
  90. // XXX: Use TryEnterCriticalSection in a timed loop here
  91. //EnterCriticalSection(mutVars._mutex);
  92. WaitForSingleObject(mutVars._semaphore, ms);
  93. }
  94. void MutexUnlock(ref MutexPlatformVars mutVars) {
  95. //LeaveCriticalSection(mutVars._mutex);
  96. ReleaseSemaphore(mutVars._semaphore, 1, null);
  97. }
  98. // Conditions
  99. void ConditionInit(ref ConditionPlatformVars condVars) {
  100. }
  101. void ConditionSignal(ref ConditionPlatformVars condVars) {
  102. }
  103. void ConditionWait(ref ConditionPlatformVars condVars) {
  104. }
  105. void ConditionWait(ref ConditionPlatformVars condVars, ref MutexPlatformVars mutVars) {
  106. }
  107. void ConditionUninit(ref ConditionPlatformVars condVars) {
  108. }