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.h 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
  18. /**
  19. * Windows minimalist Pthread Wrapper, based on :
  20. * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
  21. */
  22. #ifdef WINVER
  23. # undef WINVER
  24. #endif
  25. #define WINVER 0x0600
  26. #ifdef _WIN32_WINNT
  27. # undef _WIN32_WINNT
  28. #endif
  29. #define _WIN32_WINNT 0x0600
  30. #ifndef WIN32_LEAN_AND_MEAN
  31. # define WIN32_LEAN_AND_MEAN
  32. #endif
  33. #undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
  34. #include <windows.h>
  35. #undef ERROR
  36. #define ERROR(name) ZSTD_ERROR(name)
  37. /* mutex */
  38. #define pthread_mutex_t CRITICAL_SECTION
  39. #define pthread_mutex_init(a,b) (InitializeCriticalSection((a)), 0)
  40. #define pthread_mutex_destroy(a) DeleteCriticalSection((a))
  41. #define pthread_mutex_lock(a) EnterCriticalSection((a))
  42. #define pthread_mutex_unlock(a) LeaveCriticalSection((a))
  43. /* condition variable */
  44. #define pthread_cond_t CONDITION_VARIABLE
  45. #define pthread_cond_init(a, b) (InitializeConditionVariable((a)), 0)
  46. #define pthread_cond_destroy(a) /* No delete */
  47. #define pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
  48. #define pthread_cond_signal(a) WakeConditionVariable((a))
  49. #define pthread_cond_broadcast(a) WakeAllConditionVariable((a))
  50. /* pthread_create() and pthread_join() */
  51. typedef struct {
  52. HANDLE handle;
  53. void* (*start_routine)(void*);
  54. void* arg;
  55. } pthread_t;
  56. int pthread_create(pthread_t* thread, const void* unused,
  57. void* (*start_routine) (void*), void* arg);
  58. #define pthread_join(a, b) _pthread_join(&(a), (b))
  59. int _pthread_join(pthread_t* thread, void** value_ptr);
  60. /**
  61. * add here more wrappers as required
  62. */
  63. #elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
  64. /* === POSIX Systems === */
  65. # include <pthread.h>
  66. #else /* ZSTD_MULTITHREAD not defined */
  67. /* No multithreading support */
  68. #define pthread_mutex_t int /* #define rather than typedef, because sometimes pthread support is implicit, resulting in duplicated symbols */
  69. #define pthread_mutex_init(a,b) ((void)a, 0)
  70. #define pthread_mutex_destroy(a)
  71. #define pthread_mutex_lock(a)
  72. #define pthread_mutex_unlock(a)
  73. #define pthread_cond_t int
  74. #define pthread_cond_init(a,b) ((void)a, 0)
  75. #define pthread_cond_destroy(a)
  76. #define pthread_cond_wait(a,b)
  77. #define pthread_cond_signal(a)
  78. #define pthread_cond_broadcast(a)
  79. /* do not use pthread_t */
  80. #endif /* ZSTD_MULTITHREAD */
  81. #if defined (__cplusplus)
  82. }
  83. #endif
  84. #endif /* THREADING_H_938743 */