Skip to content

Commit f0f1c03

Browse files
committed
Updated thread state values, increased polling interval and updated tests
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent 485c23b commit f0f1c03

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

source/lib/ze_lib.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ namespace ze_lib
2727
ze_lib::context = nullptr;
2828
}
2929
}
30+
#define ZEL_STABILITY_THREAD_STATE_RUNNING 1
31+
#define ZEL_STABILITY_THREAD_STATE_POLLING 0
32+
#define ZEL_STABILITY_THREAD_STATE_SHUTDOWN -1
3033
bool delayContextDestruction = false;
3134
std::mutex *stabilityMutex = nullptr;
3235
std::promise<int> *stabilityPromiseResult = nullptr;
3336
std::future<int> *resultFutureResult = nullptr;
34-
std::atomic<int> *stabilityCheckThreadStarted = nullptr;
37+
std::atomic<int> *stabilityCheckThreadStatus = nullptr;
3538
std::thread *stabilityThread = nullptr;
3639
#endif
3740
bool destruction = false;
@@ -49,8 +52,8 @@ namespace ze_lib
4952
if (loader) {
5053
FREE_DRIVER_LIBRARY( loader );
5154
}
52-
if (ze_lib::stabilityCheckThreadStarted)
53-
ze_lib::stabilityCheckThreadStarted->store(-1);
55+
if (ze_lib::stabilityCheckThreadStatus)
56+
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_SHUTDOWN);
5457
try {
5558
if (stabilityThread && stabilityThread->joinable()) {
5659
stabilityThread->join();
@@ -74,9 +77,9 @@ namespace ze_lib
7477
delete resultFutureResult;
7578
resultFutureResult = nullptr;
7679
}
77-
if (stabilityCheckThreadStarted) {
78-
delete stabilityCheckThreadStarted;
79-
stabilityCheckThreadStarted = nullptr;
80+
if (stabilityCheckThreadStatus) {
81+
delete stabilityCheckThreadStatus;
82+
stabilityCheckThreadStatus = nullptr;
8083
}
8184
#endif
8285
ze_lib::destruction = true;
@@ -187,7 +190,7 @@ namespace ze_lib
187190
stabilityMutex = new std::mutex();
188191
stabilityPromiseResult = new std::promise<int>();
189192
resultFutureResult = new std::future<int>(stabilityPromiseResult->get_future());
190-
stabilityCheckThreadStarted = new std::atomic<int>(0);
193+
stabilityCheckThreadStatus = new std::atomic<int>(ZEL_STABILITY_THREAD_STATE_POLLING);
191194
#else
192195
result = zeLoaderInit();
193196
if( ZE_RESULT_SUCCESS == result ) {
@@ -451,6 +454,8 @@ zelSetDelayLoaderContextTeardown()
451454
#define ZEL_STABILITY_CHECK_RESULT_EXCEPTION 3
452455
// The stability check thread timeout in milliseconds
453456
#define ZEL_STABILITY_CHECK_THREAD_TIMEOUT 100
457+
// The stability check thread polling interval in nanoseconds
458+
#define ZEL_STABILITY_CHECK_THREAD_POLLING_INTERVAL 100
454459

455460
/**
456461
* @brief Performs a stability check for the Level Zero loader.
@@ -532,23 +537,23 @@ zelCheckIsLoaderInTearDown() {
532537
std::lock_guard<std::mutex> lock(*ze_lib::stabilityMutex);
533538
*ze_lib::stabilityPromiseResult = std::promise<int>();
534539
*ze_lib::resultFutureResult = ze_lib::stabilityPromiseResult->get_future();
535-
ze_lib::stabilityCheckThreadStarted->store(1);
540+
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_RUNNING);
536541
std::call_once(stabilityThreadFlag, []() {
537542
ze_lib::stabilityThread = new std::thread([]() {
538543
while (true) {
539-
while(ze_lib::stabilityCheckThreadStarted && ze_lib::stabilityCheckThreadStarted->load() == 0) {
540-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
544+
while(ze_lib::stabilityCheckThreadStatus && ze_lib::stabilityCheckThreadStatus->load() == ZEL_STABILITY_THREAD_STATE_POLLING) {
545+
std::this_thread::sleep_for(std::chrono::nanoseconds(ZEL_STABILITY_CHECK_THREAD_POLLING_INTERVAL));
541546
}
542547
if (ze_lib::destruction || ze_lib::context == nullptr) {
543548
break;
544549
}
545-
if (!ze_lib::stabilityCheckThreadStarted) {
550+
if (!ze_lib::stabilityCheckThreadStatus) {
546551
break;
547552
}
548-
if (ze_lib::stabilityCheckThreadStarted->load() == -1) {
553+
if (ze_lib::stabilityCheckThreadStatus->load() == ZEL_STABILITY_THREAD_STATE_SHUTDOWN) {
549554
break;
550555
}
551-
ze_lib::stabilityCheckThreadStarted->store(0);
556+
ze_lib::stabilityCheckThreadStatus->store(ZEL_STABILITY_THREAD_STATE_POLLING);
552557
int result = stabilityCheck();
553558
if (result != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
554559
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "loader/ze_loader.h"
1212
#include "ze_api.h"
1313
#include "zes_api.h"
14+
#include "../source/lib/ze_lib.h" // Include the header file defining ze_lib
1415

1516
#include <fstream>
1617

@@ -374,6 +375,9 @@ TEST(
374375

375376
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
376377
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
378+
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
379+
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
380+
EXPECT_FALSE(zelCheckIsLoaderInTearDown());
377381
}
378382

379383
class CaptureOutput {

0 commit comments

Comments
 (0)