e099a00d217ddc8efae4f589f120acc01b249afe
[platform/upstream/cryptsetup.git] / lib / crypto_backend / argon2 / thread.c
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 #if !defined(ARGON2_NO_THREADS)
19
20 #include "thread.h"
21 #if defined(_WIN32)
22 #include <windows.h>
23 #endif
24
25 int argon2_thread_create(argon2_thread_handle_t *handle,
26                          argon2_thread_func_t func, void *args) {
27     if (NULL == handle || func == NULL) {
28         return -1;
29     }
30 #if defined(_WIN32)
31     *handle = _beginthreadex(NULL, 0, func, args, 0, NULL);
32     return *handle != 0 ? 0 : -1;
33 #else
34     return pthread_create(handle, NULL, func, args);
35 #endif
36 }
37
38 int argon2_thread_join(argon2_thread_handle_t handle) {
39 #if defined(_WIN32)
40     if (WaitForSingleObject((HANDLE)handle, INFINITE) == WAIT_OBJECT_0) {
41         return CloseHandle((HANDLE)handle) != 0 ? 0 : -1;
42     }
43     return -1;
44 #else
45     return pthread_join(handle, NULL);
46 #endif
47 }
48
49 void argon2_thread_exit(void) {
50 #if defined(_WIN32)
51     _endthreadex(0);
52 #else
53     pthread_exit(NULL);
54 #endif
55 }
56
57 #endif /* ARGON2_NO_THREADS */