thread.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef ARGON2_THREAD_H
  18. #define ARGON2_THREAD_H
  19. #if !defined(ARGON2_NO_THREADS)
  20. /*
  21. Here we implement an abstraction layer for the simpĺe requirements
  22. of the Argon2 code. We only require 3 primitives---thread creation,
  23. joining, and termination---so full emulation of the pthreads API
  24. is unwarranted. Currently we wrap pthreads and Win32 threads.
  25. The API defines 2 types: the function pointer type,
  26. argon2_thread_func_t,
  27. and the type of the thread handle---argon2_thread_handle_t.
  28. */
  29. #if defined(_WIN32)
  30. #include <process.h>
  31. typedef unsigned(__stdcall *argon2_thread_func_t)(void *);
  32. typedef uintptr_t argon2_thread_handle_t;
  33. #else
  34. #include <pthread.h>
  35. typedef void *(*argon2_thread_func_t)(void *);
  36. typedef pthread_t argon2_thread_handle_t;
  37. #endif
  38. /* Creates a thread
  39. * @param handle pointer to a thread handle, which is the output of this
  40. * function. Must not be NULL.
  41. * @param func A function pointer for the thread's entry point. Must not be
  42. * NULL.
  43. * @param args Pointer that is passed as an argument to @func. May be NULL.
  44. * @return 0 if @handle and @func are valid pointers and a thread is successfully
  45. * created.
  46. */
  47. int kp_argon2_thread_create(argon2_thread_handle_t *handle,
  48. argon2_thread_func_t func, void *args);
  49. /* Waits for a thread to terminate
  50. * @param handle Handle to a thread created with argon2_thread_create.
  51. * @return 0 if @handle is a valid handle, and joining completed successfully.
  52. */
  53. int kp_argon2_thread_join(argon2_thread_handle_t handle);
  54. /* Terminate the current thread. Must be run inside a thread created by
  55. * argon2_thread_create.
  56. */
  57. void argon2_thread_exit(void);
  58. #endif /* ARGON2_NO_THREADS */
  59. #endif