threading.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2016 Tino Reichardt
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. *
  9. * You can contact the author at:
  10. * - zstdmt source repository: https://github.com/mcmilk/zstdmt
  11. */
  12. #ifndef THREADING_H_938743
  13. #define THREADING_H_938743
  14. #if defined (__cplusplus)
  15. extern "C" {
  16. #endif
  17. /* === Build Macro === */
  18. #ifndef POOL_MT // can be defined on command line
  19. # define POOL_MT 1
  20. #endif
  21. /* === Implementation === */
  22. #if POOL_MT && defined(_WIN32)
  23. /**
  24. * Define windows version before include
  25. */
  26. #undef WINVER
  27. #define WINVER 0x0600
  28. #undef _WIN32_WINNT
  29. #define _WIN32_WINNT 0x0600
  30. #ifndef WIN32_LEAN_AND_MEAN
  31. # define WIN32_LEAN_AND_MEAN
  32. #endif
  33. #include <windows.h>
  34. #include <stdio.h>
  35. /* mutex */
  36. #define ZSTD_pthread_mutex_t CRITICAL_SECTION
  37. #define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)
  38. #define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))
  39. #define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))
  40. #define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))
  41. /* condition variable */
  42. #define ZSTD_pthread_cond_t CONDITION_VARIABLE
  43. #define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)
  44. #define ZSTD_pthread_cond_destroy(a) ((void)(a))
  45. #define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
  46. #define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))
  47. #define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))
  48. /* ZSTD_pthread_create() and ZSTD_pthread_join() */
  49. typedef struct {
  50. HANDLE handle;
  51. void* (*start_routine)(void*);
  52. void* arg;
  53. } ZSTD_pthread_t;
  54. int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
  55. void* (*start_routine) (void*), void* arg);
  56. int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
  57. /**
  58. * add here more wrappers as required
  59. */
  60. #elif POOL_MT /* posix assumed ; need a better detection method */
  61. /* === POSIX Systems === */
  62. # include <pthread.h>
  63. #define ZSTD_pthread_mutex_t pthread_mutex_t
  64. #define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
  65. #define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
  66. #define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))
  67. #define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))
  68. #define ZSTD_pthread_cond_t pthread_cond_t
  69. #define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))
  70. #define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))
  71. #define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))
  72. #define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))
  73. #define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))
  74. #define ZSTD_pthread_t pthread_t
  75. #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
  76. #define ZSTD_pthread_join(a, b) pthread_join((a),(b))
  77. #else /* POOL_MT == 0 */
  78. /* No multithreading support */
  79. typedef int ZSTD_pthread_mutex_t;
  80. #define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)
  81. #define ZSTD_pthread_mutex_destroy(a) ((void)(a))
  82. #define ZSTD_pthread_mutex_lock(a) ((void)(a))
  83. #define ZSTD_pthread_mutex_unlock(a) ((void)(a))
  84. typedef int ZSTD_pthread_cond_t;
  85. #define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)
  86. #define ZSTD_pthread_cond_destroy(a) ((void)(a))
  87. #define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))
  88. #define ZSTD_pthread_cond_signal(a) ((void)(a))
  89. #define ZSTD_pthread_cond_broadcast(a) ((void)(a))
  90. /* do not use ZSTD_pthread_t */
  91. #endif /* POOL_MT */
  92. #if defined (__cplusplus)
  93. }
  94. #endif
  95. #endif /* THREADING_H_938743 */