Removed threads support from Argon2.

This commit is contained in:
XMRig 2019-08-17 12:33:08 +07:00
parent 4efe6040e9
commit 77eb474b29
4 changed files with 2 additions and 187 deletions

View file

@ -6,7 +6,6 @@ set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)
include(CheckCSourceCompiles) include(CheckCSourceCompiles)
find_package(Threads REQUIRED)
add_library(argon2-interface INTERFACE) add_library(argon2-interface INTERFACE)
target_include_directories(argon2-interface INTERFACE target_include_directories(argon2-interface INTERFACE
@ -24,20 +23,13 @@ add_library(argon2 STATIC
lib/encoding.c lib/encoding.c
lib/genkat.c lib/genkat.c
lib/impl-select.c lib/impl-select.c
lib/thread.c
lib/blake2/blake2.c lib/blake2/blake2.c
) )
target_link_libraries(argon2
PUBLIC argon2-interface ${CMAKE_THREAD_LIBS_INIT}
PRIVATE argon2-internal
)
set_property(TARGET argon2 PROPERTY VERSION ${Upstream_VERSION}) set_property(TARGET argon2 PROPERTY VERSION ${Upstream_VERSION})
set_property(TARGET argon2 PROPERTY SOVERSION 1) set_property(TARGET argon2 PROPERTY SOVERSION 1)
set_property(TARGET argon2 PROPERTY INTERFACE_ARGON2_MAJOR_VERSION 1) set_property(TARGET argon2 PROPERTY INTERFACE_ARGON2_MAJOR_VERSION 1)
set_property(TARGET argon2 APPEND PROPERTY set_property(TARGET argon2 APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ARGON2_MAJOR_VERSION)
COMPATIBLE_INTERFACE_STRING ARGON2_MAJOR_VERSION
)
if (CMAKE_C_COMPILER_ID MATCHES MSVC) if (CMAKE_C_COMPILER_ID MATCHES MSVC)
function(add_feature_impl FEATURE MSVC_FLAG DEF) function(add_feature_impl FEATURE MSVC_FLAG DEF)

View file

@ -27,7 +27,6 @@
#include <string.h> #include <string.h>
#include "core.h" #include "core.h"
#include "thread.h"
#include "blake2/blake2.h" #include "blake2/blake2.h"
#include "blake2/blake2-impl.h" #include "blake2/blake2-impl.h"
@ -250,18 +249,6 @@ uint32_t index_alpha(const argon2_instance_t *instance,
return absolute_position; return absolute_position;
} }
#ifdef _WIN32
static unsigned __stdcall fill_segment_thr(void *thread_data)
#else
static void *fill_segment_thr(void *thread_data)
#endif
{
argon2_thread_data *my_data = thread_data;
fill_segment(my_data->instance_ptr, my_data->pos);
argon2_thread_exit();
return 0;
}
/* Single-threaded version for p=1 case */ /* Single-threaded version for p=1 case */
static int fill_memory_blocks_st(argon2_instance_t *instance) { static int fill_memory_blocks_st(argon2_instance_t *instance) {
uint32_t r, s, l; uint32_t r, s, l;
@ -281,93 +268,12 @@ static int fill_memory_blocks_st(argon2_instance_t *instance) {
return ARGON2_OK; return ARGON2_OK;
} }
/* Multi-threaded version for p > 1 case */
static int fill_memory_blocks_mt(argon2_instance_t *instance) {
uint32_t r, s;
argon2_thread_handle_t *thread = NULL;
argon2_thread_data *thr_data = NULL;
int rc = ARGON2_OK;
/* 1. Allocating space for threads */
thread = calloc(instance->lanes, sizeof(argon2_thread_handle_t));
if (thread == NULL) {
rc = ARGON2_MEMORY_ALLOCATION_ERROR;
goto fail;
}
thr_data = calloc(instance->lanes, sizeof(argon2_thread_data));
if (thr_data == NULL) {
rc = ARGON2_MEMORY_ALLOCATION_ERROR;
goto fail;
}
for (r = 0; r < instance->passes; ++r) {
for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
uint32_t l;
/* 2. Calling threads */
for (l = 0; l < instance->lanes; ++l) {
argon2_position_t position;
/* 2.1 Join a thread if limit is exceeded */
if (l >= instance->threads) {
if (argon2_thread_join(thread[l - instance->threads])) {
rc = ARGON2_THREAD_FAIL;
goto fail;
}
}
/* 2.2 Create thread */
position.pass = r;
position.lane = l;
position.slice = (uint8_t)s;
position.index = 0;
thr_data[l].instance_ptr =
instance; /* preparing the thread input */
memcpy(&(thr_data[l].pos), &position,
sizeof(argon2_position_t));
if (argon2_thread_create(&thread[l], &fill_segment_thr,
(void *)&thr_data[l])) {
rc = ARGON2_THREAD_FAIL;
goto fail;
}
/* fill_segment(instance, position); */
/*Non-thread equivalent of the lines above */
}
/* 3. Joining remaining threads */
for (l = instance->lanes - instance->threads; l < instance->lanes;
++l) {
if (argon2_thread_join(thread[l])) {
rc = ARGON2_THREAD_FAIL;
goto fail;
}
}
}
if (instance->print_internals) {
internal_kat(instance, r); /* Print all memory blocks */
}
}
fail:
if (thread != NULL) {
free(thread);
}
if (thr_data != NULL) {
free(thr_data);
}
return rc;
}
int fill_memory_blocks(argon2_instance_t *instance) { int fill_memory_blocks(argon2_instance_t *instance) {
if (instance == NULL || instance->lanes == 0) { if (instance == NULL || instance->lanes == 0) {
return ARGON2_INCORRECT_PARAMETER; return ARGON2_INCORRECT_PARAMETER;
} }
return instance->threads == 1 ? return fill_memory_blocks_st(instance);
fill_memory_blocks_st(instance) : fill_memory_blocks_mt(instance);
} }
int validate_inputs(const argon2_context *context) { int validate_inputs(const argon2_context *context) {

View file

@ -1,36 +0,0 @@
#include "thread.h"
#if defined(_WIN32)
#include <windows.h>
#endif
int argon2_thread_create(argon2_thread_handle_t *handle,
argon2_thread_func_t func, void *args) {
if (NULL == handle || func == NULL) {
return -1;
}
#if defined(_WIN32)
*handle = _beginthreadex(NULL, 0, func, args, 0, NULL);
return *handle != 0 ? 0 : -1;
#else
return pthread_create(handle, NULL, func, args);
#endif
}
int argon2_thread_join(argon2_thread_handle_t handle) {
#if defined(_WIN32)
if (WaitForSingleObject((HANDLE)handle, INFINITE) == WAIT_OBJECT_0) {
return CloseHandle((HANDLE)handle) != 0 ? 0 : -1;
}
return -1;
#else
return pthread_join(handle, NULL);
#endif
}
void argon2_thread_exit(void) {
#if defined(_WIN32)
_endthreadex(0);
#else
pthread_exit(NULL);
#endif
}

View file

@ -1,47 +0,0 @@
#ifndef ARGON2_THREAD_H
#define ARGON2_THREAD_H
/*
Here we implement an abstraction layer for the simpĺe requirements
of the Argon2 code. We only require 3 primitives---thread creation,
joining, and termination---so full emulation of the pthreads API
is unwarranted. Currently we wrap pthreads and Win32 threads.
The API defines 2 types: the function pointer type,
argon2_thread_func_t,
and the type of the thread handle---argon2_thread_handle_t.
*/
#if defined(_WIN32)
#include <process.h>
#include <stdint.h>
typedef unsigned(__stdcall *argon2_thread_func_t)(void *);
typedef uintptr_t argon2_thread_handle_t;
#else
#include <pthread.h>
typedef void *(*argon2_thread_func_t)(void *);
typedef pthread_t argon2_thread_handle_t;
#endif
/* Creates a thread
* @param handle pointer to a thread handle, which is the output of this
* function. Must not be NULL.
* @param func A function pointer for the thread's entry point. Must not be
* NULL.
* @param args Pointer that is passed as an argument to @func. May be NULL.
* @return 0 if @handle and @func are valid pointers and a thread is successfuly
* created.
*/
int argon2_thread_create(argon2_thread_handle_t *handle,
argon2_thread_func_t func, void *args);
/* Waits for a thread to terminate
* @param handle Handle to a thread created with argon2_thread_create.
* @return 0 if @handle is a valid handle, and joining completed successfully.
*/
int argon2_thread_join(argon2_thread_handle_t handle);
/* Terminate the current thread. Must be run inside a thread created by
* argon2_thread_create.
*/
void argon2_thread_exit(void);
#endif