Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 1 | //===--- ThreadSafeModuleTest.cpp - Test basic use of ThreadSafeModule ----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | #include <atomic> |
| 13 | #include <future> |
| 14 | #include <thread> |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::orc; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(ThreadSafeModuleTest, ContextWhollyOwnedByOneModule) { |
| 22 | // Test that ownership of a context can be transferred to a single |
| 23 | // ThreadSafeModule. |
| 24 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 25 | auto M = llvm::make_unique<Module>("M", *TSCtx.getContext()); |
| 26 | ThreadSafeModule TSM(std::move(M), std::move(TSCtx)); |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | TEST(ThreadSafeModuleTest, ContextOwnershipSharedByTwoModules) { |
| 30 | // Test that ownership of a context can be shared between more than one |
| 31 | // ThreadSafeModule. |
| 32 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
| 33 | |
Lang Hames | 4328ea3 | 2018-09-28 01:41:33 +0000 | [diff] [blame] | 34 | auto M1 = llvm::make_unique<Module>("M1", *TSCtx.getContext()); |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 35 | ThreadSafeModule TSM1(std::move(M1), TSCtx); |
| 36 | |
Lang Hames | 4328ea3 | 2018-09-28 01:41:33 +0000 | [diff] [blame] | 37 | auto M2 = llvm::make_unique<Module>("M2", *TSCtx.getContext()); |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 38 | ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx)); |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TEST(ThreadSafeModuleTest, ContextOwnershipSharedWithClient) { |
| 42 | // Test that ownership of a context can be shared with a client-held |
| 43 | // ThreadSafeContext so that it can be re-used for new modules. |
| 44 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
| 45 | |
| 46 | { |
| 47 | // Create and destroy a module. |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 48 | auto M1 = llvm::make_unique<Module>("M1", *TSCtx.getContext()); |
| 49 | ThreadSafeModule TSM1(std::move(M1), TSCtx); |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Verify that the context is still available for re-use. |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 53 | auto M2 = llvm::make_unique<Module>("M2", *TSCtx.getContext()); |
| 54 | ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx)); |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST(ThreadSafeModuleTest, ThreadSafeModuleMoveAssignment) { |
| 58 | // Move assignment needs to move the module before the context (opposite |
| 59 | // to the field order) to ensure that overwriting with an empty |
| 60 | // ThreadSafeModule does not destroy the context early. |
| 61 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 62 | auto M = llvm::make_unique<Module>("M", *TSCtx.getContext()); |
| 63 | ThreadSafeModule TSM(std::move(M), std::move(TSCtx)); |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 64 | TSM = ThreadSafeModule(); |
| 65 | } |
| 66 | |
| 67 | TEST(ThreadSafeModuleTest, BasicContextLockAPI) { |
| 68 | // Test that basic lock API calls work. |
| 69 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
Lang Hames | 4328ea3 | 2018-09-28 01:41:33 +0000 | [diff] [blame] | 70 | auto M = llvm::make_unique<Module>("M", *TSCtx.getContext()); |
Lang Hames | f1c9649 | 2018-09-27 02:09:37 +0000 | [diff] [blame] | 71 | ThreadSafeModule TSM(std::move(M), TSCtx); |
Lang Hames | 5ad09de | 2018-09-27 02:09:36 +0000 | [diff] [blame] | 72 | |
| 73 | { auto L = TSCtx.getLock(); } |
| 74 | |
| 75 | { auto L = TSM.getContextLock(); } |
| 76 | } |
| 77 | |
| 78 | TEST(ThreadSafeModuleTest, ContextLockPreservesContext) { |
| 79 | // Test that the existence of a context lock preserves the attached |
| 80 | // context. |
| 81 | // The trick to verify this is a bit of a hack: We attach a Module |
| 82 | // (without the ThreadSafeModule wrapper) to the context, then verify |
| 83 | // that this Module destructs safely (which it will not if its context |
| 84 | // has been destroyed) even though all references to the context have |
| 85 | // been thrown away (apart from the lock). |
| 86 | |
| 87 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
| 88 | auto L = TSCtx.getLock(); |
| 89 | auto &Ctx = *TSCtx.getContext(); |
| 90 | auto M = llvm::make_unique<Module>("M", Ctx); |
| 91 | TSCtx = ThreadSafeContext(); |
| 92 | } |
| 93 | |
| 94 | } // end anonymous namespace |