Skip to content

Commit 7162e22

Browse files
committed
Address feedback: comment, atomic ref count, better env helper
1 parent 744e549 commit 7162e22

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

source/loader/ur_lib.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ context_t::context_t() {
2828
}
2929
}
3030
}
31+
// Remove the trailing ";"
3132
availableLayers.pop_back();
3233
parseEnvEnabledLayers();
3334
}
@@ -40,20 +41,14 @@ bool context_t::layerExists(const std::string &layerName) const {
4041
}
4142

4243
void context_t::parseEnvEnabledLayers() {
43-
auto maybeEnableEnvVar = ur_getenv("UR_ENABLE_LAYERS");
44-
if (!maybeEnableEnvVar.has_value()) {
44+
auto maybeEnableEnvVarMap = getenv_to_map("UR_ENABLE_LAYERS", false);
45+
if (!maybeEnableEnvVarMap.has_value()) {
4546
return;
4647
}
47-
auto enableEnvVar = maybeEnableEnvVar.value();
48+
auto enableEnvVarMap = maybeEnableEnvVarMap.value();
4849

49-
size_t pos = 0;
50-
while ((pos = enableEnvVar.find(",")) != std::string::npos) {
51-
enabledLayerNames.insert(enableEnvVar.substr(0, pos));
52-
enableEnvVar.erase(0, enableEnvVar.find(";") + 1);
53-
}
54-
55-
if (!enableEnvVar.empty()) {
56-
enabledLayerNames.insert(enableEnvVar);
50+
for (auto &key : enableEnvVarMap) {
51+
enabledLayerNames.insert(key.first);
5752
}
5853
}
5954

@@ -146,15 +141,16 @@ ur_result_t urLoaderConfigGetInfo(ur_loader_config_handle_t hLoaderConfig,
146141
break;
147142
}
148143
case UR_LOADER_CONFIG_INFO_REFERENCE_COUNT: {
149-
auto truePropSize = sizeof(hLoaderConfig->refCount);
144+
auto refCount = hLoaderConfig->getReferenceCount();
145+
auto truePropSize = sizeof(refCount);
150146
if (pPropSizeRet) {
151147
*pPropSizeRet = truePropSize;
152148
}
153149
if (pPropValue) {
154150
if (propSize != truePropSize) {
155151
return UR_RESULT_ERROR_INVALID_SIZE;
156152
}
157-
std::memcpy(pPropValue, &hLoaderConfig->refCount, truePropSize);
153+
std::memcpy(pPropValue, &refCount, truePropSize);
158154
}
159155
break;
160156
}

source/loader/ur_lib.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@
2323
#include "tracing/ur_tracing_layer.hpp"
2424
#endif
2525

26+
#include <atomic>
2627
#include <mutex>
2728
#include <set>
2829
#include <vector>
2930

3031
struct ur_loader_config_handle_t_ {
3132
std::set<std::string> enabledLayers;
32-
uint32_t refCount = 1;
33+
std::atomic_uint32_t refCount = 1;
3334

34-
size_t incrementReferenceCount() { return ++refCount; }
35-
size_t decrementReferenceCount() { return --refCount; }
35+
uint32_t incrementReferenceCount() {
36+
return refCount.fetch_add(1, std::memory_order_acq_rel) + 1;
37+
}
38+
uint32_t decrementReferenceCount() {
39+
return refCount.fetch_sub(1, std::memory_order_acq_rel) - 1;
40+
}
41+
uint32_t getReferenceCount() {
42+
return refCount.load(std::memory_order_acquire);
43+
}
3644
std::set<std::string> &getEnabledLayerNames() { return enabledLayers; }
3745
};
3846

0 commit comments

Comments
 (0)