diff --git a/find_compactbiomes.c b/find_compactbiomes.c index b55cace..6746faf 100644 --- a/find_compactbiomes.c +++ b/find_compactbiomes.c @@ -8,6 +8,7 @@ #include "finders.h" #include "generator.h" + #define SEED_BUF_LEN 0x10000 @@ -16,7 +17,12 @@ struct compactinfo_t int64_t seedStart, seedEnd; }; -void *searchCompactBiomesThread(void *data) + +#ifdef USE_PTHREAD +static void *searchCompactBiomesThread(void *data) +#else +static DWORD WINAPI searchCompactBiomesThread(LPVOID data) +#endif { struct compactinfo_t info = *(struct compactinfo_t *)data; @@ -26,15 +32,15 @@ void *searchCompactBiomesThread(void *data) LayerStack g = setupGenerator(MC_1_7); int *cache = allocCache(&g.layers[L_BIOME_256], 8, 8); - for(s = info.seedStart; s < info.seedEnd; s += SEED_BUF_LEN) + for (s = info.seedStart; s < info.seedEnd; s += SEED_BUF_LEN) { - if(s + SEED_BUF_LEN > info.seedEnd) + if (s + SEED_BUF_LEN > info.seedEnd) scnt = info.seedEnd - s; else scnt = SEED_BUF_LEN; - for(i = 0; i < scnt; i++) + for (i = 0; i < scnt; i++) { seeds[i] = s + i; } @@ -44,7 +50,7 @@ void *searchCompactBiomesThread(void *data) // The biomes really shouldn't be further out than 1024 blocks. scnt = filterAllMajorBiomes(&g, cache, seeds, seeds, scnt, -4, -4, 8, 8); - for(i = 0; i < scnt; i++) + for (i = 0; i < scnt; i++) { printf("%"PRId64"\n", seeds[i]); } @@ -53,7 +59,10 @@ void *searchCompactBiomesThread(void *data) free(seeds); - return NULL; +#ifdef USE_PTHREAD + pthread_exit(NULL); +#endif + return 0; } @@ -64,16 +73,16 @@ int main(int argc, char *argv[]) int64_t seedStart, seedEnd; unsigned int threads, t; - if(argc <= 1 || sscanf(argv[1], "%"PRId64, &seedStart) != 1) seedStart = 0; - if(argc <= 2 || sscanf(argv[2], "%"PRId64, &seedEnd) != 1) seedEnd = 100000000LL; - if(argc <= 3 || sscanf(argv[3], "%u", &threads) != 1) threads = 1; + if (argc <= 1 || sscanf(argv[1], "%"PRId64, &seedStart) != 1) seedStart = 0; + if (argc <= 2 || sscanf(argv[2], "%"PRId64, &seedEnd) != 1) seedEnd = 100000000LL; + if (argc <= 3 || sscanf(argv[3], "%u", &threads) != 1) threads = 1; printf("Starting search through seeds %"PRId64 " to %"PRId64", using %u threads.\n", seedStart, seedEnd, threads); - pthread_t threadID[threads]; + thread_id_t threadID[threads]; struct compactinfo_t info[threads]; - for(t = 0; t < threads; t++) + for (t = 0; t < threads; t++) { int64_t seedCnt = (seedEnd - seedStart) / threads; info[t].seedStart = seedStart + seedCnt * t; @@ -81,16 +90,29 @@ int main(int argc, char *argv[]) } info[threads-1].seedEnd = seedEnd; - for(t = 0; t < threads; t++) +#ifdef USE_PTHREAD + + for (t = 0; t < threads; t++) { pthread_create(&threadID[t], NULL, searchCompactBiomesThread, (void*)&info[t]); } - for(t = 0; t < threads; t++) + for (t = 0; t < threads; t++) { pthread_join(threadID[t], NULL); } +#else + + for (t = 0; t < threads; t++) + { + threadID[t] = CreateThread(NULL, 0, searchCompactBiomesThread, (LPVOID)&info[t], 0, NULL); + } + + WaitForMultipleObjects(threads, threadID, TRUE, INFINITE); + +#endif + return 0; } diff --git a/finders.c b/finders.c index 57a17b3..6fcff51 100644 --- a/finders.c +++ b/finders.c @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -488,8 +487,11 @@ int countBlocksInSpawnRange(Pos p[4], const int ax, const int ay, const int az) return best; } - +#ifdef USE_PTHREAD static void *search4QuadBasesThread(void *data) +#else +static DWORD WINAPI search4QuadBasesThread(LPVOID data) +#endif { quad_threadinfo_t info = *(quad_threadinfo_t*)data; @@ -595,14 +597,17 @@ static void *search4QuadBasesThread(void *data) fclose(fp); free(lowerBits); - return NULL; +#ifdef USE_PTHREAD + pthread_exit(NULL); +#endif + return 0; } void search4QuadBases(const char *fnam, const int threads, const StructureConfig structureConfig, const int quality) { - pthread_t threadID[threads]; + thread_id_t threadID[threads]; quad_threadinfo_t info[threads]; int64_t t; @@ -616,6 +621,9 @@ void search4QuadBases(const char *fnam, const int threads, info[t].sconf = structureConfig; } + +#ifdef USE_PTHREAD + for (t = 0; t < threads; t++) { pthread_create(&threadID[t], NULL, search4QuadBasesThread, (void*)&info[t]); @@ -626,6 +634,19 @@ void search4QuadBases(const char *fnam, const int threads, pthread_join(threadID[t], NULL); } +#else + + for (t = 0; t < threads; t++) + { + threadID[t] = CreateThread(NULL, 0, search4QuadBasesThread, (LPVOID)&info[t], 0, NULL); + } + + WaitForMultipleObjects(threads, threadID, TRUE, INFINITE); + +#endif + + + // merge thread parts char fnamThread[256]; diff --git a/finders.h b/finders.h index 145a9c0..e02a68f 100644 --- a/finders.h +++ b/finders.h @@ -4,9 +4,21 @@ #include "generator.h" #include #include -#include #include +#ifdef _WIN32 +#include + +typedef HANDLE thread_id_t; + +#else +#define USE_PTHREAD +#include + +typedef pthread_t thread_id_t; + +#endif + #define SEED_BASE_MAX (1LL << 48) #define PI 3.141592653589793 diff --git a/layers.h b/layers.h index 4981a05..22e09e4 100644 --- a/layers.h +++ b/layers.h @@ -46,7 +46,7 @@ enum BiomeType enum BiomeTempCategory { - Oceanic, Warm, Lush, Cold, Freezing, Unknown + Oceanic, Warm, Lush, Cold, Freezing, UnknownTemp }; diff --git a/makefile b/makefile index 91f108c..c6efeba 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,13 @@ CC = gcc -LDFLAGS = -lm -pthread +override LDFLAGS = -lm override CFLAGS += -Wall -fwrapv -march=native +ifeq ($(OS),Windows_NT) + override CFLAGS += -D_WIN32 +else + override LDFLAGS += -lX11 -pthread +endif + .PHONY : all debug clean all: CFLAGS += -O3