ThreadSafeModuleTest.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===--- ThreadSafeModuleTest.cpp - Test basic use of ThreadSafeModule ----===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
  9. #include "gtest/gtest.h"
  10. #include <atomic>
  11. #include <future>
  12. #include <thread>
  13. using namespace llvm;
  14. using namespace llvm::orc;
  15. namespace {
  16. TEST(ThreadSafeModuleTest, ContextWhollyOwnedByOneModule) {
  17. // Test that ownership of a context can be transferred to a single
  18. // ThreadSafeModule.
  19. ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
  20. auto M = std::make_unique<Module>("M", *TSCtx.getContext());
  21. ThreadSafeModule TSM(std::move(M), std::move(TSCtx));
  22. }
  23. TEST(ThreadSafeModuleTest, ContextOwnershipSharedByTwoModules) {
  24. // Test that ownership of a context can be shared between more than one
  25. // ThreadSafeModule.
  26. ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
  27. auto M1 = std::make_unique<Module>("M1", *TSCtx.getContext());
  28. ThreadSafeModule TSM1(std::move(M1), TSCtx);
  29. auto M2 = std::make_unique<Module>("M2", *TSCtx.getContext());
  30. ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx));
  31. }
  32. TEST(ThreadSafeModuleTest, ContextOwnershipSharedWithClient) {
  33. // Test that ownership of a context can be shared with a client-held
  34. // ThreadSafeContext so that it can be re-used for new modules.
  35. ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
  36. {
  37. // Create and destroy a module.
  38. auto M1 = std::make_unique<Module>("M1", *TSCtx.getContext());
  39. ThreadSafeModule TSM1(std::move(M1), TSCtx);
  40. }
  41. // Verify that the context is still available for re-use.
  42. auto M2 = std::make_unique<Module>("M2", *TSCtx.getContext());
  43. ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx));
  44. }
  45. TEST(ThreadSafeModuleTest, ThreadSafeModuleMoveAssignment) {
  46. // Move assignment needs to move the module before the context (opposite
  47. // to the field order) to ensure that overwriting with an empty
  48. // ThreadSafeModule does not destroy the context early.
  49. ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
  50. auto M = std::make_unique<Module>("M", *TSCtx.getContext());
  51. ThreadSafeModule TSM(std::move(M), std::move(TSCtx));
  52. TSM = ThreadSafeModule();
  53. }
  54. TEST(ThreadSafeModuleTest, BasicContextLockAPI) {
  55. // Test that basic lock API calls work.
  56. ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
  57. auto M = std::make_unique<Module>("M", *TSCtx.getContext());
  58. ThreadSafeModule TSM(std::move(M), TSCtx);
  59. { auto L = TSCtx.getLock(); }
  60. { auto L = TSM.getContext().getLock(); }
  61. }
  62. TEST(ThreadSafeModuleTest, ContextLockPreservesContext) {
  63. // Test that the existence of a context lock preserves the attached
  64. // context.
  65. // The trick to verify this is a bit of a hack: We attach a Module
  66. // (without the ThreadSafeModule wrapper) to the context, then verify
  67. // that this Module destructs safely (which it will not if its context
  68. // has been destroyed) even though all references to the context have
  69. // been thrown away (apart from the lock).
  70. ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
  71. auto L = TSCtx.getLock();
  72. auto &Ctx = *TSCtx.getContext();
  73. auto M = std::make_unique<Module>("M", Ctx);
  74. TSCtx = ThreadSafeContext();
  75. }
  76. } // end anonymous namespace