diff --git a/src/cpu/processor.cc b/src/cpu/processor.cc index a627a452b..a7331456d 100644 --- a/src/cpu/processor.cc +++ b/src/cpu/processor.cc @@ -27,20 +27,42 @@ using namespace xe::cpu; using namespace xe::kernel; +namespace { + void InitializeIfNeeded(); + void CleanupOnShutdown(); + + void InitializeIfNeeded() { + static bool has_initialized = false; + if (has_initialized) { + return; + } + has_initialized = true; + + // TODO(benvanik): only do this once + LLVMLinkInInterpreter(); + LLVMLinkInJIT(); + InitializeNativeTarget(); + + // TODO(benvanik): only do this once + codegen::RegisterEmitCategoryALU(); + codegen::RegisterEmitCategoryControl(); + codegen::RegisterEmitCategoryFPU(); + codegen::RegisterEmitCategoryMemory(); + + atexit(CleanupOnShutdown); + } + + void CleanupOnShutdown() { + llvm_shutdown(); + } +} + + Processor::Processor(xe_pal_ref pal, xe_memory_ref memory) { pal_ = xe_pal_retain(pal); memory_ = xe_memory_retain(memory); - // TODO(benvanik): only do this once - LLVMLinkInInterpreter(); - LLVMLinkInJIT(); - InitializeNativeTarget(); - - // TODO(benvanik): only do this once - codegen::RegisterEmitCategoryALU(); - codegen::RegisterEmitCategoryControl(); - codegen::RegisterEmitCategoryFPU(); - codegen::RegisterEmitCategoryMemory(); + InitializeIfNeeded(); } Processor::~Processor() { @@ -51,7 +73,6 @@ Processor::~Processor() { } engine_.reset(); - llvm_shutdown(); xe_memory_release(memory_); xe_pal_release(pal_);