You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threading.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2016 Tino Reichardt
  3. * All rights reserved.
  4. *
  5. * You can contact the author at:
  6. * - zstdmt source repository: https://github.com/mcmilk/zstdmt
  7. *
  8. * This source code is licensed under both the BSD-style license (found in the
  9. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  10. * in the COPYING file in the root directory of this source tree).
  11. * You may select, at your option, one of the above-listed licenses.
  12. */
  13. /**
  14. * This file will hold wrapper for systems, which do not support pthreads
  15. */
  16. #include "threading.h"
  17. /* create fake symbol to avoid empty translation unit warning */
  18. int g_ZSTD_threading_useless_symbol;
  19. #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
  20. /**
  21. * Windows minimalist Pthread Wrapper, based on :
  22. * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
  23. */
  24. /* === Dependencies === */
  25. #include <process.h>
  26. #include <errno.h>
  27. /* === Implementation === */
  28. static unsigned __stdcall worker(void *arg)
  29. {
  30. ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg;
  31. thread->arg = thread->start_routine(thread->arg);
  32. return 0;
  33. }
  34. int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
  35. void* (*start_routine) (void*), void* arg)
  36. {
  37. (void)unused;
  38. thread->arg = arg;
  39. thread->start_routine = start_routine;
  40. thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL);
  41. if (!thread->handle)
  42. return errno;
  43. else
  44. return 0;
  45. }
  46. int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
  47. {
  48. DWORD result;
  49. if (!thread.handle) return 0;
  50. result = WaitForSingleObject(thread.handle, INFINITE);
  51. switch (result) {
  52. case WAIT_OBJECT_0:
  53. if (value_ptr) *value_ptr = thread.arg;
  54. return 0;
  55. case WAIT_ABANDONED:
  56. return EINVAL;
  57. default:
  58. return GetLastError();
  59. }
  60. }
  61. #endif /* ZSTD_MULTITHREAD */
  62. #if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
  63. #include <stdlib.h>
  64. int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
  65. {
  66. *mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
  67. if (!*mutex)
  68. return 1;
  69. return pthread_mutex_init(*mutex, attr);
  70. }
  71. int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
  72. {
  73. if (!*mutex)
  74. return 0;
  75. {
  76. int const ret = pthread_mutex_destroy(*mutex);
  77. free(*mutex);
  78. return ret;
  79. }
  80. }
  81. int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
  82. {
  83. *cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
  84. if (!*cond)
  85. return 1;
  86. return pthread_cond_init(*cond, attr);
  87. }
  88. int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
  89. {
  90. if (!*cond)
  91. return 0;
  92. {
  93. int const ret = pthread_cond_destroy(*cond);
  94. free(*cond);
  95. return ret;
  96. }
  97. }
  98. #endif