MelderThread.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef _MelderThread_h_
  2. #define _MelderThread_h_
  3. /* MelderThread.h
  4. *
  5. * Copyright (C) 2014-2017 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <vector>
  21. #include "Thing.h"
  22. #if defined (_WIN32)
  23. #define USE_WINTHREADS 1
  24. #define USE_PTHREADS 0
  25. #define USE_CPPTHREADS 0
  26. #elif defined (macintosh)
  27. #define USE_WINTHREADS 0
  28. #define USE_PTHREADS 1
  29. #define USE_CPPTHREADS 0
  30. #else
  31. #define USE_WINTHREADS 0
  32. #define USE_PTHREADS 1
  33. #define USE_CPPTHREADS 0
  34. #endif
  35. #if USE_WINTHREADS
  36. #include <windows.h>
  37. #include <process.h>
  38. #define MelderThread_MUTEX(_mutex) static CRITICAL_SECTION _mutex
  39. #define MelderThread_MUTEX_INIT(_mutex) InitializeCriticalSection (& _mutex)
  40. #define MelderThread_LOCK(_mutex) EnterCriticalSection (& _mutex)
  41. #define MelderThread_UNLOCK(_mutex) LeaveCriticalSection (& _mutex)
  42. #define MelderThread_RETURN_TYPE DWORD WINAPI
  43. #define MelderThread_RETURN return 0;
  44. #elif USE_PTHREADS
  45. #include <pthread.h>
  46. #define MelderThread_MUTEX(_mutex) static pthread_mutex_t _mutex = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER
  47. #define MelderThread_MUTEX_INIT(_mutex) (void) 0
  48. #define MelderThread_LOCK(_mutex) pthread_mutex_lock (& _mutex)
  49. #define MelderThread_UNLOCK(_mutex) pthread_mutex_unlock (& _mutex)
  50. #define MelderThread_RETURN_TYPE void *
  51. #define MelderThread_RETURN return nullptr;
  52. #elif USE_CPPTHREADS
  53. #include <mutex>
  54. #include <thread>
  55. #define MelderThread_MUTEX(_mutex) static std::mutex _mutex
  56. #define MelderThread_MUTEX_INIT(_mutex) (void) 0
  57. #define MelderThread_LOCK(_mutex) std::lock_guard <std::mutex> lock (_mutex)
  58. #define MelderThread_UNLOCK(_mutex) (void) 0
  59. #define MelderThread_RETURN_TYPE void
  60. #define MelderThread_RETURN return;
  61. #else
  62. /* No threads. Make single-threaded. */
  63. #define MelderThread_MUTEX(_mutex) static int _mutex
  64. #define MelderThread_MUTEX_INIT(_mutex) (void) 0
  65. #define MelderThread_LOCK(_mutex) (void) 0
  66. #define MelderThread_UNLOCK(_mutex) (void) 0
  67. #define MelderThread_RETURN_TYPE void
  68. #define MelderThread_RETURN return;
  69. #endif
  70. #if 0
  71. /* For debugging of lock code only. */
  72. #define MelderThread_MUTEX(_mutex) static volatile int _mutex
  73. #define MelderThread_MUTEX_INIT(_mutex) _mutex = 0
  74. #define MelderThread_LOCK(_mutex) do { while (_mutex) ; _mutex = 1; } while (0)
  75. #define MelderThread_UNLOCK(_mutex) _mutex = 0
  76. #endif
  77. inline static int MelderThread_getNumberOfProcessors () {
  78. #if USE_WINTHREADS
  79. return 8;
  80. #elif USE_PTHREADS
  81. return 8;
  82. #elif USE_CPPTHREADS
  83. return std::thread::hardware_concurrency ();
  84. #else
  85. return 1;
  86. #endif
  87. }
  88. #if USE_WINTHREADS
  89. template <class T> void MelderThread_run (DWORD (WINAPI *func) (T *), autoSomeThing <T> *args, int numberOfThreads) {
  90. if (numberOfThreads == 1) {
  91. func (args [0].get());
  92. } else {
  93. std::vector <HANDLE> threads (numberOfThreads);
  94. try {
  95. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  96. threads [ithread - 1] = CreateThread (nullptr, 0,
  97. (DWORD (WINAPI *)(void *)) func, (void *) args [ithread - 1].get(), 0, nullptr);
  98. }
  99. func (args [numberOfThreads - 1].get());
  100. } catch (MelderError) {
  101. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  102. WaitForSingleObject (threads [ithread - 1], INFINITE);
  103. CloseHandle (threads [ithread - 1]);
  104. }
  105. throw;
  106. }
  107. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  108. WaitForSingleObject (threads [ithread - 1], INFINITE);
  109. CloseHandle (threads [ithread - 1]);
  110. }
  111. }
  112. }
  113. #elif USE_PTHREADS
  114. template <class T> void MelderThread_run (void * (*func) (T *), autoSomeThing <T> *args, int numberOfThreads) {
  115. if (numberOfThreads == 1) {
  116. func (args [0].get());
  117. } else {
  118. std::vector <pthread_t> threads ((size_t) numberOfThreads);
  119. try {
  120. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  121. (void) pthread_create (& threads [(size_t) ithread - 1],
  122. nullptr, (void*(*)(void *)) func, (void *) args [ithread - 1].get());
  123. }
  124. func (args [numberOfThreads - 1].get());
  125. } catch (MelderError) {
  126. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  127. pthread_join (threads [(size_t) ithread - 1], nullptr);
  128. }
  129. throw;
  130. }
  131. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  132. pthread_join (threads [(size_t) ithread - 1], nullptr);
  133. }
  134. }
  135. }
  136. #elif USE_CPPTHREADS
  137. template <class T> void MelderThread_run (void * (*func) (T *), autoSomeThing <T> *args, int numberOfThreads) {
  138. if (numberOfThreads == 1) {
  139. func (args [0].get());
  140. } else {
  141. std::vector <std::thread> thread (numberOfThreads);
  142. try {
  143. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  144. thread [ithread - 1] = std::thread (func, args [ithread - 1].get());
  145. }
  146. func (args [numberOfThreads - 1].get());
  147. } catch (MelderError) {
  148. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  149. if (thread [ithread - 1]. joinable ())
  150. thread [ithread - 1]. join ();
  151. }
  152. throw;
  153. }
  154. for (int ithread = 1; ithread < numberOfThreads; ithread ++) {
  155. thread [ithread - 1]. join ();
  156. }
  157. }
  158. }
  159. #else
  160. template <class T> void MelderThread_run (void (*func) (T *), autoSomeThing <T> *args, int numberOfThreads) {
  161. func (args [0].get());
  162. }
  163. #endif
  164. #endif
  165. /* End of file MelderThread.h */