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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /**
  13. * This file will hold wrapper for systems, which do not support pthreads
  14. */
  15. /* create fake symbol to avoid empty trnaslation unit warning */
  16. int g_ZSTD_threading_useles_symbol;
  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. /* === Dependencies === */
  23. #include <process.h>
  24. #include <errno.h>
  25. #include "threading.h"
  26. /* === Implementation === */
  27. static unsigned __stdcall worker(void *arg)
  28. {
  29. pthread_t* const thread = (pthread_t*) arg;
  30. thread->arg = thread->start_routine(thread->arg);
  31. return 0;
  32. }
  33. int pthread_create(pthread_t* thread, const void* unused,
  34. void* (*start_routine) (void*), void* arg)
  35. {
  36. (void)unused;
  37. thread->arg = arg;
  38. thread->start_routine = start_routine;
  39. thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL);
  40. if (!thread->handle)
  41. return errno;
  42. else
  43. return 0;
  44. }
  45. int _pthread_join(pthread_t * thread, void **value_ptr)
  46. {
  47. DWORD result;
  48. if (!thread->handle) return 0;
  49. result = WaitForSingleObject(thread->handle, INFINITE);
  50. switch (result) {
  51. case WAIT_OBJECT_0:
  52. if (value_ptr) *value_ptr = thread->arg;
  53. return 0;
  54. case WAIT_ABANDONED:
  55. return EINVAL;
  56. default:
  57. return GetLastError();
  58. }
  59. }
  60. #endif /* ZSTD_MULTITHREAD */