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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef THREADING_H_938743
  14. #define THREADING_H_938743
  15. #include "debug.h"
  16. #if defined (__cplusplus)
  17. extern "C" {
  18. #endif
  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. #ifdef WINVER
  25. # undef WINVER
  26. #endif
  27. #define WINVER 0x0600
  28. #ifdef _WIN32_WINNT
  29. # undef _WIN32_WINNT
  30. #endif
  31. #define _WIN32_WINNT 0x0600
  32. #ifndef WIN32_LEAN_AND_MEAN
  33. # define WIN32_LEAN_AND_MEAN
  34. #endif
  35. #undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
  36. #include <windows.h>
  37. #undef ERROR
  38. #define ERROR(name) ZSTD_ERROR(name)
  39. /* mutex */
  40. #define ZSTD_pthread_mutex_t CRITICAL_SECTION
  41. #define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)
  42. #define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))
  43. #define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))
  44. #define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))
  45. /* condition variable */
  46. #define ZSTD_pthread_cond_t CONDITION_VARIABLE
  47. #define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)
  48. #define ZSTD_pthread_cond_destroy(a) ((void)(a))
  49. #define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
  50. #define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))
  51. #define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))
  52. /* ZSTD_pthread_create() and ZSTD_pthread_join() */
  53. typedef struct {
  54. HANDLE handle;
  55. void* (*start_routine)(void*);
  56. void* arg;
  57. } ZSTD_pthread_t;
  58. int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
  59. void* (*start_routine) (void*), void* arg);
  60. int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
  61. /**
  62. * add here more wrappers as required
  63. */
  64. #elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
  65. /* === POSIX Systems === */
  66. # include <pthread.h>
  67. #if DEBUGLEVEL < 1
  68. #define ZSTD_pthread_mutex_t pthread_mutex_t
  69. #define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
  70. #define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
  71. #define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))
  72. #define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))
  73. #define ZSTD_pthread_cond_t pthread_cond_t
  74. #define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))
  75. #define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))
  76. #define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))
  77. #define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))
  78. #define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))
  79. #define ZSTD_pthread_t pthread_t
  80. #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
  81. #define ZSTD_pthread_join(a, b) pthread_join((a),(b))
  82. #else /* DEBUGLEVEL >= 1 */
  83. /* Debug implementation of threading.
  84. * In this implementation we use pointers for mutexes and condition variables.
  85. * This way, if we forget to init/destroy them the program will crash or ASAN
  86. * will report leaks.
  87. */
  88. #define ZSTD_pthread_mutex_t pthread_mutex_t*
  89. int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
  90. int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);
  91. #define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))
  92. #define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))
  93. #define ZSTD_pthread_cond_t pthread_cond_t*
  94. int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
  95. int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
  96. #define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))
  97. #define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))
  98. #define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))
  99. #define ZSTD_pthread_t pthread_t
  100. #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
  101. #define ZSTD_pthread_join(a, b) pthread_join((a),(b))
  102. #endif
  103. #else /* ZSTD_MULTITHREAD not defined */
  104. /* No multithreading support */
  105. typedef int ZSTD_pthread_mutex_t;
  106. #define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)
  107. #define ZSTD_pthread_mutex_destroy(a) ((void)(a))
  108. #define ZSTD_pthread_mutex_lock(a) ((void)(a))
  109. #define ZSTD_pthread_mutex_unlock(a) ((void)(a))
  110. typedef int ZSTD_pthread_cond_t;
  111. #define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)
  112. #define ZSTD_pthread_cond_destroy(a) ((void)(a))
  113. #define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))
  114. #define ZSTD_pthread_cond_signal(a) ((void)(a))
  115. #define ZSTD_pthread_cond_broadcast(a) ((void)(a))
  116. /* do not use ZSTD_pthread_t */
  117. #endif /* ZSTD_MULTITHREAD */
  118. #if defined (__cplusplus)
  119. }
  120. #endif
  121. #endif /* THREADING_H_938743 */