-
Notifications
You must be signed in to change notification settings - Fork 617
🏗️ Refactor Windows compatibility #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
🏗️ Refactor Windows compatibility #423
Conversation
- Updated conditional compilation for Windows to exclude MinGW and Cygwin.
- Introduced a compatibility layer for sockets and threading for MinGW.
- Added os_getrandom implementation for portable random byte generation.
- Refactored mutex and thread handling to use pthreads in MinGW.
- Improved code organization and readability across multiple files.
- Ensured consistent handling of random number generation across platforms.
- Updated conditional compilation for Windows to exclude MinGW and Cygwin. - Introduced a compatibility layer for sockets and threading for MinGW. - Added os_getrandom implementation for portable random byte generation. - Refactored mutex and thread handling to use pthreads in MinGW. - Improved code organization and readability across multiple files. - Ensured consistent handling of random number generation across platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces a comprehensive compatibility layer for MinGW and Windows builds, refactoring platform-specific code to support cross-platform compilation. The main changes include introducing pthread-compatible wrappers for Windows threading APIs, abstracting socket operations, and providing portable random number generation.
Key changes:
- New compatibility layer files (
win_thread_compat.{h,c},win_compat.{h,c},os_random.{h,c}) that provide abstraction for threading, sockets, and random number generation - Updated conditional compilation macros throughout the codebase to explicitly exclude MinGW/Cygwin from native Windows code paths
- Refactored random number generation to use a unified
os_getrandom()API across all platforms
Reviewed changes
Copilot reviewed 21 out of 22 changed files in this pull request and generated 16 comments.
Show a summary per file
| File | Description |
|---|---|
| win_thread_compat.h | Header defining pthread compatibility layer for MinGW, mapping WinAPI thread/mutex functions to pthreads |
| win_thread_compat.c | Implementation of pthread compatibility wrappers for CreateThread, CreateMutex, WaitForSingleObject, etc. |
| win_compat.h | Header for socket compatibility layer with cross-platform socket initialization |
| win_compat.c | Implementation of socket wrappers (WSAStartup/closesocket on Windows, standard POSIX otherwise) |
| os_random.h | Header for portable random byte generation API |
| os_random.c | Implementation using BCryptGenRandom on Windows and /dev/urandom on POSIX systems |
| secp256k1/SECP256K1.cpp | Updated __declspec(align) conditionals to exclude MinGW |
| secp256k1/Random.cpp | Refactored to use os_getrandom() API and updated conditionals |
| secp256k1/Int.cpp | Updated format specifiers and conditionals for MinGW compatibility |
| rmd160/rmd160.h | Updated conditionals to exclude MinGW from Windows-specific paths |
| oldbloom/oldbloom.h | Added pthread.h include for non-Windows platforms and updated conditionals |
| oldbloom/bloom.cpp | Updated mutex usage conditionals and fixed indentation |
| keyhunt_legacy.cpp | Mass update of conditional compilation macros and includes for compatibility layer |
| keyhunt.cpp | Extensive refactoring of conditionals, random generation, and format specifiers for cross-platform support |
| hash/sha256_sse.cpp | Updated SSE intrinsics access to handle MinGW's GCC-based compilation |
| hash/ripemd160_sse.cpp | Updated SSE intrinsics and conditionals for MinGW compatibility |
| gmp256k1/Random.cpp | Simplified random generation to use os_getrandom() wrapper |
| bsgsd.cpp | Updated socket operations and random generation for compatibility layer |
| bloom/bloom.h | Updated Windows header includes to exclude MinGW |
| base58/base58.c | Updated network header selection to use Winsock on all Windows platforms |
| Makefile | Added compilation rules for compatibility layer and new MinGW target |
| .gitignore | Added .specstory/ directory to ignore list |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) | ||
| tid[j] = CreateThread(NULL, 0, thread_bPload, (void*) &bPload_temp_ptr[j], 0, &s); | ||
| #else | ||
| s = pthread_create(&tid[j],NULL,thread_bPload,(void*) &bPload_temp_ptr[j]); | ||
| pthread_detach(tid[j]); | ||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: preprocessor directives have mixed leading whitespace.
| uint32_t threadid; | ||
| uint64_t from; | ||
| uint64_t to; | ||
| int osr = os_getrandom(&rseedvalue, sizeof(unsigned long)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line appears to be a misplaced code fragment. It looks like a variable initialization int osr = os_getrandom(&rseedvalue, sizeof(unsigned long)); that should not be inside the struct definition. This will cause a compilation error as struct members cannot have code statements like function calls at the point of declaration.
| int osr = os_getrandom(&rseedvalue, sizeof(unsigned long)); | |
| int osr; |
| fflush(stdout); | ||
| THREADOUTPUT = 0; | ||
| #ifdef _WIN64 | ||
| #if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent conditional compilation: this line uses #if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__) while line 2241 uses #if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__), but line 2228 should include && !defined(__CYGWIN__) for consistency with the pattern used throughout the rest of the file (see lines 2176, 2241, etc.).
| #if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__) | |
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) |
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) | ||
| bloom_bPx3rd_mutex = (HANDLE*) calloc(256,sizeof(HANDLE)); | ||
| #else | ||
| bloom_bPx3rd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t)); | ||
| bloom_bPx3rd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: similar to lines 1238-1241, these lines use leading tabs that don't match the surrounding code style.
| bloom_bPx3rd_mutex[i] = CreateMutex(NULL, FALSE, NULL); | ||
| #else | ||
| pthread_mutex_init(&bloom_bPx3rd_mutex[i],NULL); | ||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: preprocessor directives have mixed indentation that doesn't align with the surrounding code.
| #endif | |
| #endif |
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) | ||
| bloom_bPx2nd_mutex = (HANDLE*) calloc(256,sizeof(HANDLE)); | ||
| #else | ||
| bloom_bPx2nd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t)); | ||
| bloom_bPx2nd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: these lines use leading tabs instead of the consistent indentation pattern used in surrounding code. Lines 1239 and 1241 should align with the surrounding context.
| #else | ||
| pthread_mutex_init(&bPload_mutex[j],NULL); | ||
| #endif | ||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: these lines have unusual leading whitespace that doesn't match the surrounding code pattern.
| #endif | |
| #endif |
| fprintf(stderr,"[E] Error os_getrandom() ?\n"); | ||
| exit(EXIT_FAILURE); | ||
| rseed(clock() + time(NULL) + rand()*rand()); | ||
| rseed(clock() + time(NULL)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unreachable code: line 469 calls exit(EXIT_FAILURE) but line 470 has rseed(clock() + time(NULL)) after it. This line will never execute because the program exits on line 469. This appears to be leftover code that should have been removed.
| rseed(clock() + time(NULL)); |
| free(h); | ||
| return 0; | ||
| } else if (h->type == 1) { | ||
| if(!h->detached) pthread_detach(h->u.thread); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential double-detach issue: if CloseHandle is called on a thread that has already been joined via WaitForSingleObject, calling pthread_detach on an already-joined thread is undefined behavior. The code should track whether the thread has been joined and only detach if it hasn't been.
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) | ||
| bloom_bPx2nd_mutex[i] = CreateMutex(NULL, FALSE, NULL); | ||
| #else | ||
| pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL); | ||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: the preprocessor directives and their content have mixed indentation (lines 1250-1254). The indentation should be consistent with surrounding code blocks.
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) | |
| bloom_bPx2nd_mutex[i] = CreateMutex(NULL, FALSE, NULL); | |
| #else | |
| pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL); | |
| #endif | |
| #if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) | |
| bloom_bPx2nd_mutex[i] = CreateMutex(NULL, FALSE, NULL); | |
| #else | |
| pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL); | |
| #endif |
…end on external libraries.
…to avoid DLL dependencies.