Skip to content
Merged
Changes from 1 commit
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
21 changes: 12 additions & 9 deletions offload/liboffload/src/OffloadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ struct AllocInfo {

// Global shared state for liboffload
struct OffloadContext;
static OffloadContext *OffloadContextVal;
// This pointer is non-null if and only if the context is valid and fully
// initialized
static std::atomic<OffloadContext *> OffloadContextVal;
std::mutex OffloadContextValMutex;
struct OffloadContext {
OffloadContext(OffloadContext &) = delete;
Expand Down Expand Up @@ -147,9 +149,7 @@ constexpr ol_platform_backend_t pluginNameToBackend(StringRef Name) {
#define PLUGIN_TARGET(Name) extern "C" GenericPluginTy *createPlugin_##Name();
#include "Shared/Targets.def"

Error initPlugins() {
auto &Context = OffloadContext::get();

Error initPlugins(OffloadContext &Context) {
// Attempt to create an instance of each supported plugin.
#define PLUGIN_TARGET(Name) \
do { \
Expand Down Expand Up @@ -199,8 +199,11 @@ Error olInit_impl() {
return Plugin::success();
}

OffloadContextVal = new OffloadContext{};
Error InitResult = initPlugins();
// Use a temporary to ensure that entry points querying OffloadContextVal do
// not get a partially initialized context
auto *NewContext = new OffloadContext{};
Error InitResult = initPlugins(*NewContext);
OffloadContextVal.store(NewContext);
OffloadContext::get().RefCount++;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we be calling new and delete on this if it's null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The context is new'd in initPlugins(), but perhaps it makes sense doing it in olInit_impl, so I'll move it here.


return InitResult;
Expand All @@ -213,18 +216,18 @@ Error olShutDown_impl() {
return Error::success();

llvm::Error Result = Error::success();
auto *OldContext = OffloadContextVal.exchange(nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, forgot to mention that this needed to be atomic since we're using it outside the mutex as a guard.


for (auto &P : OffloadContext::get().Platforms) {
for (auto &P : OldContext->Platforms) {
// Host plugin is nullptr and has no deinit
if (!P.Plugin)
continue;

if (auto Res = P.Plugin->deinit())
Result = llvm::joinErrors(std::move(Result), std::move(Res));
}
delete OffloadContextVal;
OffloadContextVal = nullptr;

delete OldContext;
return Result;
}

Expand Down
Loading