Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jstimpfle committed Aug 22, 2019
1 parent d17f8bf commit 0014a7f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
16 changes: 8 additions & 8 deletions include/astedit/filereadthread.h
Expand Up @@ -3,16 +3,16 @@

#include <astedit/filepositions.h> // FILEPOS

struct FilereadThreadCtx;
struct FilereadThreadHandle;

struct FilereadThreadCtx *run_file_read_thread(
struct FilereadThreadHandle *run_file_read_thread(
const char *filepath, void *param,
char *buffer, int bufferSize, int *bufferFill,
void(*prepare)(void *param, FILEPOS filesizeInBytes),
void(*finalize)(void *param),
int(*flush_buffer)(void *param));
int check_if_file_read_thread_has_exited(struct FilereadThreadCtx *ctx);
void wait_for_file_read_thread_to_end(struct FilereadThreadCtx *ctx);
void dispose_file_read_thread(struct FilereadThreadCtx *ctx); // requires terminated thread
void(*prepareFunc)(void *param, FILEPOS filesizeInBytes),
void(*finalizeFunc)(void *param),
int(*flushBufferFunc)(void *param));
int check_if_file_read_thread_has_exited(struct FilereadThreadHandle *handle);
void wait_for_file_read_thread_to_end(struct FilereadThreadHandle *handle);
void dispose_file_read_thread(struct FilereadThreadHandle *handle); // requires terminated thread

#endif
2 changes: 1 addition & 1 deletion include/astedit/textedit.h
Expand Up @@ -50,7 +50,7 @@ struct TextEdit {
int loadingBufferFill; // fill from start

Timer *loadingTimer;
struct FilereadThreadCtx *loadingFilereadThreadCtx;
struct FilereadThreadHandle *loadingThreadHandle;

int isVimodeActive;
struct ViState vistate;
Expand Down
96 changes: 51 additions & 45 deletions src/astedit/filereadthread-win32.c
Expand Up @@ -9,24 +9,33 @@

struct FilereadThreadCtx {
char *filepath;
HANDLE threadHandle;
/* additional user value that is handed to the flush_buffer() function below */
void *param;

/* Read buffer provided by the caller to the reader thread. */
char *buffer;
int bufferSize;
int *bufferFill;

void(*prepare)(void *param, FILEPOS filesizeInBytes);
void(*finalize)(void *param);
/* to flush the buffer completely. Return values is 0 for success
or -1 to indicate error (reader thread should terminate itself in this case */
int (*flush_buffer)(void *param);
/* Context provided by the caller to the reader thread.
Reader thread hands param to caller when calling back. */
void *param;

/* Callbacks */
void(*prepareFunc)(void *param, FILEPOS filesizeInBytes);
void(*finalizeFunc)(void *param);
/* to flush the buffer completely. Return value is 0 in case of success,
or -1 in case of error (=> reader thread should terminate itself) */
int (*flushBufferFunc)(void *param);

/* thread can report return status here, so we don't depend on OS facilities
which might be hard to use or have their own gotchas... */
int returnStatus;
};

struct FilereadThreadHandle {
struct FilereadThreadCtx ctx;
HANDLE threadHandle;
};

#include <sys/stat.h>
static void read_file_thread(struct FilereadThreadCtx *ctx)
{
Expand Down Expand Up @@ -54,7 +63,7 @@ static void read_file_thread(struct FilereadThreadCtx *ctx)
}
FILEPOS filesize = (FILEPOS) _buf.st_size;
ENSURE(filesize == _buf.st_size); // dirty (and probably technically invalid) way to check cast
ctx->prepare(ctx->param, filesize);
ctx->prepareFunc(ctx->param, filesize);
}

for (;;) {
Expand All @@ -68,18 +77,16 @@ static void read_file_thread(struct FilereadThreadCtx *ctx)
goto out3;
}

int bufferSize = ctx->bufferSize;
int bufferFill = *ctx->bufferFill;
size_t n = fread(ctx->buffer + bufferFill, 1, bufferSize - bufferFill, f);
size_t n = fread(ctx->buffer + *ctx->bufferFill, 1,
ctx->bufferSize - *ctx->bufferFill, f);

if (n == 0)
/* EOF. Ignore remaining undecoded bytes */
break;

