1 //===--- ThreadSafeModuleTest.cpp - Test basic use of ThreadSafeModule ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
11 #include "gtest/gtest.h"
18 using namespace llvm::orc
;
22 TEST(ThreadSafeModuleTest
, ContextWhollyOwnedByOneModule
) {
23 // Test that ownership of a context can be transferred to a single
25 ThreadSafeContext
TSCtx(llvm::make_unique
<LLVMContext
>());
26 auto M
= llvm::make_unique
<Module
>("M", *TSCtx
.getContext());
27 ThreadSafeModule
TSM(std::move(M
), std::move(TSCtx
));
30 TEST(ThreadSafeModuleTest
, ContextOwnershipSharedByTwoModules
) {
31 // Test that ownership of a context can be shared between more than one
33 ThreadSafeContext
TSCtx(llvm::make_unique
<LLVMContext
>());
35 auto M1
= llvm::make_unique
<Module
>("M1", *TSCtx
.getContext());
36 ThreadSafeModule
TSM1(std::move(M1
), TSCtx
);
38 auto M2
= llvm::make_unique
<Module
>("M2", *TSCtx
.getContext());
39 ThreadSafeModule
TSM2(std::move(M2
), std::move(TSCtx
));
42 TEST(ThreadSafeModuleTest
, ContextOwnershipSharedWithClient
) {
43 // Test that ownership of a context can be shared with a client-held
44 // ThreadSafeContext so that it can be re-used for new modules.
45 ThreadSafeContext
TSCtx(llvm::make_unique
<LLVMContext
>());
48 // Create and destroy a module.
49 auto M1
= llvm::make_unique
<Module
>("M1", *TSCtx
.getContext());
50 ThreadSafeModule
TSM1(std::move(M1
), TSCtx
);
53 // Verify that the context is still available for re-use.
54 auto M2
= llvm::make_unique
<Module
>("M2", *TSCtx
.getContext());
55 ThreadSafeModule
TSM2(std::move(M2
), std::move(TSCtx
));
58 TEST(ThreadSafeModuleTest
, ThreadSafeModuleMoveAssignment
) {
59 // Move assignment needs to move the module before the context (opposite
60 // to the field order) to ensure that overwriting with an empty
61 // ThreadSafeModule does not destroy the context early.
62 ThreadSafeContext
TSCtx(llvm::make_unique
<LLVMContext
>());
63 auto M
= llvm::make_unique
<Module
>("M", *TSCtx
.getContext());
64 ThreadSafeModule
TSM(std::move(M
), std::move(TSCtx
));
65 TSM
= ThreadSafeModule();
68 TEST(ThreadSafeModuleTest
, BasicContextLockAPI
) {
69 // Test that basic lock API calls work.
70 ThreadSafeContext
TSCtx(llvm::make_unique
<LLVMContext
>());
71 auto M
= llvm::make_unique
<Module
>("M", *TSCtx
.getContext());
72 ThreadSafeModule
TSM(std::move(M
), TSCtx
);
74 { auto L
= TSCtx
.getLock(); }
76 { auto L
= TSM
.getContextLock(); }
79 TEST(ThreadSafeModuleTest
, ContextLockPreservesContext
) {
80 // Test that the existence of a context lock preserves the attached
82 // The trick to verify this is a bit of a hack: We attach a Module
83 // (without the ThreadSafeModule wrapper) to the context, then verify
84 // that this Module destructs safely (which it will not if its context
85 // has been destroyed) even though all references to the context have
86 // been thrown away (apart from the lock).
88 ThreadSafeContext
TSCtx(llvm::make_unique
<LLVMContext
>());
89 auto L
= TSCtx
.getLock();
90 auto &Ctx
= *TSCtx
.getContext();
91 auto M
= llvm::make_unique
<Module
>("M", Ctx
);
92 TSCtx
= ThreadSafeContext();
95 } // end anonymous namespace