Skip to content
Permalink
Browse files

RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES support via function

  • Loading branch information
shravanrn committed Dec 16, 2019
1 parent 9917d84 commit da68ed467665c729bb19fb542216c9ed6ce61a63
@@ -1,3 +1,3 @@
Checks: '*'
Checks: '*,-modernize-use-trailing-return-type'
HeaderFilterRegex: '*'
WarningsAsErrors: '*'
@@ -199,8 +199,10 @@ add_library(rlbox_glue_lib_shared SHARED code/tests/rlbox_glue/lib/libtest.c)
target_include_directories(rlbox_glue_lib_shared
PUBLIC code/tests/rlbox_glue/lib)

add_executable(test_rlbox_glue code/tests/test_main.cpp
code/tests/rlbox_glue/test_noop_sandbox_glue.cpp)
add_executable(test_rlbox_glue
code/tests/test_main.cpp
code/tests/rlbox_glue/test_noop_sandbox_glue.cpp
code/tests/rlbox_glue/test_noop_sandbox_glue_embedder_vars.cpp)
target_include_directories(test_rlbox_glue PUBLIC code/tests/rlbox_glue)

target_link_libraries(test_rlbox_glue
@@ -12,6 +12,30 @@

namespace rlbox {

class rlbox_noop_sandbox;

struct rlbox_noop_sandbox_thread_data
{
rlbox_noop_sandbox* sandbox;
uint32_t last_callback_invoked;
};

#ifdef RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES

rlbox_noop_sandbox_thread_data* get_rlbox_noop_sandbox_thread_data();
# define RLBOX_NOOP_SANDBOX_STATIC_VARIABLES() \
thread_local rlbox::rlbox_noop_sandbox_thread_data \
rlbox_noop_sandbox_thread_info{ 0, 0 }; \
namespace rlbox { \
rlbox_noop_sandbox_thread_data* get_rlbox_noop_sandbox_thread_data() \
{ \
return &rlbox_noop_sandbox_thread_info; \
} \
} \
static_assert(true, "Enforce semi-colon")

#endif

/**
* @brief Class that implements the null sandbox. This sandbox doesn't actually
* provide any isolation and only serves as a stepping stone towards migrating
@@ -33,28 +57,16 @@ class rlbox_noop_sandbox
void* callback_unique_keys[MAX_CALLBACKS]{ 0 };
void* callbacks[MAX_CALLBACKS]{ 0 };

struct rlbox_noop_sandbox_thread_local
{
rlbox_noop_sandbox* sandbox;
uint32_t last_callback_invoked;
};

#ifndef RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES
thread_local static inline rlbox_noop_sandbox_thread_local thread_data{ 0,
0 };
#else
thread_local static rlbox_noop_sandbox_thread_local thread_data;
# define RLBOX_NOOP_SANDBOX_STATIC_VARIABLES() \
thread_local rlbox::rlbox_noop_sandbox::rlbox_noop_sandbox_thread_local \
rlbox::rlbox_noop_sandbox::thread_data \
{ \
0, 0 \
}
thread_local static inline rlbox_noop_sandbox_thread_data thread_data{ 0, 0 };
#endif

template<uint32_t N, typename T_Ret, typename... T_Args>
static T_Ret callback_trampoline(T_Args... params)
{
#ifdef RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES
auto& thread_data = *get_rlbox_noop_sandbox_thread_data();
#endif
thread_data.last_callback_invoked = N;
using T_Func = T_Ret (*)(T_Args...);
T_Func func;
@@ -161,6 +173,9 @@ class rlbox_noop_sandbox
template<typename T, typename T_Converted, typename... T_Args>
auto impl_invoke_with_func_ptr(T_Converted* func_ptr, T_Args&&... params)
{
#ifdef RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES
auto& thread_data = *get_rlbox_noop_sandbox_thread_data();
#endif
thread_data.sandbox = this;
return (*func_ptr)(params...);
}
@@ -189,6 +204,9 @@ class rlbox_noop_sandbox
static inline std::pair<rlbox_noop_sandbox*, void*>
impl_get_executed_callback_sandbox_and_key()
{
#ifdef RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES
auto& thread_data = *get_rlbox_noop_sandbox_thread_data();
#endif
auto sandbox = thread_data.sandbox;
auto callback_num = thread_data.last_callback_invoked;
void* key = sandbox->callback_unique_keys[callback_num];
@@ -0,0 +1,18 @@
// NOLINTNEXTLINE
#define RLBOX_USE_STATIC_CALLS() rlbox_noop_sandbox_lookup_symbol
#define RLBOX_USE_EXCEPTIONS
#define RLBOX_ENABLE_DEBUG_ASSERTIONS
#define RLBOX_SINGLE_THREADED_INVOCATIONS
#define RLBOX_EMBEDDER_PROVIDES_TLS_STATIC_VARIABLES
#include "rlbox_noop_sandbox.hpp"

// NOLINTNEXTLINE
#define TestName "rlbox_noop_sandbox embedder"
// NOLINTNEXTLINE
#define TestType rlbox::rlbox_noop_sandbox
// NOLINTNEXTLINE
#define CreateSandbox(sandbox) sandbox.create_sandbox()

RLBOX_NOOP_SANDBOX_STATIC_VARIABLES();

#include "test_sandbox_glue.inc.cpp"

0 comments on commit da68ed4

Please sign in to comment.
You can’t perform that action at this time.