Skip to content

[SYCL] Only call shutdown when DLL is being unloaded, not when process is terminating #4983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sycl/source/detail/global_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL,
// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_DETACH:
shutdown();
if (!lpReserved)
shutdown();
break;
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ add_subdirectory(thread_safety)
add_subdirectory(program_manager)
add_subdirectory(assert)
add_subdirectory(Extensions)
add_subdirectory(windows)
4 changes: 4 additions & 0 deletions sycl/unittests/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_sycl_unittest(WindowsDllMainTest OBJECT
dllmain.cpp
)

65 changes: 65 additions & 0 deletions sycl/unittests/windows/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//==----- dllmain.cpp --- verify behaviour of lib on process termination ---==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

/*
* This test calls DllMain on Windows. This means, the process performs actions
* which are required for library unload. That said, the test requires to be a
* distinct binary executable.
*/

#include <CL/sycl.hpp>
#include <helpers/CommonRedefinitions.hpp>
#include <helpers/PiImage.hpp>
#include <helpers/PiMock.hpp>

#include <gtest/gtest.h>

#ifdef _WIN32
#include <windows.h>

extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpReserved);

static std::atomic<int> TearDownCalls{0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. I would add a static_assert to check that this atomic is trivially destructible.


pi_result redefinedTearDown(void *PluginParameter) {
fprintf(stderr, "intercepted tear down\n");
++TearDownCalls;

return PI_SUCCESS;
}
#endif

TEST(Windows, DllMainCall) {
#ifdef _WIN32
sycl::platform Plt{sycl::default_selector()};
if (Plt.is_host()) {
printf("Test is not supported on host, skipping\n");
return;
}
sycl::unittest::PiMock Mock{Plt};
setupDefaultMockAPIs(Mock);
Mock.redefine<sycl::detail::PiApiKind::piTearDown>(redefinedTearDown);

// Teardown calls are only expected on sycl.dll library unload, not when
// process gets terminated.
// The first call to DllMain is to simulate library unload. The second one
// is to simulate process termination
fprintf(stderr, "Call DllMain for the first time\n");
DllMain((HINSTANCE)0, DLL_PROCESS_DETACH, (LPVOID)NULL);

int TearDownCallsDone = TearDownCalls.load();

EXPECT_NE(TearDownCallsDone, 0);

fprintf(stderr, "Call DllMain for the second time\n");
DllMain((HINSTANCE)0, DLL_PROCESS_DETACH, (LPVOID)0x01);

EXPECT_EQ(TearDownCalls.load(), TearDownCallsDone);
#endif
}