Skip to content

Commit c038c59

Browse files
committed
Updated thread state values, increased polling interval and updated tests
Signed-off-by: Neil R. Spruit <[email protected]>
1 parent 485c23b commit c038c59

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

source/lib/ze_lib.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
*
43
* Copyright (C) 2019-2021 Intel Corporation
@@ -27,11 +26,14 @@ namespace ze_lib
2726
ze_lib::context = nullptr;
2827
}
2928
}
29+
#define ZEL_STABILITY_THREAD_STATE_RUNNING 1
30+
#define ZEL_STABILITY_THREAD_STATE_POLLING 0
31+
#define ZEL_STABILITY_THREAD_STATE_SHUTDOWN -1
3032
bool delayContextDestruction = false;
3133
std::mutex *stabilityMutex = nullptr;
3234
std::promise<int> *stabilityPromiseResult = nullptr;
3335
std::future<int> *resultFutureResult = nullptr;
34-
std::atomic<int> *stabilityCheckThreadStarted = nullptr;
36+
std::atomic<int> *stabilityCheckThreadStatus = nullptr;
3537
std::thread *stabilityThread = nullptr;
3638
#endif
3739
bool destruction = false;
@@ -49,8 +51,8 @@ namespace ze_lib
4951
if (loader) {
5052
FREE_DRIVER_LIBRARY( loader );
5153
}
52-
if (ze_lib::stabilityCheckThreadStarted)
53-
ze_lib::stabilityCheckThreadStarted->store(-1);
54+
if (ze_lib::stabilityCheckThreadStatus)
55+
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_SHUTDOWN);
5456
try {
5557
if (stabilityThread && stabilityThread->joinable()) {
5658
stabilityThread->join();
@@ -74,9 +76,9 @@ namespace ze_lib
7476
delete resultFutureResult;
7577
resultFutureResult = nullptr;
7678
}
77-
if (stabilityCheckThreadStarted) {
78-
delete stabilityCheckThreadStarted;
79-
stabilityCheckThreadStarted = nullptr;
79+
if (stabilityCheckThreadStatus) {
80+
delete stabilityCheckThreadStatus;
81+
stabilityCheckThreadStatus = nullptr;
8082
}
8183
#endif
8284
ze_lib::destruction = true;
@@ -187,7 +189,7 @@ namespace ze_lib
187189
stabilityMutex = new std::mutex();
188190
stabilityPromiseResult = new std::promise<int>();
189191
resultFutureResult = new std::future<int>(stabilityPromiseResult->get_future());
190-
stabilityCheckThreadStarted = new std::atomic<int>(0);
192+
stabilityCheckThreadStatus = new std::atomic<int>(ZEL_STABILITY_THREAD_STATE_POLLING);
191193
#else
192194
result = zeLoaderInit();
193195
if( ZE_RESULT_SUCCESS == result ) {
@@ -451,6 +453,8 @@ zelSetDelayLoaderContextTeardown()
451453
#define ZEL_STABILITY_CHECK_RESULT_EXCEPTION 3
452454
// The stability check thread timeout in milliseconds
453455
#define ZEL_STABILITY_CHECK_THREAD_TIMEOUT 100
456+
// The stability check thread polling interval in nanoseconds
457+
#define ZEL_STABILITY_CHECK_THREAD_POLLING_INTERVAL 100
454458

455459
/**
456460
* @brief Performs a stability check for the Level Zero loader.
@@ -532,23 +536,23 @@ zelCheckIsLoaderInTearDown() {
532536
std::lock_guard<std::mutex> lock(*ze_lib::stabilityMutex);
533537
*ze_lib::stabilityPromiseResult = std::promise<int>();
534538
*ze_lib::resultFutureResult = ze_lib::stabilityPromiseResult->get_future();
535-
ze_lib::stabilityCheckThreadStarted->store(1);
539+
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_RUNNING);
536540
std::call_once(stabilityThreadFlag, []() {
537541
ze_lib::stabilityThread = new std::thread([]() {
538542
while (true) {
539-
while(ze_lib::stabilityCheckThreadStarted && ze_lib::stabilityCheckThreadStarted->load() == 0) {
540-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
543+
while(ze_lib::stabilityCheckThreadStatus && ze_lib::stabilityCheckThreadStatus->load() == ZEL_STABILITY_THREAD_STATE_POLLING) {
544+
std::this_thread::sleep_for(std::chrono::nanoseconds(ZEL_STABILITY_CHECK_THREAD_POLLING_INTERVAL));
541545
}
542546
if (ze_lib::destruction || ze_lib::context == nullptr) {
543547
break;
544548
}
545-
if (!ze_lib::stabilityCheckThreadStarted) {
549+
if (!ze_lib::stabilityCheckThreadStatus) {
546550
break;
547551
}
548-
if (ze_lib::stabilityCheckThreadStarted->load() == -1) {
552+
if (ze_lib::stabilityCheckThreadStatus->load() == ZEL_STABILITY_THREAD_STATE_SHUTDOWN) {
549553
break;
550554
}
551-
ze_lib::stabilityCheckThreadStarted->store(0);
555+
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_POLLING);
552556
int result = stabilityCheck();
553557
if (result != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
554558
if (ze_lib::context->debugTraceEnabled) {

test/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ endif()
9797
add_test(NAME tests_loader_teardown_check COMMAND tests --gtest_filter=*GivenLoaderNotInDestructionStateWhenCallingzelCheckIsLoaderInTearDownThenFalseIsReturned)
9898
set_property(TEST tests_loader_teardown_check PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
9999

100+
add_test(NAME test_zello_world_legacy COMMAND zello_world --enable_legacy_init --enable_null_driver --force_loader_intercepts --enable_validation_layer --enable_tracing_layer --enable_tracing_layer_runtime)
101+
set_property(TEST test_zello_world_legacy PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
102+
103+
add_test(NAME test_zello_world_legacy_intercept COMMAND zello_world --enable_legacy_init --enable_null_driver --force_loader_intercepts)
104+
set_property(TEST test_zello_world_legacy_intercept PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
105+
106+
add_test(NAME test_zello_world_legacy_validation_layer COMMAND zello_world --enable_legacy_init --enable_null_driver --enable_validation_layer)
107+
set_property(TEST test_zello_world_legacy_validation_layer PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
108+
109+
add_test(NAME test_zello_world_legacy_tracing COMMAND zello_world --enable_legacy_init --enable_null_driver --enable_tracing_layer)
110+
set_property(TEST test_zello_world_legacy_tracing PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
111+
112+
add_test(NAME test_zello_world_legacy_dynamic_tracing COMMAND zello_world --enable_legacy_init --enable_null_driver --enable_tracing_layer_runtime)
113+
set_property(TEST test_zello_world_legacy_dynamic_tracing PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
114+
115+
add_test(NAME test_zello_world_legacy_all_tracing COMMAND zello_world --enable_legacy_init --enable_null_driver --force_loader_intercepts --enable_validation_layer --enable_tracing_layer)
116+
set_property(TEST test_zello_world_legacy_all_tracing PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
117+
118+
add_test(NAME test_zello_world_legacy_all_tracing_dynamic COMMAND zello_world --enable_legacy_init --enable_null_driver --force_loader_intercepts --enable_validation_layer --enable_tracing_layer_runtime)
119+
set_property(TEST test_zello_world_legacy_all_tracing_dynamic PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1")
120+
100121
# These tests are currently not supported on Windows. The reason is that the std::cerr is not being redirected to a pipe in Windows to be then checked against the expected output.
101122
if(NOT MSVC)
102123
add_test(NAME tests_event_deadlock COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingzeCommandListAppendMemoryCopyWithCircularDependencyOnEventsThenValidationLayerPrintsWarningOfDeadlock*)

test/loader_api.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ TEST(
374374

375375
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
376376
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
377+
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
378+
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
379+
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
377380
}
378381

379382
class CaptureOutput {

0 commit comments

Comments
 (0)