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