threading.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #define ZSTD_DEPS_NEED_MALLOC
  64. #include "zstd_deps.h"
  65. int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
  66. {
  67. *mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
  68. if (!*mutex)
  69. return 1;
  70. return pthread_mutex_init(*mutex, attr);
  71. }
  72. int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
  73. {
  74. if (!*mutex)
  75. return 0;
  76. {
  77. int const ret = pthread_mutex_destroy(*mutex);
  78. ZSTD_free(*mutex);
  79. return ret;
  80. }
  81. }
  82. int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
  83. {
  84. *cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
  85. if (!*cond)
  86. return 1;
  87. return pthread_cond_init(*cond, attr);
  88. }
  89. int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
  90. {
  91. if (!*cond)
  92. return 0;
  93. {
  94. int const ret = pthread_cond_destroy(*cond);
  95. ZSTD_free(*cond);
  96. return ret;
  97. }
  98. }
  99. #endif