/platform/unix/scaffold/thread.d

http://github.com/wilkie/djehuty · D · 187 lines · 69 code · 37 blank · 81 comment · 0 complexity · 7ad1d75c216650e31c77e3c52897480f MD5 · raw file

  1. /*
  2. * thread.d
  3. *
  4. * This Scaffold holds the Thread implementations for the Linux platform
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module scaffold.thread;
  10. import core.string;
  11. import core.color;
  12. import core.main;
  13. import core.definitions;
  14. import core.string;
  15. import synch.thread;
  16. import platform.unix.common;
  17. import platform.vars.thread;
  18. import platform.vars.condition;
  19. import platform.vars.mutex;
  20. import platform.vars.semaphore;
  21. void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds)
  22. {
  23. timespec timetoexpire;
  24. timetoexpire.tv_sec = (milliseconds / 1000);
  25. timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000;
  26. nanosleep(&timetoexpire, null);
  27. }
  28. /*
  29. extern (C)
  30. void *_djehuty_unix_thread_proc(void* udata)
  31. {
  32. Thread t_info = cast(Thread)(udata);
  33. ThreadPlatformVars* threadVars = ThreadGetPlatformVars(t_info);
  34. t_info.run();
  35. threadVars.id = 0;
  36. ThreadUninit(t_info);
  37. pthread_exit(null);
  38. return null;
  39. }
  40. void ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread)
  41. {
  42. int ret = pthread_create(&threadVars.id, null, &_djehuty_unix_thread_proc, cast(void *)thread);
  43. if (ret)
  44. {
  45. // error creating thread
  46. threadVars.id = 0;
  47. }
  48. }
  49. void ThreadStop(ref ThreadPlatformVars threadVars)
  50. {
  51. if (threadVars.id)
  52. {
  53. if (threadVars.id == pthread_self())
  54. {
  55. //soft exit
  56. printf("thread - soft kill\n");
  57. threadVars.id = 0;
  58. pthread_exit(null);
  59. }
  60. else
  61. {
  62. //hard exit
  63. printf("thread - hard kill\n");
  64. pthread_kill(threadVars.id, SIGKILL);
  65. }
  66. threadVars.id = 0;
  67. }
  68. }
  69. void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds)
  70. {
  71. timespec timetoexpire;
  72. timetoexpire.tv_sec = (milliseconds / 1000);
  73. timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000;
  74. nanosleep(&timetoexpire, null);
  75. }
  76. bool ThreadIsCurrent(ref ThreadPlatformVars threadVars)
  77. {
  78. return false;
  79. }
  80. */
  81. // Semaphores
  82. void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue)
  83. {
  84. sem_init(&semVars.sem_id, 0, initialValue);
  85. }
  86. void SemaphoreUninit(ref SemaphorePlatformVars semVars)
  87. {
  88. sem_destroy(&semVars.sem_id);
  89. }
  90. void SemaphoreUp(ref SemaphorePlatformVars semVars)
  91. {
  92. sem_post(&semVars.sem_id);
  93. }
  94. void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms)
  95. {
  96. // TODO: semaphore timeout
  97. sem_wait(&semVars.sem_id);
  98. }
  99. void SemaphoreDown(ref SemaphorePlatformVars semVars)
  100. {
  101. sem_wait(&semVars.sem_id);
  102. }
  103. // Mutexes
  104. void MutexInit(ref MutexPlatformVars mutVars)
  105. {
  106. pthread_mutex_init(&mutVars.mut_id, null);
  107. }
  108. void MutexUninit(ref MutexPlatformVars mutVars)
  109. {
  110. pthread_mutex_destroy(&mutVars.mut_id);
  111. }
  112. void MutexLock(ref MutexPlatformVars mutVars)
  113. {
  114. pthread_mutex_lock(&mutVars.mut_id);
  115. }
  116. void MutexLock(ref MutexPlatformVars mutVars, ref uint ms)
  117. {
  118. }
  119. void MutexUnlock(ref MutexPlatformVars mutVars)
  120. {
  121. pthread_mutex_unlock(&mutVars.mut_id);
  122. }
  123. void ConditionInit(ref ConditionPlatformVars condVars) {
  124. }
  125. void ConditionSignal(ref ConditionPlatformVars condVars) {
  126. }
  127. void ConditionWait(ref ConditionPlatformVars condVars) {
  128. }
  129. void ConditionWait(ref ConditionPlatformVars condVars, ref MutexPlatformVars mutVars) {
  130. }
  131. void ConditionUninit(ref ConditionPlatformVars condVars) {
  132. pthread_cond_destroy(&condVars.cond_id);
  133. }