/platform/osx/scaffold/thread.d

http://github.com/wilkie/djehuty · D · 185 lines · 65 code · 38 blank · 82 comment · 0 complexity · a6af937cc5285fbaf482e1ad4efe44eb 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.osx.common;
  17. import platform.unix.common;
  18. import platform.vars.thread;
  19. import platform.vars.condition;
  20. import platform.vars.mutex;
  21. import platform.vars.semaphore;
  22. import binding.c;
  23. void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) {
  24. timespec timetoexpire;
  25. timetoexpire.tv_sec = (milliseconds / 1000);
  26. timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000;
  27. int ret = nanosleep(&timetoexpire, null);
  28. }
  29. /*
  30. extern (C)
  31. void *_djehuty_unix_thread_proc(void* udata)
  32. {
  33. Thread t_info = cast(Thread)(udata);
  34. ThreadPlatformVars* threadVars = ThreadGetPlatformVars(t_info);
  35. t_info.run();
  36. threadVars.id = 0;
  37. ThreadUninit(t_info);
  38. pthread_exit(null);
  39. return null;
  40. }
  41. void ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread)
  42. {
  43. int ret = pthread_create(&threadVars.id, null, &_djehuty_unix_thread_proc, cast(void *)thread);
  44. if (ret)
  45. {
  46. // error creating thread
  47. threadVars.id = 0;
  48. }
  49. }
  50. void ThreadStop(ref ThreadPlatformVars threadVars)
  51. {
  52. if (threadVars.id)
  53. {
  54. if (threadVars.id == pthread_self())
  55. {
  56. //soft exit
  57. printf("thread - soft kill\n");
  58. threadVars.id = 0;
  59. pthread_exit(null);
  60. }
  61. else
  62. {
  63. //hard exit
  64. printf("thread - hard kill\n");
  65. pthread_kill(threadVars.id, SIGKILL);
  66. }
  67. threadVars.id = 0;
  68. }
  69. }
  70. void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds)
  71. {
  72. timespec timetoexpire;
  73. timetoexpire.tv_sec = (milliseconds / 1000);
  74. timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000;
  75. nanosleep(&timetoexpire, null);
  76. }
  77. bool ThreadIsCurrent(ref ThreadPlatformVars threadVars)
  78. {
  79. return false;
  80. }
  81. */
  82. // Semaphores
  83. void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue)
  84. {
  85. sem_init(&semVars.sem_id, 0, initialValue);
  86. }
  87. void SemaphoreUninit(ref SemaphorePlatformVars semVars)
  88. {
  89. sem_destroy(&semVars.sem_id);
  90. }
  91. void SemaphoreUp(ref SemaphorePlatformVars semVars)
  92. {
  93. sem_post(&semVars.sem_id);
  94. }
  95. void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms)
  96. {
  97. // TODO: semaphore timeout
  98. sem_wait(&semVars.sem_id);
  99. }
  100. void SemaphoreDown(ref SemaphorePlatformVars semVars)
  101. {
  102. sem_wait(&semVars.sem_id);
  103. }
  104. // Mutexes
  105. void MutexInit(ref MutexPlatformVars mutVars)
  106. {
  107. }
  108. void MutexUninit(ref MutexPlatformVars mutVars)
  109. {
  110. }
  111. void MutexLock(ref MutexPlatformVars mutVars)
  112. {
  113. }
  114. void MutexLock(ref MutexPlatformVars mutVars, ref uint ms)
  115. {
  116. }
  117. void MutexUnlock(ref MutexPlatformVars mutVars)
  118. {
  119. }
  120. void ConditionInit(ref ConditionPlatformVars condVars) {
  121. }
  122. void ConditionSignal(ref ConditionPlatformVars condVars) {
  123. }
  124. void ConditionWait(ref ConditionPlatformVars condVars) {
  125. }
  126. void ConditionWait(ref ConditionPlatformVars condVars, ref MutexPlatformVars mutVars) {
  127. }
  128. void ConditionUninit(ref ConditionPlatformVars condVars) {
  129. //pthread_cond_destroy(&condVars.cond_id);
  130. }