OS X compatibility

This commit is contained in:
Biryuzovye Kleshni 2018-06-25 07:51:39 +00:00
parent 4f9274a5cc
commit cac0237ae0
6 changed files with 175 additions and 172 deletions

View File

@ -1,6 +1,12 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#ifdef _WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __attribute__ ((visibility("default")))
#endif
extern volatile int run; extern volatile int run;
#define SEED_LENGTH (32 + 8) #define SEED_LENGTH (32 + 8)

View File

@ -1,4 +0,0 @@
{
global: fastsolver_*;
local: *;
};

View File

@ -1,8 +1,9 @@
CFLAGS += -std=gnu99 -Wall -Wextra -pedantic -O3 -fPIC CFLAGS += -std=gnu99 -Wall -Wextra -pedantic -O3 -fPIC -fvisibility=hidden
LDFLAGS += -shared -lpthread -lcrypto -Wl,-version-script=main.map LDFLAGS += -shared
LDLIBS = -lpthread -lcrypto
libfastsolver.so: main.map common.o pthread.o libfastsolver.so: common.o pthread.o
$(CC) $(LDFLAGS) -o $@ common.o pthread.o $(CC) $(LDFLAGS) -o $@ common.o pthread.o $(LDLIBS)
common.o: common.h common.c common.o: common.h common.c
pthread.o: common.h pthread.c pthread.o: common.h pthread.c

View File

@ -1 +1 @@
/Ox /MD common.c winapi.c /link /DLL /OUT:libfastsolver.dll /EXPORT:fastsolver_add /EXPORT:fastsolver_remove /EXPORT:fastsolver_search libcrypto.lib /Ox /MD common.c winapi.c /link /DLL /OUT:libfastsolver.dll libcrypto.lib

View File

