thread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "config.h"
  25. #include "assert.h"
  26. #include "types.h"
  27. #include "semaphore.h"
  28. #if CROWN_PLATFORM_POSIX
  29. #include <pthread.h>
  30. #elif CROWN_PLATFORM_WINDOWS
  31. #include "win_headers.h"
  32. #include <process.h>
  33. #endif
  34. namespace crown
  35. {
  36. typedef int32_t (*ThreadFunction)(void*);
  37. struct Thread
  38. {
  39. Thread()
  40. #if CROWN_PLATFORM_POSIX
  41. : m_handle(0)
  42. #elif CROWN_PLATFORM_WINDOWS
  43. : m_handle(INVALID_HANDLE_VALUE)
  44. #endif
  45. , m_function(NULL)
  46. , m_data(NULL)
  47. , m_stack_size(0)
  48. , m_is_running(false)
  49. {
  50. }
  51. ~Thread()
  52. {
  53. if (m_is_running)
  54. stop();
  55. }
  56. void start(ThreadFunction func, void* data = NULL, size_t stack_size = 0)
  57. {
  58. CE_ASSERT(!m_is_running, "Thread is already running");
  59. CE_ASSERT(func != NULL, "Function must be != NULL");
  60. m_function = func;
  61. m_data = data;
  62. m_stack_size = stack_size;
  63. #if CROWN_PLATFORM_POSIX
  64. pthread_attr_t attr;
  65. int result = pthread_attr_init(&attr);
  66. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  67. CE_ASSERT(result == 0, "pthread_attr_init: errno = %d", result);
  68. if (m_stack_size != 0)
  69. {
  70. result = pthread_attr_setstacksize(&attr, m_stack_size);
  71. CE_ASSERT(result == 0, "pthread_attr_setstacksize: errno = %d", result);
  72. }
  73. result = pthread_create(&m_handle, &attr, thread_proc, this);
  74. CE_ASSERT(result == 0, "pthread_create: errno = %d", result);
  75. // Free attr memory
  76. result = pthread_attr_destroy(&attr);
  77. CE_ASSERT(result == 0, "pthread_attr_destroy: errno = %d", result);
  78. CE_UNUSED(result);
  79. #elif CROWN_PLATFORM_WINDOWS
  80. m_handle = CreateThread(NULL, stack_size, Thread::thread_proc, this, 0, NULL);
  81. CE_ASSERT(m_handle != NULL, "CreateThread: GetLastError = %d", GetLastError());
  82. #endif
  83. m_is_running = true;
  84. m_sem.wait();
  85. }
  86. void stop()
  87. {
  88. CE_ASSERT(m_is_running, "Thread is not running");
  89. #if CROWN_PLATFORM_POSIX
  90. int result = pthread_join(m_handle, NULL);
  91. CE_ASSERT(result == 0, "pthread_join: errno = %d", result);
  92. CE_UNUSED(result);
  93. m_handle = 0;
  94. #elif CROWN_PLATFORM_WINDOWS
  95. WaitForSingleObject(m_handle, INFINITE);
  96. // GetExitCodeThread(m_handle, &m_exit_code);
  97. CloseHandle(m_handle);
  98. m_handle = INVALID_HANDLE_VALUE;
  99. #endif
  100. m_is_running = false;
  101. }
  102. bool is_running()
  103. {
  104. return m_is_running;
  105. }
  106. private:
  107. int32_t run()
  108. {
  109. m_sem.post();
  110. return m_function(m_data);
  111. }
  112. #if CROWN_PLATFORM_POSIX
  113. static void* thread_proc(void* arg)
  114. {
  115. static int32_t result = -1;
  116. result = ((Thread*)arg)->run();
  117. return (void*)&result;
  118. }
  119. #elif CROWN_PLATFORM_WINDOWS
  120. static DWORD WINAPI thread_proc(void* arg)
  121. {
  122. Thread* thread = (Thread*) arg;
  123. int32_t result = thread->run();
  124. return result;
  125. }
  126. #endif
  127. private:
  128. #if CROWN_PLATFORM_POSIX
  129. pthread_t m_handle;
  130. #elif CROWN_PLATFORM_WINDOWS
  131. HANDLE m_handle;
  132. #endif
  133. ThreadFunction m_function;
  134. void* m_data;
  135. Semaphore m_sem;
  136. size_t m_stack_size;
  137. bool m_is_running;
  138. private:
  139. // Disable copying
  140. Thread(const Thread&);
  141. Thread& operator=(const Thread&);
  142. };
  143. } // namespace crown