Removed POSIX dependence.

This commit is contained in:
Cubitect 2018-08-01 17:51:17 +02:00
parent 5127be24a4
commit f3243ca3fc
5 changed files with 81 additions and 20 deletions

View File

@ -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;
}

View File

@ -2,7 +2,6 @@
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <math.h>
@ -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];

View File

@ -4,9 +4,21 @@
#include "generator.h"
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#ifdef _WIN32
#include <Windows.h>
typedef HANDLE thread_id_t;
#else
#define USE_PTHREAD
#include <pthread.h>
typedef pthread_t thread_id_t;
#endif
#define SEED_BASE_MAX (1LL << 48)
#define PI 3.141592653589793

View File

@ -46,7 +46,7 @@ enum BiomeType
enum BiomeTempCategory
{
Oceanic, Warm, Lush, Cold, Freezing, Unknown
Oceanic, Warm, Lush, Cold, Freezing, UnknownTemp
};

View File

@ -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