esl_threadmutex.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Cross Platform Thread/Mutex abstraction
  3. * Copyright(C) 2007 Michael Jerris
  4. *
  5. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  6. * copies of the Software, and permit persons to whom the Software is
  7. * furnished to do so.
  8. *
  9. * This work is provided under this license on an "as is" basis, without warranty of any kind,
  10. * either expressed or implied, including, without limitation, warranties that the covered code
  11. * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
  12. * risk as to the quality and performance of the covered code is with you. Should any covered
  13. * code prove defective in any respect, you (not the initial developer or any other contributor)
  14. * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
  15. * constitutes an essential part of this license. No use of any covered code is authorized hereunder
  16. * except under this disclaimer.
  17. *
  18. */
  19. #ifdef WIN32
  20. /* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
  21. #define _WIN32_WINNT 0x0400
  22. #endif
  23. #include "esl.h"
  24. #include "esl_threadmutex.h"
  25. #ifdef WIN32
  26. #include <process.h>
  27. #define ESL_THREAD_CALLING_CONVENTION __stdcall
  28. struct esl_mutex {
  29. CRITICAL_SECTION mutex;
  30. };
  31. #else
  32. #include <pthread.h>
  33. #define ESL_THREAD_CALLING_CONVENTION
  34. struct esl_mutex {
  35. pthread_mutex_t mutex;
  36. };
  37. #endif
  38. struct esl_thread {
  39. #ifdef WIN32
  40. void *handle;
  41. #else
  42. pthread_t handle;
  43. #endif
  44. void *private_data;
  45. esl_thread_function_t function;
  46. size_t stack_size;
  47. #ifndef WIN32
  48. pthread_attr_t attribute;
  49. #endif
  50. };
  51. size_t thread_default_stacksize = 240 * 1024;
  52. void esl_thread_override_default_stacksize(size_t size)
  53. {
  54. thread_default_stacksize = size;
  55. }
  56. static void * ESL_THREAD_CALLING_CONVENTION thread_launch(void *args)
  57. {
  58. void *exit_val;
  59. esl_thread_t *thread = (esl_thread_t *)args;
  60. exit_val = thread->function(thread, thread->private_data);
  61. #ifndef WIN32
  62. pthread_attr_destroy(&thread->attribute);
  63. #endif
  64. free(thread);
  65. return exit_val;
  66. }
  67. ESL_DECLARE(esl_status_t) esl_thread_create_detached(esl_thread_function_t func, void *data)
  68. {
  69. return esl_thread_create_detached_ex(func, data, thread_default_stacksize);
  70. }
  71. esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *data, size_t stack_size)
  72. {
  73. esl_thread_t *thread = NULL;
  74. esl_status_t status = ESL_FAIL;
  75. if (!func || !(thread = (esl_thread_t *)malloc(sizeof(esl_thread_t)))) {
  76. goto done;
  77. }
  78. thread->private_data = data;
  79. thread->function = func;
  80. thread->stack_size = stack_size;
  81. #if defined(WIN32)
  82. thread->handle = (void *)_beginthreadex(NULL, (unsigned)thread->stack_size, (unsigned int (__stdcall *)(void *))thread_launch, thread, 0, NULL);
  83. if (!thread->handle) {
  84. goto fail;
  85. }
  86. CloseHandle(thread->handle);
  87. status = ESL_SUCCESS;
  88. goto done;
  89. #else
  90. if (pthread_attr_init(&thread->attribute) != 0) goto fail;
  91. if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
  92. if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
  93. if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
  94. status = ESL_SUCCESS;
  95. goto done;
  96. failpthread:
  97. pthread_attr_destroy(&thread->attribute);
  98. #endif
  99. fail:
  100. if (thread) {
  101. free(thread);
  102. }
  103. done:
  104. return status;
  105. }
  106. ESL_DECLARE(esl_status_t) esl_mutex_create(esl_mutex_t **mutex)
  107. {
  108. esl_status_t status = ESL_FAIL;
  109. #ifndef WIN32
  110. pthread_mutexattr_t attr;
  111. #endif
  112. esl_mutex_t *check = NULL;
  113. check = (esl_mutex_t *)malloc(sizeof(**mutex));
  114. if (!check)
  115. goto done;
  116. #ifdef WIN32
  117. InitializeCriticalSection(&check->mutex);
  118. #else
  119. if (pthread_mutexattr_init(&attr)) {
  120. free(check);
  121. goto done;
  122. }
  123. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
  124. goto fail;
  125. if (pthread_mutex_init(&check->mutex, &attr))
  126. goto fail;
  127. goto success;
  128. fail:
  129. pthread_mutexattr_destroy(&attr);
  130. free(check);
  131. goto done;
  132. success:
  133. #endif
  134. *mutex = check;
  135. status = ESL_SUCCESS;
  136. done:
  137. return status;
  138. }
  139. ESL_DECLARE(esl_status_t) esl_mutex_destroy(esl_mutex_t **mutex)
  140. {
  141. esl_mutex_t *mp = *mutex;
  142. *mutex = NULL;
  143. if (!mp) {
  144. return ESL_FAIL;
  145. }
  146. #ifdef WIN32
  147. DeleteCriticalSection(&mp->mutex);
  148. #else
  149. if (pthread_mutex_destroy(&mp->mutex))
  150. return ESL_FAIL;
  151. #endif
  152. free(mp);
  153. return ESL_SUCCESS;
  154. }
  155. ESL_DECLARE(esl_status_t) esl_mutex_lock(esl_mutex_t *mutex)
  156. {
  157. #ifdef WIN32
  158. EnterCriticalSection(&mutex->mutex);
  159. #else
  160. if (pthread_mutex_lock(&mutex->mutex))
  161. return ESL_FAIL;
  162. #endif
  163. return ESL_SUCCESS;
  164. }
  165. ESL_DECLARE(esl_status_t) esl_mutex_trylock(esl_mutex_t *mutex)
  166. {
  167. #ifdef WIN32
  168. if (!TryEnterCriticalSection(&mutex->mutex))
  169. return ESL_FAIL;
  170. #else
  171. if (pthread_mutex_trylock(&mutex->mutex))
  172. return ESL_FAIL;
  173. #endif
  174. return ESL_SUCCESS;
  175. }
  176. ESL_DECLARE(esl_status_t) esl_mutex_unlock(esl_mutex_t *mutex)
  177. {
  178. #ifdef WIN32
  179. LeaveCriticalSection(&mutex->mutex);
  180. #else
  181. if (pthread_mutex_unlock(&mutex->mutex))
  182. return ESL_FAIL;
  183. #endif
  184. return ESL_SUCCESS;
  185. }
  186. /* For Emacs:
  187. * Local Variables:
  188. * mode:c
  189. * indent-tabs-mode:t
  190. * tab-width:4
  191. * c-basic-offset:4
  192. * End:
  193. * For VIM:
  194. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  195. */