Make mjz_decoder threadsafe.

PiperOrigin-RevId: 907574530
Change-Id: Ie292ff10dfa515e4ddc9dad1459e44c3ba9eeabe
This commit is contained in:
Sam Haves
2026-04-29 07:18:34 -07:00
committed by Copybara-Service
parent e745538ad6
commit a2cd7aa52f
+5
View File
@@ -16,6 +16,7 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <mutex>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <span> #include <span>
@@ -141,6 +142,9 @@ class ZipArchiveProvider : public mjpResourceProvider {
FileInfo& info = it->second; FileInfo& info = it->second;
// Lazily read and store the file contents from the archive. // Lazily read and store the file contents from the archive.
// The mutex is needed because mz_zip_archive is not thread-safe, and
// multiple threads may call Read concurrently during parallel compilation.
std::lock_guard<std::mutex> lock(mutex_);
if (info.contents.empty()) { if (info.contents.empty()) {
info.contents.resize(info.size); info.contents.resize(info.size);
if (!mz_zip_reader_extract_to_mem(&archive_, info.index, if (!mz_zip_reader_extract_to_mem(&archive_, info.index,
@@ -168,6 +172,7 @@ class ZipArchiveProvider : public mjpResourceProvider {
mz_zip_archive archive_; mz_zip_archive archive_;
std::vector<char> buffer_; std::vector<char> buffer_;
std::unordered_map<std::string, FileInfo> files_; std::unordered_map<std::string, FileInfo> files_;
mutable std::mutex mutex_;
}; };
static mjSpec* ParseZipBuffer(const void* buffer, int nbuffer, const char* name, static mjSpec* ParseZipBuffer(const void* buffer, int nbuffer, const char* name,