Skip to content

Commit

Permalink
try fix segfaults related to call_once
Browse files Browse the repository at this point in the history
  • Loading branch information
wdeconinck committed Feb 25, 2025
1 parent bbd5b4c commit 3197e52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
1 change: 0 additions & 1 deletion pluto/src/pluto/host/MemoryResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ inline void set_default_resource(std::string_view name) {
}

inline memory_resource* get_default_resource() {
pluto::init();
return pluto::get_default_resource();
}

Expand Down
35 changes: 23 additions & 12 deletions pluto/src/pluto/memory_resource/memory_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,40 @@
#include "pluto/host/MemoryResource.h"

namespace pluto {
void init() {
static std::once_flag flag;
std::call_once(flag, [](){
#if PLUTO_DEBUGGING
std::cout << "pluto::init()" << std::endl;
#endif
namespace {
struct RegisterPlutoResources {
private:
RegisterPlutoResources() {
register_resource("pluto::null_memory_resource", null_memory_resource());
register_resource("pluto::new_delete_resource", new_delete_resource());
register_resource("pluto::pinned_resource", pinned_resource());
register_resource("pluto::device_resource", device_resource());
register_resource("pluto::managed_resource", managed_resource());
register_resource("pluto::pinned_resource", pinned_resource());
register_resource("pluto::pool_resource", pool_resource());
register_resource("pluto::pinned_pool_resource", pinned_pool_resource());
register_resource("pluto::device_pool_resource", device_pool_resource());
register_resource("pluto::managed_pool_resource", managed_pool_resource());
}
public:
static void once() {
static RegisterPlutoResources instance;
}
};
} // namespace

void init() {
[[maybe_unused]] static bool initialized = []() {
#if PLUTO_DEBUGGING
std::cout << "pluto::init()" << std::endl;
#endif
if (char* env = std::getenv("PLUTO_HOST_MEMORY_RESOURCE")) {
pluto::host::set_default_resource(env);
}
if (char* env = std::getenv("PLUTO_DEVICE_MEMORY_RESOURCE")) {
pluto::device::set_default_resource(env);
}
});

return true;
}();
}


Expand Down Expand Up @@ -90,17 +101,17 @@ void unregister_resource(std::string_view name) {
}

memory_resource* get_registered_resource(std::string_view name) {
init();
RegisterPlutoResources::once();
return &MemoryResourceRegistry::instance().get(name);
}

std::string_view get_registered_name(void* mr) {
init();
RegisterPlutoResources::once();
return MemoryResourceRegistry::instance().name(mr);
}

bool has_registered_resource(std::string_view name) {
init();
RegisterPlutoResources::once();
return MemoryResourceRegistry::instance().has(name);
}

Expand Down

0 comments on commit 3197e52

Please sign in to comment.