Detect the number of available CPU cores by using either sched_getaffinity(), GetProcessAffinityMask() or sysconf(), depending on the target platform.
This commit is contained in:
parent
fdc1c8b0d8
commit
69df385d57
@ -4,9 +4,9 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# define _CRT_SECURE_NO_WARNINGS 1
|
# define _CRT_SECURE_NO_WARNINGS 1
|
||||||
#else
|
#else
|
||||||
# define _GNU_SOURCE 1
|
# define _GNU_SOURCE 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# define _CRT_SECURE_NO_WARNINGS 1
|
# define _CRT_SECURE_NO_WARNINGS 1
|
||||||
#else
|
#else
|
||||||
# define _GNU_SOURCE 1
|
# define _GNU_SOURCE 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# define _CRT_SECURE_NO_WARNINGS 1
|
# define _CRT_SECURE_NO_WARNINGS 1
|
||||||
#else
|
#else
|
||||||
# define _GNU_SOURCE 1
|
# define _GNU_SOURCE 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
@ -126,7 +126,7 @@ int weak_passphrase(const char *str)
|
|||||||
{
|
{
|
||||||
flags |= isupper(c) ? 0x1 : 0x2;
|
flags |= isupper(c) ? 0x1 : 0x2;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
flags |= isdigit(c) ? 0x4 : 0x8;
|
flags |= isdigit(c) ? 0x4 : 0x8;
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# define _CRT_SECURE_NO_WARNINGS 1
|
# define _CRT_SECURE_NO_WARNINGS 1
|
||||||
#else
|
#else
|
||||||
# define _GNU_SOURCE 1
|
# define _GNU_SOURCE 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# define WIN32_LEAN_AND_MEAN 1
|
# define WIN32_LEAN_AND_MEAN 1
|
||||||
# define _CRT_SECURE_NO_WARNINGS 1
|
# define _CRT_SECURE_NO_WARNINGS 1
|
||||||
#else
|
#else
|
||||||
# define _GNU_SOURCE 1
|
# define _GNU_SOURCE 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
@ -23,31 +23,31 @@
|
|||||||
|
|
||||||
/* Platform support */
|
/* Platform support */
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <Windows.h>
|
# include <Windows.h>
|
||||||
# include <io.h>
|
# include <io.h>
|
||||||
# include <fcntl.h>
|
# include <fcntl.h>
|
||||||
# define STAT_T struct _stati64
|
# define STAT_T struct _stati64
|
||||||
# define STAT(X,Y) _wstati64((X),(Y))
|
# define STAT(X,Y) _wstati64((X),(Y))
|
||||||
# define FSTAT(X,Y) _fstati64((X),(Y))
|
# define FSTAT(X,Y) _fstati64((X),(Y))
|
||||||
# define FILENO(X) _fileno((X))
|
# define FILENO(X) _fileno((X))
|
||||||
# define S_IFMT _S_IFMT
|
# define S_IFMT _S_IFMT
|
||||||
# define S_IFDIR _S_IFDIR
|
# define S_IFDIR _S_IFDIR
|
||||||
# define S_IFIFO _S_IFIFO
|
# define S_IFIFO _S_IFIFO
|
||||||
# ifndef _O_U8TEXT
|
# ifndef _O_U8TEXT
|
||||||
# define _O_U8TEXT 0x40000
|
# define _O_U8TEXT 0x40000
|
||||||
# endif
|
# endif
|
||||||
#else
|
#else
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
|
# if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
|
||||||
# define STAT_T struct stat64
|
# define STAT_T struct stat64
|
||||||
# define STAT(X,Y) stat64((X),(Y))
|
# define STAT(X,Y) stat64((X),(Y))
|
||||||
# define FSTAT(X,Y) fstat64((X),(Y))
|
# define FSTAT(X,Y) fstat64((X),(Y))
|
||||||
# else
|
# else
|
||||||
# define STAT_T struct stat
|
# define STAT_T struct stat
|
||||||
# define STAT(X,Y) stat((X),(Y))
|
# define STAT(X,Y) stat((X),(Y))
|
||||||
# define FSTAT(X,Y) fstat((X),(Y))
|
# define FSTAT(X,Y) fstat((X),(Y))
|
||||||
# endif
|
# endif
|
||||||
# define FILENO(X) fileno((X))
|
# define FILENO(X) fileno((X))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
#else
|
#else
|
||||||
#define SLUNKCRYPT_API
|
#define SLUNKCRYPT_API
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* C++ support
|
* C++ support
|
||||||
*/
|
*/
|
||||||
|
@ -298,7 +298,7 @@ slunkcrypt_t slunkcrypt_alloc_ext(const uint64_t nonce, const uint8_t *const pas
|
|||||||
{
|
{
|
||||||
crypt_state_t* state = NULL;
|
crypt_state_t* state = NULL;
|
||||||
|
|
||||||
if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) ||
|
if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) ||
|
||||||
(mode < SLUNKCRYPT_ENCRYPT) || (mode > SLUNKCRYPT_DECRYPT) || (!param) || (param->version == 0U) || (param->version > SLUNKCRYPT_PARAM_VERSION))
|
(mode < SLUNKCRYPT_ENCRYPT) || (mode > SLUNKCRYPT_DECRYPT) || (!param) || (param->version == 0U) || (param->version > SLUNKCRYPT_PARAM_VERSION))
|
||||||
{
|
{
|
||||||
return SLUNKCRYPT_NULL;
|
return SLUNKCRYPT_NULL;
|
||||||
|
@ -3,6 +3,12 @@
|
|||||||
/* This work has been released under the CC0 1.0 Universal license! */
|
/* This work has been released under the CC0 1.0 Universal license! */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
# define _CRT_SECURE_NO_WARNINGS 1
|
||||||
|
#else
|
||||||
|
# define _GNU_SOURCE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
@ -16,10 +22,17 @@
|
|||||||
# define PTW32_STATIC_LIB 1
|
# define PTW32_STATIC_LIB 1
|
||||||
#endif
|
#endif
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <sched.h>
|
||||||
|
|
||||||
/* System info */
|
/* Platform */
|
||||||
#ifdef __unix__
|
#if defined(__linux__) || defined(PTW32_VERSION) || defined(__CYGWIN__)
|
||||||
# include <sys/sysinfo.h>
|
# define HAVE_SCHED_GETAFFINITY 1
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
# define WIN32_LEAN_AND_MEAN 1
|
||||||
|
# define HAVE_GETPROCESSAFFINITYMASK 1
|
||||||
|
# include <Windows.h>
|
||||||
|
#else
|
||||||
|
# include <unistd.h> /* fall back to sysconf() function */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* States */
|
/* States */
|
||||||
@ -120,6 +133,35 @@ while(0)
|
|||||||
} \
|
} \
|
||||||
while(0)
|
while(0)
|
||||||
|
|
||||||
|
// ==========================================================================
|
||||||
|
// System info
|
||||||
|
// ==========================================================================
|
||||||
|
|
||||||
|
static size_t detect_available_cpu_count(void)
|
||||||
|
{
|
||||||
|
long num_processors = 0L;
|
||||||
|
#if defined(HAVE_SCHED_GETAFFINITY)
|
||||||
|
cpu_set_t cpu_mask;
|
||||||
|
CPU_ZERO(&cpu_mask);
|
||||||
|
if (sched_getaffinity(0, sizeof(cpu_set_t), &cpu_mask) == 0)
|
||||||
|
{
|
||||||
|
num_processors = CPU_COUNT(&cpu_mask);
|
||||||
|
}
|
||||||
|
#elif defined(HAVE_GETPROCESSAFFINITYMASK)
|
||||||
|
DWORD_PTR proc_mask, sys_mask;
|
||||||
|
if (GetProcessAffinityMask(GetCurrentProcess(), &proc_mask, &sys_mask))
|
||||||
|
{
|
||||||
|
for (; proc_mask != 0U; proc_mask &= proc_mask - 1U)
|
||||||
|
{
|
||||||
|
++num_processors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
num_processors = sysconf(_SC_NPROCESSORS_ONLN);
|
||||||
|
#endif
|
||||||
|
return (num_processors > 0) ? ((size_t)num_processors) : 1U;
|
||||||
|
}
|
||||||
|
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
// Thread main
|
// Thread main
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
@ -128,7 +170,7 @@ static void *worker_thread_main(void *const arg)
|
|||||||
{
|
{
|
||||||
thrdpl_thread_t *const data = (thrdpl_thread_t*) arg;
|
thrdpl_thread_t *const data = (thrdpl_thread_t*) arg;
|
||||||
thrdpl_shared_t *const shared = (thrdpl_shared_t*) data->shared;
|
thrdpl_shared_t *const shared = (thrdpl_shared_t*) data->shared;
|
||||||
|
|
||||||
size_t previous = 0U;
|
size_t previous = 0U;
|
||||||
|
|
||||||
PTHRD_MUTEX_ENTER(&shared->mutex);
|
PTHRD_MUTEX_ENTER(&shared->mutex);
|
||||||
@ -157,28 +199,6 @@ static void *worker_thread_main(void *const arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================================================
|
|
||||||
// System info
|
|
||||||
// ==========================================================================
|
|
||||||
|
|
||||||
#if defined(__unix__)
|
|
||||||
# define NUM_PROCESSORS_FUNC get_nprocs
|
|
||||||
#elif defined(PTW32_VERSION)
|
|
||||||
# define NUM_PROCESSORS_FUNC pthread_num_processors_np
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static size_t detect_cpu_count(void)
|
|
||||||
{
|
|
||||||
#ifdef NUM_PROCESSORS_FUNC
|
|
||||||
const int cpu_count = NUM_PROCESSORS_FUNC();
|
|
||||||
if (cpu_count > 0)
|
|
||||||
{
|
|
||||||
return (size_t) cpu_count;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return 1U;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
// Thread pool API
|
// Thread pool API
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
@ -188,7 +208,7 @@ thrdpl_t *slunkcrypt_thrdpl_create(const size_t count, const thrdpl_worker_t wor
|
|||||||
size_t i;
|
size_t i;
|
||||||
thrdpl_t *thrdpl = NULL;
|
thrdpl_t *thrdpl = NULL;
|
||||||
|
|
||||||
const size_t cpu_count = BOUND(1U, (count > 0U) ? count : detect_cpu_count(), MAX_THREADS);
|
const size_t cpu_count = BOUND(1U, (count > 0U) ? count : detect_available_cpu_count(), MAX_THREADS);
|
||||||
if (cpu_count < 2U)
|
if (cpu_count < 2U)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -206,7 +226,7 @@ thrdpl_t *slunkcrypt_thrdpl_create(const size_t count, const thrdpl_worker_t wor
|
|||||||
{
|
{
|
||||||
goto failure;
|
goto failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_cond_init(&thrdpl->shared.cond_0, NULL) != 0)
|
if (pthread_cond_init(&thrdpl->shared.cond_0, NULL) != 0)
|
||||||
{
|
{
|
||||||
pthread_mutex_destroy(&thrdpl->shared.mutex);
|
pthread_mutex_destroy(&thrdpl->shared.mutex);
|
||||||
@ -260,7 +280,7 @@ void slunkcrypt_thrdpl_exec(thrdpl_t *const thrdpl, uint8_t *const buffer, const
|
|||||||
thrdpl->shared.buffer = buffer;
|
thrdpl->shared.buffer = buffer;
|
||||||
thrdpl->shared.length = length;
|
thrdpl->shared.length = length;
|
||||||
thrdpl->shared.remain = thrdpl->shared.thread_count;
|
thrdpl->shared.remain = thrdpl->shared.thread_count;
|
||||||
|
|
||||||
++thrdpl->shared.generation;
|
++thrdpl->shared.generation;
|
||||||
PTHRD_COND_BRDCST(&thrdpl->shared.cond_0);
|
PTHRD_COND_BRDCST(&thrdpl->shared.cond_0);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user