Skip to content

Commit

Permalink
use per-thread context
Browse files Browse the repository at this point in the history
  • Loading branch information
graphitemaster committed Aug 15, 2023
1 parent de1fe2f commit fff53f7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static Bool dump_ast(String pathname) {
if (work->error) {
goto L_error;
}
// dump(work->tree);
dump(work->tree);
}

result = true;
Expand Down
2 changes: 1 addition & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ static Expression *parse_directive_prefix(Parser *parser, Bool lhs) {
}
case DIRECTIVE_RELATIVE:
{
Expression *const base_type = parse_directive_call_expression(parser, SCLIT("relative"));
parse_directive_call_expression(parser, SCLIT("relative"));
Type *const pointer_type = parse_type(parser);
TRACE_LEAVE();
return RCAST(Expression *, tree_new_type_expression(parser->tree, pointer_type));
Expand Down
16 changes: 6 additions & 10 deletions src/sched_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ struct SchedAsync {
};

struct SchedAsyncWork {
Context context;
SchedAsync *sched;
void *data;
void (*func)(void *data, Context *context);
void (*dispose)(void *data, Context *context);
};

static void _sched_async_work_func(void *data) {
static void _sched_async_work_func(void *data, Context *context) {
SchedAsyncWork *work = RCAST(SchedAsyncWork *, data);
if (!setjmp(work->context.jmp)) {
work->func(work->data, &work->context);
if (!setjmp(context->jmp)) {
work->func(work->data, context);
}
if (work->dispose) {
work->dispose(work->data, &work->context);
work->dispose(work->data, context);
}
SchedAsync *sched = work->sched;
mutex_lock(&sched->mutex);
Expand All @@ -37,9 +36,8 @@ static void _sched_async_work_func(void *data) {
mutex_unlock(&sched->mutex);
}

static void _sched_async_work_dispose(void *data) {
SchedAsyncWork *work = RCAST(SchedAsyncWork *, data);
Allocator *allocator = &work->context.allocator;
static void _sched_async_work_dispose(void *data, Context *context) {
Allocator *allocator = &context->allocator;
allocator_deallocate(allocator, data);
}

Expand Down Expand Up @@ -85,8 +83,6 @@ static Bool sched_async_queue(void *ctx, void *data, void (*func)(void *data, Co
THROW(ERROR_OOM);
}

context_copy(&work->context, context);

work->sched = sched;
work->data = data;
work->func = func;
Expand Down
28 changes: 12 additions & 16 deletions src/threadpool.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include "threadpool.h"
#include "context.h"

struct ThreadPoolWork {
void (*function)(void *user);
void (*function)(void *user, Context *context);
void *user;
void (*dispose)(void *user);
void (*dispose)(void *user, Context *context);
};

static int threadpool_worker(void *user) {
ThreadPool *pool = CAST(ThreadPool*, user);

Context context;
context_init(&context, pool->context->allocator.ops->name);

for (;;) {
mutex_lock(&pool->mutex);
while (!pool->quit && array_size(pool->work) == 0) {
Expand All @@ -23,12 +28,14 @@ static int threadpool_worker(void *user) {
ThreadPoolWork work = pool->work[--meta->size];
mutex_unlock(&pool->mutex);

work.function(work.user);
work.function(work.user, &context);

if (work.dispose) {
work.dispose(work.user);
work.dispose(work.user, &context);
}
}

context_fini(&context);
}

Bool threadpool_init(ThreadPool *pool, Size n_threads, Context *context) {
Expand Down Expand Up @@ -63,22 +70,11 @@ void threadpool_fini(ThreadPool *pool) {

array_free(pool->threads);

mutex_lock(&pool->mutex);
const Size n_work = array_size(pool->work);
for (Size i = 0; i < n_work; i++) {
ThreadPoolWork *work = &pool->work[i];
if (work->dispose) {
work->dispose(work->user);
}
}
array_free(pool->work);
mutex_unlock(&pool->mutex);

cond_fini(&pool->cond);
mutex_fini(&pool->mutex);
}

void threadpool_queue(ThreadPool *pool, void (*function)(void*), void *user, void (*dispose)(void*)) {
void threadpool_queue(ThreadPool *pool, void (*function)(void*, Context*), void *user, void (*dispose)(void*, Context*)) {
mutex_lock(&pool->mutex);
const ThreadPoolWork work = LIT(ThreadPoolWork, function, user, dispose);
array_push(pool->work, work);
Expand Down
2 changes: 1 addition & 1 deletion src/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ struct ThreadPool {

Bool threadpool_init(ThreadPool *pool, Size n_threads, Context *context);
void threadpool_fini(ThreadPool *pool);
void threadpool_queue(ThreadPool *pool, void (*function)(void*), void *user, void (*dispose)(void*));
void threadpool_queue(ThreadPool *pool, void (*function)(void*, Context *context), void *user, void (*dispose)(void*, Context *context));

#endif // CODIN_THREADPOOL_H
5 changes: 4 additions & 1 deletion tests/main.odin
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package main
import "core:fmt"

main :: proc() {
fmt.printf("Hello, world!");
if true
{
fmt.printf("Hello, world!");
}
}

0 comments on commit fff53f7

Please sign in to comment.