Skip to content

Commit

Permalink
Make thread support optional for WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorSundberg committed Dec 31, 2020
1 parent b16fccf commit 02c05ae
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
#define CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_

#ifndef RTTR_NO_CXX11_THREAD
#include <atomic>
#endif
#include <memory>
#include <set>
#include <stdexcept>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
#define CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_

#ifndef RTTR_NO_CXX11_THREAD
#include <atomic>
#endif
#include <memory>
#include <set>
#include <stdexcept>
Expand Down
19 changes: 18 additions & 1 deletion src/rttr/detail/library/library_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
#include <windows.h>
#endif

#ifndef RTTR_NO_CXX11_THREAD
#include <atomic>
#endif

namespace rttr
{
Expand Down Expand Up @@ -95,7 +97,12 @@ class RTTR_LOCAL library_private

--m_load_count;

if (m_load_count.load() == 0)
#ifdef RTTR_NO_CXX11_THREAD
auto load_count = m_load_count;
#else
auto load_count = m_load_count.load();
#endif
if (load_count == 0)
{
auto ret = unload_native();
if (ret)
Expand Down Expand Up @@ -126,9 +133,15 @@ class RTTR_LOCAL library_private

array_range<method> get_global_methods() const RTTR_NOEXCEPT { return m_state_saver.get_global_methods(); }

#ifdef RTTR_NO_CXX11_THREAD
int get_load_count() const RTTR_NOEXCEPT { return m_load_count; }

void set_load_count(int count) { m_load_count = count; }
#else
int get_load_count() const RTTR_NOEXCEPT { return m_load_count.load(); }

void set_load_count(int count) { m_load_count.store(count); }
#endif

private:
bool load_native();
Expand All @@ -141,7 +154,11 @@ class RTTR_LOCAL library_private
std::string m_error_string;
registration_state_saver m_state_saver;

#ifdef RTTR_NO_CXX11_THREAD
int m_load_count;
#else
std::atomic_int m_load_count;
#endif

#if RTTR_PLATFORM == RTTR_PLATFORM_WINDOWS
HMODULE
Expand Down
8 changes: 8 additions & 0 deletions src/rttr/detail/type/type_register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ type_data* type_register_private::register_name_if_neccessary(type_data* info)
if (ret != m_orig_name_to_id.end())
return ret->m_type_data;

#ifndef RTTR_NO_CXX11_THREAD
std::lock_guard<std::mutex> lock(m_mutex);
#endif

m_orig_name_to_id.insert(std::make_pair(info->type_name, type(info)));
info->name = derive_name(type(info));
Expand Down Expand Up @@ -556,7 +558,9 @@ type_data* type_register_private::register_type(type_data* info) RTTR_NOEXCEPT
info->raw_type_data = !info->raw_type_data->is_valid ? info : info->raw_type_data;

{
#ifndef RTTR_NO_CXX11_THREAD
std::lock_guard<std::mutex> lock(m_mutex);
#endif
m_type_data_storage.push_back(info);
}

Expand Down Expand Up @@ -607,7 +611,9 @@ void type_register_private::unregister_type(type_data* info) RTTR_NOEXCEPT
// REMARK: the base_types has to be provided as argument explicitely and cannot be retrieve via the type_data itself,
// because the `class_data` which holds the base_types information cannot be retrieve via the function `get_class_data`
// anymore because the containing std::unique_ptr is already destroyed
#ifndef RTTR_NO_CXX11_THREAD
std::lock_guard<std::mutex> lock(m_mutex);
#endif

bool found_type_data = false;

Expand Down Expand Up @@ -672,7 +678,9 @@ std::string type_register_private::derive_template_instance_name(type_data* info

void type_register_private::update_custom_name(std::string new_name, const type& t)
{
#ifndef RTTR_NO_CXX11_THREAD
std::lock_guard<std::mutex> lock(m_mutex);
#endif

auto& type_name = t.m_type_data->name;

Expand Down
5 changes: 5 additions & 0 deletions src/rttr/detail/type/type_register_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
#include <memory>
#include <string>
#include <vector>

#ifndef RTTR_NO_CXX11_THREAD
#include <mutex>
#endif

namespace rttr
{
Expand Down Expand Up @@ -227,7 +230,9 @@ class RTTR_LOCAL type_register_private
std::vector<data_container<const type_comparator_base*>> m_type_equal_cmp_list;
std::vector<data_container<const type_comparator_base*>> m_type_less_than_cmp_list;

#ifndef RTTR_NO_CXX11_THREAD
std::mutex m_mutex;
#endif
};

} // end namespace detail
Expand Down
10 changes: 10 additions & 0 deletions src/rttr/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
#include "rttr/detail/library/library_p.h"

#include <map>

#ifndef RTTR_NO_CXX11_THREAD
#include <mutex>
#endif

namespace rttr
{
Expand All @@ -49,7 +52,9 @@ class library_manager
static std::shared_ptr<library_private> create_or_find_library(string_view file_name, string_view version)
{
auto& manager = get_instance();
#ifndef RTTR_NO_CXX11_THREAD
std::lock_guard<std::mutex> lock(manager.m_library_mutex);
#endif

auto file_as_string = file_name.to_string();
auto itr = manager.m_library_map.find(file_as_string);
Expand All @@ -71,7 +76,9 @@ class library_manager
static void remove_item(const std::shared_ptr<library_private>& item)
{
auto& manager = get_instance();
#ifndef RTTR_NO_CXX11_THREAD
std::lock_guard<std::mutex> lock(manager.m_library_mutex);
#endif

auto itr = manager.m_library_map.find(item->get_file_name().to_string()); // because we use string_view to find the item
if (itr != manager.m_library_map.end())
Expand Down Expand Up @@ -102,7 +109,10 @@ class library_manager

// use std::less in order to use string_view for finding the item
std::map<std::string, std::shared_ptr<library_private>> m_library_map;

#ifndef RTTR_NO_CXX11_THREAD
std::mutex m_library_mutex;
#endif
};
}

Expand Down
7 changes: 5 additions & 2 deletions src/rttr/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@
#include <vector>
#include <memory>
#include <set>
#include <thread>
#include <mutex>
#include <cstring>
#include <cctype>
#include <utility>

#ifndef RTTR_NO_CXX11_THREAD
#include <thread>
#include <mutex>
#endif

using namespace std;

namespace rttr
Expand Down

0 comments on commit 02c05ae

Please sign in to comment.