@ -104,7 +104,7 @@ static int initialize(void) {
error_lock: return 0; error_lock: return 0;
} }
size_t fastsolver_add(void) { EXPORT size_t fastsolver_add(void) {
#ifdef SCHED_IDLE #ifdef SCHED_IDLE
int policy = SCHED_IDLE; int policy = SCHED_IDLE;
#else #else
@ -135,7 +135,7 @@ size_t fastsolver_add(void) {
return threads_count; return threads_count;
} }
size_t fastsolver_remove(size_t count) { EXPORT size_t fastsolver_remove(size_t count) {
size_t i; size_t i;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
@ -154,7 +154,7 @@ size_t fastsolver_remove(size_t count) {
return threads_count; return threads_count;
} }
int fastsolver_search( EXPORT int fastsolver_search(
char *local_nonce, char *local_nonce,
unsigned long long *local_iterations_count, unsigned long long *local_iterations_count,
const char *local_initial_hash, const char *local_initial_hash,

View File

@ -1,160 +1,160 @@
#include <string.h> #include <string.h>
#include <Windows.h> #include <Windows.h>
#include "common.h" #include "common.h"
static int initialized; static int initialized;
#define MAXIMUM_THREADS_COUNT 4096 #define MAXIMUM_THREADS_COUNT 4096
static size_t threads_count; static size_t threads_count;
static HANDLE threads[MAXIMUM_THREADS_COUNT]; static HANDLE threads[MAXIMUM_THREADS_COUNT];
static CRITICAL_SECTION lock; static CRITICAL_SECTION lock;
static CONDITION_VARIABLE start = CONDITION_VARIABLE_INIT; static CONDITION_VARIABLE start = CONDITION_VARIABLE_INIT;
static CONDITION_VARIABLE done = CONDITION_VARIABLE_INIT; static CONDITION_VARIABLE done = CONDITION_VARIABLE_INIT;
static size_t running_threads_count; static size_t running_threads_count;
static int found; static int found;
static char best_nonce[8]; static char best_nonce[8];
static unsigned long long total_iterations_count; static unsigned long long total_iterations_count;
DWORD WINAPI thread_function(LPVOID argument) { DWORD WINAPI thread_function(LPVOID argument) {
size_t thread_number = (HANDLE *) argument - threads; size_t thread_number = (HANDLE *) argument - threads;
while (1) { while (1) {
char nonce[8]; char nonce[8];
unsigned long long iterations_count = 0; unsigned long long iterations_count = 0;
int result; int result;
EnterCriticalSection(&lock); EnterCriticalSection(&lock);
while (!run && threads_count > thread_number) { while (!run && threads_count > thread_number) {
SleepConditionVariableCS(&start, &lock, INFINITE); SleepConditionVariableCS(&start, &lock, INFINITE);
} }
if (threads_count <= thread_number) { if (threads_count <= thread_number) {
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
return 0; return 0;
} }
++running_threads_count; ++running_threads_count;
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
result = work(nonce, &iterations_count, thread_number); result = work(nonce, &iterations_count, thread_number);
EnterCriticalSection(&lock); EnterCriticalSection(&lock);
if (result == 1) { if (result == 1) {
found = 1; found = 1;
memcpy(best_nonce, nonce, 8); memcpy(best_nonce, nonce, 8);
} }
total_iterations_count += iterations_count; total_iterations_count += iterations_count;
run = 0; run = 0;
--running_threads_count; --running_threads_count;
WakeConditionVariable(&done); WakeConditionVariable(&done);
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
} }
} }
static int initialize(void) { static int initialize(void) {
if (initialized == 1) { if (initialized == 1) {
return 1; return 1;
} }
InitializeCriticalSection(&lock); InitializeCriticalSection(&lock);
initialized = 1; initialized = 1;
return 1; return 1;
} }
size_t fastsolver_add(void) { EXPORT size_t fastsolver_add(void) {
if (initialize() == 0) { if (initialize() == 0) {
return threads_count; return threads_count;
} }
EnterCriticalSection(&lock); EnterCriticalSection(&lock);
threads[threads_count] = CreateThread(NULL, 0, thread_function, &threads[threads_count], 0, NULL); threads[threads_count] = CreateThread(NULL, 0, thread_function, &threads[threads_count], 0, NULL);
if (threads[threads_count] == NULL) { if (threads[threads_count] == NULL) {
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
return threads_count; return threads_count;
} }
SetThreadPriority(threads[threads_count], THREAD_PRIORITY_IDLE); SetThreadPriority(threads[threads_count], THREAD_PRIORITY_IDLE);
++threads_count; ++threads_count;
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
return threads_count; return threads_count;
} }
size_t fastsolver_remove(size_t count) { EXPORT size_t fastsolver_remove(size_t count) {
size_t i; size_t i;
EnterCriticalSection(&lock); EnterCriticalSection(&lock);
threads_count -= count; threads_count -= count;
WakeAllConditionVariable(&start); WakeAllConditionVariable(&start);
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
WaitForMultipleObjects(count, threads + threads_count, TRUE, INFINITE); WaitForMultipleObjects(count, threads + threads_count, TRUE, INFINITE);
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
CloseHandle(threads[threads_count + i]); CloseHandle(threads[threads_count + i]);
} }
return threads_count; return threads_count;
} }
int fastsolver_search( EXPORT int fastsolver_search(
char *local_nonce, char *local_nonce,
unsigned long long *local_iterations_count, unsigned long long *local_iterations_count,
const char *local_initial_hash, const char *local_initial_hash,
unsigned long long local_target, unsigned long long local_target,
const char *local_seed, const char *local_seed,
unsigned long long timeout unsigned long long timeout
) { ) {
initial_hash = local_initial_hash; initial_hash = local_initial_hash;
target = local_target; target = local_target;
seed = local_seed; seed = local_seed;
found = 0; found = 0;
total_iterations_count = 0; total_iterations_count = 0;
EnterCriticalSection(&lock); EnterCriticalSection(&lock);
run = 1; run = 1;
WakeAllConditionVariable(&start); WakeAllConditionVariable(&start);
SleepConditionVariableCS(&done, &lock, timeout / 1000); SleepConditionVariableCS(&done, &lock, timeout / 1000);
run = 0; run = 0;
while (running_threads_count != 0) { while (running_threads_count != 0) {
SleepConditionVariableCS(&done, &lock, INFINITE); SleepConditionVariableCS(&done, &lock, INFINITE);
} }
LeaveCriticalSection(&lock); LeaveCriticalSection(&lock);
if (found) { if (found) {
memcpy(local_nonce, best_nonce, 8); memcpy(local_nonce, best_nonce, 8);
} }
*local_iterations_count = total_iterations_count; *local_iterations_count = total_iterations_count;
return found; return found;
} }