| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifndef MUJOCO_SRC_ENGINE_ENGINE_GLOBAL_TABLE_H_ |
| | #define MUJOCO_SRC_ENGINE_ENGINE_GLOBAL_TABLE_H_ |
| |
|
| | #include <atomic> |
| | #include <cctype> |
| | #include <cstdio> |
| | #include <mutex> |
| | #include <new> |
| | #include <string> |
| | #include <string_view> |
| | #include <type_traits> |
| |
|
| | #include "engine/engine_util_errmem.h" |
| |
|
| | namespace mujoco { |
| | static constexpr int kCacheLineBytes = 256; |
| |
|
| | static inline bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2) { |
| | if (s1.length() != s2.length()) { |
| | return false; |
| | } |
| | auto len = s1.length(); |
| | for (decltype(len) i = 0; i < len; ++i) { |
| | if (std::tolower(s1[i]) != std::tolower(s2[i])) { |
| | return false; |
| | } |
| | } |
| | return true; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename T> |
| | struct alignas(kCacheLineBytes) TableBlock { |
| | static constexpr int kBlockSize = 15; |
| |
|
| | TableBlock() : objects{}, next(nullptr) { |
| | static_assert( |
| | sizeof(TableBlock<T>) / kCacheLineBytes == |
| | sizeof(TableBlock<T>::objects) / kCacheLineBytes |
| | + (sizeof(TableBlock<T>::objects) % kCacheLineBytes > 0), |
| | "TableBlock::next doesn't fit in the same cache line as the end of TableBlock::objects"); |
| | } |
| |
|
| | T objects[kBlockSize]; |
| | TableBlock<T>* next; |
| | }; |
| |
|
| |
|
| | using Mutex = std::mutex; |
| |
|
| | class ReentrantWriteLock { |
| | public: |
| | ReentrantWriteLock(Mutex& mutex) : mutex_(mutex) { |
| | if (LockCountOnCurrentThread() == 0) { |
| | mutex_.lock(); |
| | } |
| | ++LockCountOnCurrentThread(); |
| | } |
| |
|
| | ~ReentrantWriteLock() { |
| | if (--LockCountOnCurrentThread() == 0) { |
| | mutex_.unlock(); |
| | } |
| | } |
| |
|
| | private: |
| | Mutex& mutex_; |
| |
|
| | static int& LockCountOnCurrentThread() noexcept { |
| | thread_local int counter = 0; |
| | return counter; |
| | } |
| | }; |
| |
|
| | template<typename T> |
| | class GlobalTable { |
| | public: |
| | static GlobalTable<T>& GetSingleton() { |
| | static_assert(std::is_trivially_destructible_v<GlobalTable<T>>); |
| | static GlobalTable<T> global; |
| | return global; |
| | } |
| |
|
| | |
| | using ErrorMessage = char[512]; |
| | static const char* HumanReadableTypeName(); |
| | static std::string_view ObjectKey(const T&); |
| | static bool ObjectEqual(const T&, const T&); |
| | static bool CopyObject(T& dst, const T& src, ErrorMessage& err); |
| |
|
| | int count() { |
| | return count_.load(std::memory_order_acquire); |
| | } |
| |
|
| | ReentrantWriteLock LockExclusively() { |
| | return ReentrantWriteLock(mutex()); |
| | } |
| |
|
| | int AppendIfUnique(const T& obj) { |
| | ErrorMessage err = "\0"; |
| |
|
| | |
| | |
| | |
| | int slot = [&]() { |
| | auto lock = LockExclusively(); |
| |
|
| | int count = count_.load(std::memory_order_acquire); |
| | int local_idx = 0; |
| | TableBlock<T>* block = &first_block_; |
| |
|
| | |
| | for (int i = 0; i < count; ++i, ++local_idx) { |
| | if (local_idx == TableBlock<T>::kBlockSize) { |
| | local_idx = 0; |
| | block = block->next; |
| | } |
| | const T& existing = block->objects[local_idx]; |
| | if (CaseInsensitiveEqual(ObjectKey(obj), ObjectKey(existing))) { |
| | if (!ObjectEqual(obj, existing)) { |
| | std::snprintf(err, sizeof(err), "%s '%s' is already registered", |
| | HumanReadableTypeName(), std::string(ObjectKey(obj)).c_str()); |
| | return -1; |
| | } else { |
| | return i; |
| | } |
| | } |
| | } |
| |
|
| | |
| | if (local_idx == TableBlock<T>::kBlockSize) { |
| | local_idx = 0; |
| | block->next = new(std::nothrow) TableBlock<T>; |
| | if (!block->next) { |
| | std::snprintf(err, sizeof(err), "failed to allocate memory for a new %s table block", |
| | HumanReadableTypeName()); |
| | return -1; |
| | } |
| | block = block->next; |
| | } |
| |
|
| | |
| | if (!CopyObject(block->objects[local_idx], obj, err)) { |
| | return -1; |
| | } |
| |
|
| | |
| | count_.store(count + 1, std::memory_order_release); |
| |
|
| | return count; |
| | }(); |
| | |
| | |
| |
|
| | |
| | if (slot < 0) { |
| | err[sizeof(err) - 1] = '\0'; |
| | mju_error("%s", err); |
| | } |
| |
|
| | return slot; |
| | } |
| |
|
| | |
| | const T* GetAtSlotUnsafe(int slot, int nslot) { |
| | if (slot < 0 || slot >= nslot) { |
| | return nullptr; |
| | } |
| |
|
| | TableBlock<T>* block = &first_block_; |
| |
|
| | |
| | int local_idx = slot; |
| | while (local_idx >= TableBlock<T>::kBlockSize) { |
| | local_idx -= TableBlock<T>::kBlockSize; |
| | block = block->next; |
| | if (!block) { |
| | return nullptr; |
| | } |
| | } |
| |
|
| | |
| | T* obj = &(block->objects[local_idx]); |
| |
|
| | |
| | if (obj && ObjectKey(*obj).empty()) { |
| | return nullptr; |
| | } |
| |
|
| | return obj; |
| | } |
| |
|
| | |
| | const T* GetByKeyUnsafe(std::string_view key, int* slot, int nslot) { |
| | if (slot) *slot = -1; |
| |
|
| | if (key.empty()) { |
| | return nullptr; |
| | } |
| |
|
| | TableBlock<T>* block = &first_block_; |
| | int found_slot = 0; |
| | while (block) { |
| | for (int i = 0; |
| | i < TableBlock<T>::kBlockSize && found_slot < nslot; |
| | ++i, ++found_slot) { |
| | const T& obj = block->objects[i]; |
| |
|
| | |
| | |
| | std::string_view candidate_key = ObjectKey(obj); |
| | if (candidate_key.empty()) { |
| | return nullptr; |
| | } |
| |
|
| | |
| | if (CaseInsensitiveEqual(candidate_key, key)) { |
| | if (slot) *slot = found_slot; |
| | return &obj; |
| | } |
| | } |
| |
|
| | block = block->next; |
| | } |
| |
|
| | return nullptr; |
| | } |
| |
|
| | const T* GetAtSlot(int slot) { |
| | |
| | |
| | return GetAtSlotUnsafe(slot, count()); |
| | } |
| |
|
| | const T* GetByKey(std::string_view key, int* slot) { |
| | |
| | |
| | return GetByKeyUnsafe(key, slot, count()); |
| | } |
| |
|
| | private: |
| | GlobalTable() { |
| | new(mutex_) Mutex; |
| | } |
| |
|
| | Mutex& mutex() { |
| | return *std::launder(reinterpret_cast<Mutex*>(&mutex_)); |
| | } |
| |
|
| | TableBlock<T> first_block_; |
| | std::atomic_int count_; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | alignas(Mutex) unsigned char mutex_[sizeof(Mutex)]; |
| | }; |
| |
|
| | } |
| |
|
| | #endif |
| |
|