diff --git a/main.odin b/main.odin index 2b14dfd..a40a429 100644 --- a/main.odin +++ b/main.odin @@ -5,7 +5,10 @@ import "base:runtime" import "core:fmt" import "core:mem" import "core:os" +import "core:strconv" import "core:strings" +import "core:sync" +import "core:thread" import "core:time" // -------------------------------------------------------- @@ -242,18 +245,28 @@ CalcOggLength :: proc(data: []byte) -> (length: int, ok: bool) { EXTRACT_DIRECTORY :: "MolruExtract" -Extract :: proc(catalog: Catalog) { - memBuffer := make([]byte, 512 * mem.Megabyte, context.allocator) - defer delete(memBuffer, context.allocator) - - arena: mem.Arena - mem.arena_init(&arena, memBuffer) - fileBuffer := mem.arena_allocator(&arena) +ExtractTask :: proc(catalog: ^Catalog, mutex: ^sync.Mutex, id: int) { + arena: mem.Dynamic_Arena + mem.dynamic_arena_init(&arena, context.allocator, context.allocator, block_size=4*mem.Megabyte, alignment=4*mem.Megabyte) + defer mem.dynamic_arena_destroy(&arena) + fileBuffer := mem.dynamic_arena_allocator(&arena) + + for { + key: string + entry: CatalogEntry + + if sync.mutex_guard(mutex) { + if len(catalog) == 0 do break + for k, v in catalog { + key, entry = k, v + break + } + delete_key(catalog, key) + } - for key, entry in catalog { defer free_all(context.temp_allocator) - if verbose do fmt.println(key) + if verbose2 do fmt.printfln("[THREAD %02d] %s", id, key) molruPath := strings.concatenate({key, ".molru"}, context.temp_allocator) os.exists(molruPath) or_continue @@ -268,7 +281,7 @@ Extract :: proc(catalog: Catalog) { it := CreateIterator(molruFile) for found in GetData(&it) { name := i < len(entry) ? entry[i] : fmt.tprintf("unknown_%8x.%v", found.begin, it.type) - if verbose do fmt.printfln(" [%v: %8x-%8x] %s", it.type, found.begin, found.end, name) + if verbose do fmt.printfln("[THREAD %02d] [%v: %8x-%8x] %s", id, it.type, found.begin, found.end, name) extractFile := Assert(os.join_path({extractDir, name}, context.temp_allocator), "Fail to join path") Assert(os.write_entire_file(extractFile, molruFile[found.begin:found.end]), "Fail to save data") i += 1 @@ -277,13 +290,31 @@ Extract :: proc(catalog: Catalog) { } } +Extract :: proc(catalog: Catalog) { + catalog := catalog + + mutex: sync.Mutex + threads := make([]^thread.Thread, threadNum, context.allocator) + defer delete(threads, context.allocator) + + for i in 0..