Skip to content

Commit af7cc6d

Browse files
committed
Allow rlbox plugins to support threading
1 parent de11a67 commit af7cc6d

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

code/include/rlbox_sandbox.hpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ struct rlbox_transition_timing
7979
};
8080
#endif
8181

82-
#ifndef RLBOX_SINGLE_THREADED_INVOCATIONS
83-
# error \
84-
"RLBox does not yet support threading. Please define RLBOX_SINGLE_THREADED_INVOCATIONS prior to including RLBox and ensure you are only using it from a single thread. If threading is required, please file a bug."
85-
#endif
86-
8782
/**
8883
* @brief Encapsulation for sandboxes.
8984
*
@@ -97,6 +92,9 @@ class rlbox_sandbox : protected T_Sbx
9792

9893
private:
9994
#ifdef RLBOX_MEASURE_TRANSITION_TIMES
95+
#ifndef RLBOX_SINGLE_THREADED_INVOCATIONS
96+
RLBOX_SHARED_LOCK(transition_times_lock);
97+
#endif
10098
std::vector<rlbox_transition_timing> transition_times;
10199
#endif
102100

@@ -109,6 +107,7 @@ class rlbox_sandbox : protected T_Sbx
109107
RLBOX_SHARED_LOCK(func_ptr_cache_lock);
110108
std::map<std::string, void*> func_ptr_map;
111109

110+
// This is thread-safe so no locks needed
112111
app_pointer_map<typename T_Sbx::T_PointerType> app_ptr_map;
113112

114113
// This variable tracks of the sandbox has already been created/destroyed.
@@ -249,11 +248,16 @@ class rlbox_sandbox : protected T_Sbx
249248
auto on_exit = rlbox::detail::make_scope_exit([&] {
250249
auto exit_time = high_resolution_clock::now();
251250
int64_t ns = duration_cast<nanoseconds>(exit_time - enter_time).count();
252-
sandbox.transition_times.push_back(
253-
rlbox_transition_timing{ rlbox_transition::CALLBACK,
254-
nullptr /* func_name */,
255-
key /* func_ptr */,
256-
ns });
251+
{
252+
#ifndef RLBOX_SINGLE_THREADED_INVOCATIONS
253+
RLBOX_ACQUIRE_UNIQUE_GUARD(transition_times_lock);
254+
#endif
255+
sandbox.transition_times.push_back(
256+
rlbox_transition_timing{ rlbox_transition::CALLBACK,
257+
nullptr /* func_name */,
258+
key /* func_ptr */,
259+
ns });
260+
}
257261
});
258262
#endif
259263
#ifdef RLBOX_TRANSITION_ACTION_OUT
@@ -752,8 +756,13 @@ class rlbox_sandbox : protected T_Sbx
752756
auto on_exit = rlbox::detail::make_scope_exit([&] {
753757
auto exit_time = high_resolution_clock::now();
754758
int64_t ns = duration_cast<nanoseconds>(exit_time - enter_time).count();
755-
transition_times.push_back(rlbox_transition_timing{
756-
rlbox_transition::INVOKE, func_name, func_ptr, ns });
759+
{
760+
#ifndef RLBOX_SINGLE_THREADED_INVOCATIONS
761+
RLBOX_ACQUIRE_UNIQUE_GUARD(transition_times_lock);
762+
#endif
763+
transition_times.push_back(rlbox_transition_timing{
764+
rlbox_transition::INVOKE, func_name, func_ptr, ns });
765+
}
757766
});
758767
#endif
759768
#ifdef RLBOX_TRANSITION_ACTION_IN
@@ -1024,6 +1033,9 @@ class rlbox_sandbox : protected T_Sbx
10241033
}
10251034
inline int64_t get_total_ns_time_in_sandbox_and_transitions()
10261035
{
1036+
#ifndef RLBOX_SINGLE_THREADED_INVOCATIONS
1037+
RLBOX_ACQUIRE_SHARED_GUARD(transition_times_lock);
1038+
#endif
10271039
int64_t ret = 0;
10281040
for (auto& transition_time : transition_times) {
10291041
if (transition_time.invoke == rlbox_transition::INVOKE) {
@@ -1034,7 +1046,12 @@ class rlbox_sandbox : protected T_Sbx
10341046
}
10351047
return ret;
10361048
}
1037-
inline void clear_transition_times() { transition_times.clear(); }
1049+
inline void clear_transition_times() {
1050+
#ifndef RLBOX_SINGLE_THREADED_INVOCATIONS
1051+
RLBOX_ACQUIRE_UNIQUE_GUARD(transition_times_lock);
1052+
#endif
1053+
transition_times.clear();
1054+
}
10381055
#endif
10391056
};
10401057

0 commit comments

Comments
 (0)