From f07b74da5fad384e34454e446e0c4ac32dec686b Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sun, 10 Oct 2021 10:53:59 -0700 Subject: [PATCH] [examples] add output and combining to multi threaded parse example --- examples/integration/multi_threaded.c | 44 ++++++++++++++++++----- source/md.c | 50 ++++++++++++++++++++++----- source/md.h | 1 + 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/examples/integration/multi_threaded.c b/examples/integration/multi_threaded.c index d9acecc..ff6446e 100644 --- a/examples/integration/multi_threaded.c +++ b/examples/integration/multi_threaded.c @@ -49,14 +49,8 @@ parse_worker_loop(ThreadData *thread_data) } MD_String8 file_name = MD_S8CString(task->tasks[task_index]); MD_ParseResult parse = MD_ParseWholeFile(thread_data->arena, file_name); - if (parse.errors.first != 0) - { - MD_MessageListConcat(&thread_data->errors, &parse.errors); - } - else - { - MD_PushNewReference(thread_data->arena, thread_data->list, parse.node); - } + MD_MessageListConcat(&thread_data->errors, &parse.errors); + MD_PushNewReference(thread_data->arena, thread_data->list, parse.node); } atomic_inc_u64(&task->thread_counter); @@ -144,5 +138,37 @@ main(int argc, char **argv) #endif } - // TODO(allen): combine results + // print results + for (int i = 0; i < THREAD_COUNT; i += 1) + { + fprintf(stdout, "on thread %d:\n", i); + + // print the name of each root + for (MD_EachNode(root_it, threads[i].list->first_child)) + { + MD_Node *root = MD_ResolveNodeFromReference(root_it); + fprintf(stdout, "%.*s\n", MD_S8VArg(root->string)); + } + + // print the errors from this thread + MD_MessageList errors = threads[i].errors; + for (MD_Message *message = errors.first; + message != 0; + message = message->next) + { + MD_CodeLoc loc = MD_CodeLocFromNode(message->node); + MD_PrintMessage(stdout, loc, message->kind, message->string); + } + } + + // combine results + MD_Arena *arena = threads[0].arena; + MD_Node *list = threads[0].list; + MD_MessageList errors = threads[0].errors; + for (int i = 1; i < THREAD_COUNT; i += 1) + { + MD_ArenaDefaultAbsorb(arena, threads[i].arena); + MD_ListConcatInPlace(list, threads[i].list); + MD_MessageListConcat(&errors, &threads[i].errors); + } } diff --git a/source/md.c b/source/md.c index 377ed1b..f468573 100644 --- a/source/md.c +++ b/source/md.c @@ -597,7 +597,20 @@ MD_ArenaDefaultSetAutoAlign(MD_ArenaDefault *arena, MD_u64 align) arena->align = align; } -// TODO(allen): Arena absorb +static void +MD_ArenaDefaultAbsorb(MD_ArenaDefault *arena, MD_ArenaDefault *sub_arena) +{ + MD_ArenaDefault *current = arena->current; + MD_u64 base_pos_shift = current->base_pos + current->cap; + for (MD_ArenaDefault *node = sub_arena->current; + node != 0; + node = node->prev) + { + node->base_pos += base_pos_shift; + } + sub_arena->prev = arena->current; + arena->current = sub_arena->current; +} #endif @@ -2926,9 +2939,9 @@ MD_MessageListPush(MD_MessageList *list, MD_Message *message) MD_FUNCTION void MD_MessageListConcat(MD_MessageList *list, MD_MessageList *to_push) { - if(list->last) + if(to_push->node_count != 0) { - if(to_push->node_count != 0) + if(list->last != 0) { list->last->next = to_push->first; list->last = to_push->last; @@ -2938,12 +2951,12 @@ MD_MessageListConcat(MD_MessageList *list, MD_MessageList *to_push) list->max_message_kind = to_push->max_message_kind; } } + else + { + *list = *to_push; + } + MD_MemoryZeroStruct(to_push); } - else - { - *list = *to_push; - } - MD_MemoryZeroStruct(to_push); } //~ Location Conversions @@ -2996,7 +3009,7 @@ MD_CodeLocFromNode(MD_Node *node) MD_FUNCTION MD_b32 MD_NodeIsNil(MD_Node *node) { - return node == 0 || node == &_md_nil_node || node->kind == MD_NodeKind_Nil; + return(node == 0 || node == &_md_nil_node || node->kind == MD_NodeKind_Nil); } MD_FUNCTION MD_Node * @@ -3045,6 +3058,25 @@ MD_MakeList(MD_Arena *arena) return(result); } +MD_FUNCTION void +MD_ListConcatInPlace(MD_Node *list, MD_Node *to_push) +{ + if (!MD_NodeIsNil(to_push->first_child)) + { + if (!MD_NodeIsNil(list->first_child)) + { + list->last_child->next = to_push->first_child; + list->last_child = to_push->last_child; + } + else + { + list->first_child = to_push->first_child; + list->last_child = to_push->last_child; + } + to_push->first_child = to_push->last_child = MD_NilNode(); + } +} + MD_FUNCTION MD_Node* MD_PushNewReference(MD_Arena *arena, MD_Node *list, MD_Node *target) { diff --git a/source/md.h b/source/md.h index 7045ba6..6b358d6 100644 --- a/source/md.h +++ b/source/md.h @@ -1085,6 +1085,7 @@ MD_FUNCTION void MD_PushChild(MD_Node *parent, MD_Node *new_child); MD_FUNCTION void MD_PushTag(MD_Node *node, MD_Node *tag); MD_FUNCTION MD_Node *MD_MakeList(MD_Arena *arena); +MD_FUNCTION void MD_ListConcatInPlace(MD_Node *list, MD_Node *to_push); MD_FUNCTION MD_Node *MD_PushNewReference(MD_Arena *arena, MD_Node *list, MD_Node *target); //~ Introspection Helpers