[examples] add output and combining to multi threaded parse example

This commit is contained in:
Allen Webster
2021-10-10 10:53:59 -07:00
parent 93ac2b4bae
commit f07b74da5f
3 changed files with 77 additions and 18 deletions
+35 -9
View File
@@ -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);
}
}
+41 -9
View File
@@ -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)
{
+1
View File
@@ -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