threading.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg;
  30. thread->arg = thread->start_routine(thread->arg);
  31. return 0;
  32. }
  33. int ZSTD_pthread_create(ZSTD_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 ZSTD_pthread_join(ZSTD_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 */