Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion offload/plugins-nextgen/common/include/RPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct RPCServerTy {
std::thread Worker;

/// A boolean indicating whether or not the worker thread should continue.
std::atomic<bool> Running;
std::atomic<uint32_t> Running;

/// The number of currently executing kernels across all devices that need
/// the server thread to be running.
Expand Down
10 changes: 4 additions & 6 deletions offload/plugins-nextgen/common/src/PluginInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,8 @@ Error GenericDeviceTy::setupRPCServer(GenericPluginTy &Plugin,
if (auto Err = Server.initDevice(*this, Plugin.getGlobalHandler(), Image))
return Err;

if (!Server.Thread->Running.load(std::memory_order_acquire))
if (auto Err = Server.startThread())
return Err;
if (auto Err = Server.startThread())
return Err;

RPCServer = &Server;
DP("Running an RPC server on device %d\n", getDeviceId());
Expand Down Expand Up @@ -1635,12 +1634,11 @@ Error GenericPluginTy::deinit() {
if (GlobalHandler)
delete GlobalHandler;

if (RPCServer && RPCServer->Thread->Running.load(std::memory_order_acquire))
if (RPCServer) {
if (Error Err = RPCServer->shutDown())
return Err;

if (RPCServer)
delete RPCServer;
}

if (RecordReplay)
delete RecordReplay;
Expand Down
11 changes: 4 additions & 7 deletions offload/plugins-nextgen/common/src/RPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ static rpc::Status runServer(plugin::GenericDeviceTy &Device, void *Buffer) {
}

void RPCServerTy::ServerThread::startThread() {
assert(!Running.load(std::memory_order_relaxed) &&
"Attempting to start thread that is already running");
Running.store(true, std::memory_order_release);
Worker = std::thread([this]() { run(); });
if (!Running.fetch_or(true, std::memory_order_acquire))
Worker = std::thread([this]() { run(); });
}

void RPCServerTy::ServerThread::shutDown() {
assert(Running.load(std::memory_order_relaxed) &&
"Attempting to shut down a thread that is not running");
if (!Running.fetch_and(false, std::memory_order_release))
return;
{
std::lock_guard<decltype(Mutex)> Lock(Mutex);
Running.store(false, std::memory_order_release);
CV.notify_all();
}
if (Worker.joinable())
Expand Down