From 92a3e19cd9f4c684a461e9f6eddcf858e6b66e0d Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sun, 27 Jan 2013 19:33:57 -0800 Subject: [PATCH] Fixing to allow multiple executions/process. --- src/cpu/processor.cc | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) 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_);