bufferFill += (int) n;
*ctx->bufferFill = bufferFill;
*ctx->bufferFill += (int) n;

int r = (*ctx->flush_buffer)(ctx->param);
int r = (*ctx->flushBufferFunc)(ctx->param);

if (r == -1) {
returnStatus = -1;
Expand All @@ -93,7 +100,7 @@ static void read_file_thread(struct FilereadThreadCtx *ctx)
}

out3:
ctx->finalize(ctx->param);
ctx->finalizeFunc(ctx->param);
out2:
fclose(f);
out1:
Expand All @@ -108,57 +115,56 @@ static DWORD WINAPI read_file_thread_adapter(LPVOID param)
}


struct FilereadThreadCtx *run_file_read_thread(
struct FilereadThreadHandle *run_file_read_thread(
const char *filepath, void *param,
char *buffer, int bufferSize, int *bufferFill,
void (*prepare)(void *param, FILEPOS filesizeInBytes),
void (*finalize)(void *param),
int (*flush_buffer)(void *param))
void (*prepareFunc)(void *param, FILEPOS filesizeInBytes),
void (*finalizeFunc)(void *param),
int (*flushBufferFunc)(void *param))
{
struct FilereadThreadCtx *ctx;
ALLOC_MEMORY(&ctx, 1);
struct FilereadThreadHandle *handle;
ALLOC_MEMORY(&handle, 1);

{
int filepathLen = (int) strlen(filepath);
ALLOC_MEMORY(&ctx->filepath, filepathLen + 1);
COPY_ARRAY(ctx->filepath, filepath, filepathLen + 1);
ALLOC_MEMORY(&handle->ctx.filepath, filepathLen + 1);
COPY_ARRAY(handle->ctx.filepath, filepath, filepathLen + 1);
}

ctx->param = param;
ctx->buffer = buffer;
ctx->bufferSize = bufferSize;
ctx->bufferFill = bufferFill;
ctx->prepare = prepare;
ctx->finalize = finalize;
ctx->flush_buffer = flush_buffer;

ctx->threadHandle = CreateThread(NULL, 0, &read_file_thread_adapter, ctx, 0, NULL);
if (ctx->threadHandle == NULL)
handle->ctx.param = param;
handle->ctx.buffer = buffer;
handle->ctx.bufferSize = bufferSize;
handle->ctx.bufferFill = bufferFill;
handle->ctx.prepareFunc = prepareFunc;
handle->ctx.finalizeFunc = finalizeFunc;
handle->ctx.flushBufferFunc = flushBufferFunc;

handle->threadHandle = CreateThread(NULL, 0, &read_file_thread_adapter, &handle->ctx, 0, NULL);
if (handle->threadHandle == NULL)
fatalf("Failed to create thread\n");

return ctx;
return handle;
}

int check_if_file_read_thread_has_exited(struct FilereadThreadCtx *ctx)
int check_if_file_read_thread_has_exited(struct FilereadThreadHandle *handle)
{
DWORD exitCode;
BOOL ret = GetExitCodeThread(ctx->threadHandle, &exitCode);
BOOL ret = GetExitCodeThread(handle->threadHandle, &exitCode);
ENSURE(ret != 0);
UNUSED(ret);
return exitCode != STILL_ACTIVE;
}

void wait_for_file_read_thread_to_end(struct FilereadThreadCtx *ctx)
void wait_for_file_read_thread_to_end(struct FilereadThreadHandle *handle)
{
WaitForSingleObject(ctx->threadHandle, INFINITE);
WaitForSingleObject(handle->threadHandle, INFINITE);
}

void dispose_file_read_thread(struct FilereadThreadCtx *ctx)
void dispose_file_read_thread(struct FilereadThreadHandle *handle)
{
ENSURE(check_if_file_read_thread_has_exited(ctx));
BOOL ret = CloseHandle(ctx->threadHandle);
ENSURE(check_if_file_read_thread_has_exited(handle));
BOOL ret = CloseHandle(handle->threadHandle);
ENSURE(ret != 0);
UNUSED(ret);
FREE_MEMORY(&ctx->filepath);
FREE_MEMORY(&ctx);
FREE_MEMORY(&handle->ctx.filepath);
FREE_MEMORY(&handle);
}

0 comments on commit 0014a7f

Please sign in to comment.