thread.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Argon2 reference source code package - reference C implementations
  3. *
  4. * Copyright 2015
  5. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  6. *
  7. * You may use this work under the terms of a Creative Commons CC0 1.0
  8. * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  9. * these licenses can be found at:
  10. *
  11. * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  12. * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * You should have received a copy of both of these licenses along with this
  15. * software. If not, they may be obtained at the above URLs.
  16. */
  17. #if !defined(ARGON2_NO_THREADS)
  18. #include "thread.h"
  19. #if defined(_WIN32)
  20. #include <windows.h>
  21. #endif
  22. int kp_argon2_thread_create(argon2_thread_handle_t *handle,
  23. argon2_thread_func_t func, void *args) {
  24. if (NULL == handle || func == NULL) {
  25. return -1;
  26. }
  27. #if defined(_WIN32)
  28. *handle = _beginthreadex(NULL, 0, func, args, 0, NULL);
  29. return *handle != 0 ? 0 : -1;
  30. #else
  31. return pthread_create(handle, NULL, func, args);
  32. #endif
  33. }
  34. int kp_argon2_thread_join(argon2_thread_handle_t handle) {
  35. #if defined(_WIN32)
  36. if (WaitForSingleObject((HANDLE)handle, INFINITE) == WAIT_OBJECT_0) {
  37. return CloseHandle((HANDLE)handle) != 0 ? 0 : -1;
  38. }
  39. return -1;
  40. #else
  41. return pthread_join(handle, NULL);
  42. #endif
  43. }
  44. void argon2_thread_exit(void) {
  45. #if defined(_WIN32)
  46. _endthreadex(0);
  47. #else
  48. pthread_exit(NULL);
  49. #endif
  50. }
  51. #endif /* ARGON2_NO_THREADS */