thread.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * thread.c
  3. *
  4. * Copyright (c) 2012 Martin Szulecki All Rights Reserved.
  5. * Copyright (c) 2012 Nikias Bassen All Rights Reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "thread.h"
  22. int thread_new(thread_t *thread, thread_func_t thread_func, void* data)
  23. {
  24. #ifdef WIN32
  25. HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL);
  26. if (th == NULL) {
  27. return -1;
  28. }
  29. *thread = th;
  30. return 0;
  31. #else
  32. int res = pthread_create(thread, NULL, thread_func, data);
  33. return res;
  34. #endif
  35. }
  36. void thread_free(thread_t thread)
  37. {
  38. #ifdef WIN32
  39. CloseHandle(thread);
  40. #endif
  41. }
  42. void thread_join(thread_t thread)
  43. {
  44. /* wait for thread to complete */
  45. #ifdef WIN32
  46. WaitForSingleObject(thread, INFINITE);
  47. #else
  48. pthread_join(thread, NULL);
  49. #endif
  50. }
  51. void mutex_init(mutex_t* mutex)
  52. {
  53. #ifdef WIN32
  54. InitializeCriticalSection(mutex);
  55. #else
  56. pthread_mutex_init(mutex, NULL);
  57. #endif
  58. }
  59. void mutex_destroy(mutex_t* mutex)
  60. {
  61. #ifdef WIN32
  62. DeleteCriticalSection(mutex);
  63. #else
  64. pthread_mutex_destroy(mutex);
  65. #endif
  66. }
  67. void mutex_lock(mutex_t* mutex)
  68. {
  69. #ifdef WIN32
  70. EnterCriticalSection(mutex);
  71. #else
  72. pthread_mutex_lock(mutex);
  73. #endif
  74. }
  75. void mutex_unlock(mutex_t* mutex)
  76. {
  77. #ifdef WIN32
  78. LeaveCriticalSection(mutex);
  79. #else
  80. pthread_mutex_unlock(mutex);
  81. #endif
  82. }
  83. void thread_once(thread_once_t *once_control, void (*init_routine)(void))
  84. {
  85. #ifdef WIN32
  86. while (InterlockedExchange(&(once_control->lock), 1) != 0) {
  87. Sleep(1);
  88. }
  89. if (!once_control->state) {
  90. once_control->state = 1;
  91. init_routine();
  92. }
  93. InterlockedExchange(&(once_control->lock), 0);
  94. #else
  95. pthread_once(once_control, init_routine);
  96. #endif
  97. }