Skip to content

Commit 72bf3b0

Browse files
committed
Only create thread if used
1 parent 15cdffc commit 72bf3b0

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

offload/plugins-nextgen/common/include/RPC.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct RPCServerTy {
4444
/// Deinitialize the associated memory and resources.
4545
llvm::Error shutDown();
4646

47+
/// Initialize the worker thread.
48+
llvm::Error startThread();
49+
4750
/// Check if this device image is using an RPC server. This checks for the
4851
/// precense of an externally visible symbol in the device image that will
4952
/// be present whenever RPC code is called.
@@ -85,7 +88,9 @@ struct RPCServerTy {
8588
llvm::ArrayRef<std::atomic<uintptr_t>> Handles;
8689

8790
/// Initialize the worker thread to run in the background.
88-
ServerThread(std::atomic<uintptr_t> Handles[], size_t Length);
91+
ServerThread(std::atomic<uintptr_t> Handles[], size_t Length)
92+
: Running(true), NumUsers(0), CV(), Mutex(), Handles(Handles, Length) {}
93+
8994
~ServerThread() { assert(!Running && "Thread not shut down explicitly\n"); }
9095

9196
/// Notify the worker thread that there is a user that needs it.
@@ -105,6 +110,9 @@ struct RPCServerTy {
105110
/// Destroy the worker thread and wait.
106111
void shutDown();
107112

113+
/// Initialize the worker thread.
114+
void startThread();
115+
108116
/// Run the server thread to continuously check the RPC interface for work
109117
/// to be done for the device.
110118
void run();

offload/plugins-nextgen/common/src/PluginInterface.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,9 @@ Error GenericDeviceTy::setupRPCServer(GenericPluginTy &Plugin,
10511051
if (auto Err = Server.initDevice(*this, Plugin.getGlobalHandler(), Image))
10521052
return Err;
10531053

1054+
if (auto Err = Server.startThread())
1055+
return Err;
1056+
10541057
RPCServer = &Server;
10551058
DP("Running an RPC server on device %d\n", getDeviceId());
10561059
return Plugin::success();

offload/plugins-nextgen/common/src/RPC.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ using namespace llvm;
2121
using namespace omp;
2222
using namespace target;
2323

24+
void RPCServerTy::ServerThread::startThread() {
25+
#ifdef LIBOMPTARGET_RPC_SUPPORT
26+
Worker = std::thread([this]() { run(); });
27+
#endif
28+
}
29+
2430
void RPCServerTy::ServerThread::shutDown() {
2531
#ifdef LIBOMPTARGET_RPC_SUPPORT
2632
{
@@ -60,19 +66,18 @@ void RPCServerTy::ServerThread::run() {
6066
#endif
6167
}
6268

63-
RPCServerTy::ServerThread::ServerThread(std::atomic<uintptr_t> Handles[],
64-
size_t Length)
65-
: Running(true), NumUsers(0), CV(), Mutex(), Handles(Handles, Length) {
66-
#ifdef LIBOMPTARGET_RPC_SUPPORT
67-
Worker = std::thread([this]() { run(); });
68-
#endif
69-
}
70-
7169
RPCServerTy::RPCServerTy(plugin::GenericPluginTy &Plugin)
7270
: Handles(
7371
std::make_unique<std::atomic<uintptr_t>[]>(Plugin.getNumDevices())),
7472
Thread(new ServerThread(Handles.get(), Plugin.getNumDevices())) {}
7573

74+
llvm::Error RPCServerTy::startThread() {
75+
#ifdef LIBOMPTARGET_RPC_SUPPORT
76+
Thread->startThread();
77+
#endif
78+
return Error::success();
79+
}
80+
7681
llvm::Error RPCServerTy::shutDown() {
7782
#ifdef LIBOMPTARGET_RPC_SUPPORT
7883
Thread->shutDown();

0 commit comments

Comments
 (0)