Skip to content

Commit 00a52f0

Browse files
committed
support abort compiling
1 parent ba961cd commit 00a52f0

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/rcrl/rcrl.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef HMODULE RCRL_Dynlib;
2929

3030
#include <dlfcn.h>
3131
typedef void* RCRL_Dynlib;
32-
#define RDRL_LoadDynlib(lib) dlopen(lib, RTLD_LAZY | RTLD_GLOBAL)
32+
#define RDRL_LoadDynlib(lib) dlopen(lib, RTLD_NOW | RTLD_GLOBAL)
3333
#define RCRL_CloseDynlib dlclose
3434
#endif
3535

@@ -53,7 +53,9 @@ auto CopyFileToString(const string& f_name) {
5353
return out;
5454
}
5555
Plugin::Plugin(fs::path file, std::vector<string> flags)
56-
: is_compiling_(false), parser_(file.string() + ".cpp", flags) {
56+
: is_compiling_(false),
57+
parser_(file.string() + ".cpp", flags),
58+
abort_(false) {
5759
auto header = parser_.get_file().replace_extension(".hpp");
5860
std::ofstream f(header, std::fstream::trunc | std::fstream::out);
5961
f << "#pragma once\n";
@@ -181,15 +183,29 @@ bool Plugin::CompileCode(string code) {
181183
return lambda_impl(ec, size, lambda_impl);
182184
};
183185
ap.async_read_some(output_buffer, OnStdout);
184-
ios.run();
185-
c.join();
186+
// support aborting compiling
187+
while (!ios.stopped() && !abort_) {
188+
ios.run_for(std::chrono::milliseconds(500));
189+
}
190+
if (abort_) {
191+
ios.stop();
192+
c.terminate();
193+
abort_ = false;
194+
} else {
195+
c.join();
196+
}
186197
is_compiling_ = false;
187198
return c.exit_code();
188199
});
189200
return true;
190201
}
191202

192203
bool Plugin::IsCompiling() { return is_compiling_; }
204+
void Plugin::AbortCompiling() {
205+
if (IsCompiling()) {
206+
abort_ = true;
207+
}
208+
}
193209

194210
bool Plugin::TryGetExitStatusFromCompile(int& exit_code) {
195211
if (compiler_process_.valid() && !IsCompiling()) {
@@ -231,18 +247,18 @@ string Plugin::CopyAndLoadNewPlugin(bool redirect_stdout) {
231247
fd = dup(fileno(stdout));
232248
freopen(kRcrlOutputFile.c_str(), "w", stdout);
233249
}
250+
251+
string out;
252+
234253
// load the plugin
235254
auto plugin = RDRL_LoadDynlib(name_copied.c_str());
255+
236256
if (!plugin) {
237-
fprintf(stderr, "%s\n", dlerror());
238-
exit(EXIT_FAILURE);
257+
compiler_output_ = string("Library loading error: ") + dlerror();
258+
} else {
259+
// add the plugin to the list of loaded ones - for later unloading
260+
plugins_.push_back({name_copied, plugin});
239261
}
240-
assert(plugin);
241-
242-
// add the plugin to the list of loaded ones - for later unloading
243-
plugins_.push_back({name_copied, plugin});
244-
245-
string out;
246262

247263
if (redirect_stdout) {
248264
// Flush stdout so any buffered messages are delivered

src/rcrl/rcrl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Plugin {
2222
string CleanupPlugins(bool redirect_stdout = false);
2323
bool CompileCode(string code);
2424
bool IsCompiling();
25+
void AbortCompiling();
2526

2627
bool TryGetExitStatusFromCompile(int& exitcode);
2728
string CopyAndLoadNewPlugin(bool redirect_stdout = false);
@@ -37,6 +38,7 @@ class Plugin {
3738
std::future<int> compiler_process_;
3839
bool last_compile_successful_ = false;
3940
PluginParser parser_;
41+
std::atomic_bool abort_;
4042
};
4143

4244
} // namespace rcrl

0 commit comments

Comments
 (0)