threading.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /* === Build Macro === */
  16. #ifndef POOL_MT // can be defined on command line
  17. # define POOL_MT 1
  18. #endif
  19. /* create fake symbol to avoid empty translation unit warning */
  20. int g_ZSTD_threading_useles_symbol;
  21. #if POOL_MT && defined(_WIN32)
  22. /**
  23. * Windows minimalist Pthread Wrapper
  24. */
  25. /* === Dependencies === */
  26. #include <process.h>
  27. #include <errno.h>
  28. #include "threading.h"
  29. /* === Implementation === */
  30. static unsigned __stdcall worker(void *arg)
  31. {
  32. ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg;
  33. thread->arg = thread->start_routine(thread->arg);
  34. return 0;
  35. }
  36. int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
  37. void* (*start_routine) (void*), void* arg)
  38. {
  39. (void)unused;
  40. thread->arg = arg;
  41. thread->start_routine = start_routine;
  42. thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL);
  43. if (!thread->handle)
  44. return errno;
  45. else
  46. return 0;
  47. }
  48. int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
  49. {
  50. DWORD result;
  51. if (!thread.handle) return 0;
  52. result = WaitForSingleObject(thread.handle, INFINITE);
  53. switch (result) {
  54. case WAIT_OBJECT_0:
  55. if (value_ptr) *value_ptr = thread.arg;
  56. return 0;
  57. case WAIT_ABANDONED:
  58. return EINVAL;
  59. default:
  60. return (int)GetLastError();
  61. }
  62. }
  63. #endif /* POOL_MT */