stratosphere: use SdkMutex/SdkRecursiveMutex over Mutex

This commit is contained in:
Michael Scire
2021-09-29 22:52:50 -07:00
parent a4fe1bb5d8
commit 41ab4c2c68
70 changed files with 188 additions and 645 deletions

View File

@@ -24,10 +24,10 @@ namespace ams::cfg {
constexpr os::ProcessId InitialProcessIdMaxDeprecated = {0x50};
/* Privileged process globals. */
os::Mutex g_lock(false);
bool g_got_privileged_process_status = false;
os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId;
os::ProcessId g_cur_process_id = os::InvalidProcessId;
constinit os::SdkMutex g_lock;
constinit bool g_got_privileged_process_status = false;
constinit os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId;
constinit os::ProcessId g_cur_process_id = os::InvalidProcessId;
/* SD card helpers. */
void GetPrivilegedProcessIdRange(os::ProcessId *out_min, os::ProcessId *out_max) {

View File

@@ -29,9 +29,9 @@ namespace ams::cfg {
constexpr size_t NumRequiredServicesForSdCardAccess = util::size(RequiredServicesForSdCardAccess);
/* SD card globals. */
os::Mutex g_sd_card_lock(false);
bool g_sd_card_initialized = false;
FsFileSystem g_sd_card_filesystem = {};
constinit os::SdkMutex g_sd_card_lock;
constinit bool g_sd_card_initialized = false;
constinit FsFileSystem g_sd_card_filesystem = {};
/* SD card helpers. */
Result CheckSdCardServicesReady() {

View File

@@ -44,8 +44,8 @@ namespace ams::diag {
inline void DebugLog(const char *format, ...) __attribute__((format(printf, 1, 2)));
#ifdef AMS_ENABLE_DETAILED_ASSERTIONS
os::Mutex g_debug_log_lock(true);
char g_debug_buffer[0x400];
constinit os::SdkRecursiveMutex g_debug_log_lock;
constinit char g_debug_buffer[0x400];
void DebugLogImpl(const char *format, ::std::va_list vl) {
std::scoped_lock lk(g_debug_log_lock);

View File

@@ -60,7 +60,7 @@ namespace ams::fs::impl {
}
FileSystemAccessor::FileSystemAccessor(const char *n, std::unique_ptr<fsa::IFileSystem> &&fs, std::unique_ptr<fsa::ICommonMountNameGenerator> &&generator)
: impl(std::move(fs)), open_list_lock(false), mount_name_generator(std::move(generator)),
: impl(std::move(fs)), open_list_lock(), mount_name_generator(std::move(generator)),
access_log_enabled(false), data_cache_attachable(false), path_cache_attachable(false), path_cache_attached(false), multi_commit_supported(false)
{
R_ABORT_UNLESS(ValidateMountName(n));

View File

@@ -35,7 +35,7 @@ namespace ams::fs::impl {
std::unique_ptr<fsa::IFileSystem> impl;
FileList open_file_list;
DirList open_dir_list;
os::Mutex open_list_lock;
os::SdkMutex open_list_lock;
std::unique_ptr<fsa::ICommonMountNameGenerator> mount_name_generator;
bool access_log_enabled;
bool data_cache_attachable;

View File

@@ -26,9 +26,9 @@ namespace ams::fs::impl {
using FileSystemList = util::IntrusiveListBaseTraits<FileSystemAccessor>::ListType;
private:
FileSystemList fs_list;
os::Mutex mutex;
os::SdkMutex mutex;
public:
constexpr MountTable() : fs_list(), mutex(false) { /* ... */ }
constexpr MountTable() : fs_list(), mutex() { /* ... */ }
private:
bool CanAcceptMountName(const char *name);
public:

View File

@@ -17,7 +17,7 @@
namespace ams::fssystem {
AesXtsStorage::AesXtsStorage(IStorage *base, const void *key1, const void *key2, size_t key_size, const void *iv, size_t iv_size, size_t block_size) : base_storage(base), block_size(block_size), mutex(false) {
AesXtsStorage::AesXtsStorage(IStorage *base, const void *key1, const void *key2, size_t key_size, const void *iv, size_t iv_size, size_t block_size) : base_storage(base), block_size(block_size), mutex() {
AMS_ASSERT(base != nullptr);
AMS_ASSERT(key1 != nullptr);
AMS_ASSERT(key2 != nullptr);

View File

@@ -74,13 +74,13 @@ namespace ams::fssystem {
}
DirectorySaveDataFileSystem::DirectorySaveDataFileSystem(std::shared_ptr<fs::fsa::IFileSystem> fs)
: PathResolutionFileSystem(fs), accessor_mutex(false), open_writable_files(0)
: PathResolutionFileSystem(fs), accessor_mutex(), open_writable_files(0)
{
/* ... */
}
DirectorySaveDataFileSystem::DirectorySaveDataFileSystem(std::unique_ptr<fs::fsa::IFileSystem> fs)
: PathResolutionFileSystem(std::move(fs)), accessor_mutex(false), open_writable_files(0)
: PathResolutionFileSystem(std::move(fs)), accessor_mutex(), open_writable_files(0)
{
/* ... */
}

View File

@@ -25,7 +25,7 @@ namespace ams::fssystem {
static constexpr s32 LayerCount = 3;
static constexpr size_t HashSize = crypto::Sha256Generator::HashSize;
private:
os::Mutex mutex;
os::SdkMutex mutex;
IStorage *base_storage;
s64 base_storage_size;
char *hash_buffer;
@@ -33,7 +33,7 @@ namespace ams::fssystem {
s32 hash_target_block_size;
s32 log_size_ratio;
public:
HierarchicalSha256Storage() : mutex(false) { /* ... */ }
HierarchicalSha256Storage() : mutex() { /* ... */ }
Result Initialize(IStorage **base_storages, s32 layer_count, size_t htbs, void *hash_buf, size_t hash_buf_size);

View File

@@ -22,10 +22,10 @@ namespace ams::fssystem {
NON_COPYABLE(KeySlotCacheAccessor);
NON_MOVEABLE(KeySlotCacheAccessor);
private:
util::unique_lock<os::Mutex> lk;
util::unique_lock<os::SdkMutex> lk;
const s32 slot_index;
public:
KeySlotCacheAccessor(s32 idx, util::unique_lock<os::Mutex> &&l) : lk(std::move(l)), slot_index(idx) { /* ... */ }
KeySlotCacheAccessor(s32 idx, util::unique_lock<os::SdkMutex> &&l) : lk(std::move(l)), slot_index(idx) { /* ... */ }
s32 GetKeySlotIndex() const { return this->slot_index; }
};
@@ -64,11 +64,11 @@ namespace ams::fssystem {
private:
using KeySlotCacheEntryList = util::IntrusiveListBaseTraits<KeySlotCacheEntry>::ListType;
private:
os::Mutex mutex;
os::SdkMutex mutex;
KeySlotCacheEntryList high_priority_mru_list;
KeySlotCacheEntryList low_priority_mru_list;
public:
constexpr KeySlotCache() : mutex(false), high_priority_mru_list(), low_priority_mru_list() { /* ... */ }
constexpr KeySlotCache() : mutex(), high_priority_mru_list(), low_priority_mru_list() { /* ... */ }
Result AllocateHighPriority(std::unique_ptr<KeySlotCacheAccessor> *out, const void *key, size_t key_size, s32 key2) {
return this->AllocateFromLru(out, this->high_priority_mru_list, key, key_size, key2);

View File

@@ -21,13 +21,12 @@ namespace ams::fssystem {
class AdditionalDeviceAddressEntry {
private:
/* TODO: SdkMutex */
os::Mutex mutex;
os::SdkMutex mutex;
bool is_registered;
uintptr_t address;
size_t size;
public:
constexpr AdditionalDeviceAddressEntry() : mutex(false), is_registered(), address(), size() { /* ... */ }
constexpr AdditionalDeviceAddressEntry() : mutex(), is_registered(), address(), size() { /* ... */ }
void Register(uintptr_t addr, size_t sz) {
std::scoped_lock lk(this->mutex);
@@ -77,18 +76,17 @@ namespace ams::fssystem {
constexpr size_t HeapAllocatableSizeMax = HeapBlockSize * (static_cast<size_t>(1) << HeapOrderMax);
constexpr size_t HeapAllocatableSizeMaxForLarge = HeapBlockSize * (static_cast<size_t>(1) << HeapOrderMaxForLarge);
/* TODO: SdkMutex */
os::Mutex g_heap_mutex(false);
FileSystemBuddyHeap g_heap;
constinit os::SdkMutex g_heap_mutex;
constinit FileSystemBuddyHeap g_heap;
std::atomic<size_t> g_retry_count;
std::atomic<size_t> g_reduce_allocation_count;
constinit std::atomic<size_t> g_retry_count;
constinit std::atomic<size_t> g_reduce_allocation_count;
void *g_heap_buffer;
size_t g_heap_size;
size_t g_heap_free_size_peak;
constinit void *g_heap_buffer;
constinit size_t g_heap_size;
constinit size_t g_heap_free_size_peak;
AdditionalDeviceAddressEntry g_additional_device_address_entry;
constinit AdditionalDeviceAddressEntry g_additional_device_address_entry;
}

View File

@@ -25,12 +25,12 @@ namespace ams::fssystem {
private:
using BlockCache = LruListCache<s64, char *>;
private:
os::Mutex mutex;
os::SdkMutex mutex;
BlockCache block_cache;
fs::IStorage * const base_storage;
s32 block_size;
public:
ReadOnlyBlockCacheStorage(IStorage *bs, s32 bsz, char *buf, size_t buf_size, s32 cache_block_count) : mutex(false), base_storage(bs), block_size(bsz) {
ReadOnlyBlockCacheStorage(IStorage *bs, s32 bsz, char *buf, size_t buf_size, s32 cache_block_count) : mutex(), base_storage(bs), block_size(bsz) {
/* Validate preconditions. */
AMS_ASSERT(buf_size >= static_cast<size_t>(this->block_size));
AMS_ASSERT(util::IsPowerOfTwo(this->block_size));

View File

@@ -27,7 +27,7 @@ namespace ams::fssystem::save {
this->Finalize();
}
Result BlockCacheBufferedStorage::Initialize(IBufferManager *bm, os::Mutex *mtx, IStorage *data, s64 data_size, size_t verif_block_size, s32 max_cache_entries, bool is_real_data, s8 buffer_level, bool is_keep_burst_mode, fs::StorageType storage_type) {
Result BlockCacheBufferedStorage::Initialize(IBufferManager *bm, os::SdkRecursiveMutex *mtx, IStorage *data, s64 data_size, size_t verif_block_size, s32 max_cache_entries, bool is_real_data, s8 buffer_level, bool is_keep_burst_mode, fs::StorageType storage_type) {
/* Validate preconditions. */
AMS_ASSERT(data != nullptr);
AMS_ASSERT(bm != nullptr);

View File

@@ -582,7 +582,7 @@ namespace ams::fssystem::save {
}
};
BufferedStorage::BufferedStorage() : base_storage(), buffer_manager(), block_size(), base_storage_size(), caches(), cache_count(), next_acquire_cache(), next_fetch_cache(), mutex(false), bulk_read_enabled() {
BufferedStorage::BufferedStorage() : base_storage(), buffer_manager(), block_size(), base_storage_size(), caches(), cache_count(), next_acquire_cache(), next_fetch_cache(), mutex(), bulk_read_enabled() {
/* ... */
}

View File

@@ -150,7 +150,7 @@ namespace ams::fssystem::save {
this->storage = fs::SubStorage();
}
Result HierarchicalIntegrityVerificationStorage::Initialize(const HierarchicalIntegrityVerificationInformation &info, HierarchicalStorageInformation storage, FileSystemBufferManagerSet *bufs, os::Mutex *mtx, fs::StorageType storage_type) {
Result HierarchicalIntegrityVerificationStorage::Initialize(const HierarchicalIntegrityVerificationInformation &info, HierarchicalStorageInformation storage, FileSystemBufferManagerSet *bufs, os::SdkRecursiveMutex *mtx, fs::StorageType storage_type) {
/* Validate preconditions. */
AMS_ASSERT(bufs != nullptr);
AMS_ASSERT(IntegrityMinLayerCount <= info.max_layers && info.max_layers <= IntegrityMaxLayerCount);

View File

@@ -20,8 +20,8 @@ namespace ams::hid {
namespace {
/* Global lock. */
os::Mutex g_hid_lock(false);
bool g_initialized_hid = false;
constinit os::SdkMutex g_hid_lock;
constinit bool g_initialized_hid = false;
/* Set of supported NpadIds (we want to read from any connected controllers). */
constexpr const HidNpadIdType NpadIdTypes[] = {

View File

@@ -30,13 +30,13 @@ namespace ams::lmem::impl {
public:
explicit ScopedHeapLock(HeapHandle h) : handle(h) {
if (this->handle->option & CreateOption_ThreadSafe) {
os::LockMutex(std::addressof(this->handle->mutex));
os::LockSdkMutex(std::addressof(this->handle->mutex));
}
}
~ScopedHeapLock() {
if (this->handle->option & CreateOption_ThreadSafe) {
os::UnlockMutex(std::addressof(this->handle->mutex));
os::UnlockSdkMutex(std::addressof(this->handle->mutex));
}
}
};

View File

@@ -427,6 +427,7 @@ namespace ams::lmem::impl {
const s32 abs_alignment = std::abs(alignment);
AMS_ASSERT((abs_alignment & (abs_alignment - 1)) == 0);
AMS_ASSERT(MinimumAlignment <= static_cast<size_t>(abs_alignment));
AMS_UNUSED(abs_alignment);
/* Fix size to be correctly aligned. */
if (size == 0) {

View File

@@ -21,15 +21,12 @@ namespace ams::lmem {
HeapHandle CreateExpHeap(void *address, size_t size, u32 option) {
HeapHandle handle = impl::CreateExpHeap(address, size, option);
if (option & CreateOption_ThreadSafe) {
os::InitializeMutex(std::addressof(handle->mutex), false, 0);
os::InitializeSdkMutex(std::addressof(handle->mutex));
}
return handle;
}
void DestroyExpHeap(HeapHandle handle) {
if (handle->option & CreateOption_ThreadSafe) {
os::FinalizeMutex(std::addressof(handle->mutex));
}
impl::DestroyExpHeap(handle);
}

View File

@@ -21,7 +21,7 @@ namespace ams::lmem {
HeapHandle CreateUnitHeap(void *address, size_t size, size_t unit_size, u32 option) {
HeapHandle handle = impl::CreateUnitHeap(address, size, unit_size, DefaultAlignment, static_cast<u16>(option), InfoPlacement_Head, nullptr);
if (option & CreateOption_ThreadSafe) {
os::InitializeMutex(std::addressof(handle->mutex), false, 0);
os::InitializeSdkMutex(std::addressof(handle->mutex));
}
return handle;
}
@@ -29,7 +29,7 @@ namespace ams::lmem {
HeapHandle CreateUnitHeap(void *address, size_t size, size_t unit_size, u32 option, s32 alignment, InfoPlacement info_placement) {
HeapHandle handle = impl::CreateUnitHeap(address, size, unit_size, alignment, static_cast<u16>(option), info_placement, nullptr);
if (option & CreateOption_ThreadSafe) {
os::InitializeMutex(std::addressof(handle->mutex), false, 0);
os::InitializeSdkMutex(std::addressof(handle->mutex));
}
return handle;
}
@@ -37,15 +37,12 @@ namespace ams::lmem {
HeapHandle CreateUnitHeap(void *address, size_t size, size_t unit_size, u32 option, s32 alignment, HeapCommonHead *heap_head) {
HeapHandle handle = impl::CreateUnitHeap(address, size, unit_size, alignment, static_cast<u16>(option), InfoPlacement_Head, heap_head);
if (option & CreateOption_ThreadSafe) {
os::InitializeMutex(std::addressof(handle->mutex), false, 0);
os::InitializeSdkMutex(std::addressof(handle->mutex));
}
return handle;
}
void DestroyUnitHeap(HeapHandle handle) {
if (handle->option & CreateOption_ThreadSafe) {
os::FinalizeMutex(std::addressof(handle->mutex));
}
impl::DestroyUnitHeap(handle);
}

View File

@@ -201,14 +201,14 @@ namespace ams::mem::impl::heap {
s32 static_thread_quota;
s32 dynamic_thread_quota;
bool use_virtual_memory;
os::Mutex lock;
os::SdkRecursiveMutex lock;
ListHeader<SpanPage> spanpage_list;
ListHeader<SpanPage> full_spanpage_list;
ListHeader<Span> freelists[FreeListCount];
FreeListAvailableWord freelists_bitmap[NumFreeListBitmaps];
ListHeader<Span> smallmem_lists[TlsHeapStatic::NumClassInfo];
public:
TlsHeapCentral() : lock(true) {
TlsHeapCentral() : lock() {
this->span_table.total_pages = 0;
}

View File

@@ -635,7 +635,7 @@ namespace ams::ncm {
for (s32 i = 0; i < count; i++) {
R_UNLESS(!this->IsCancelRequested(), ncm::ResultCreatePlaceHolderCancelled());
static os::Mutex s_placeholder_mutex(false);
static constinit os::SdkMutex s_placeholder_mutex;
std::scoped_lock lk(s_placeholder_mutex);
InstallContentMeta content_meta;

View File

@@ -32,7 +32,7 @@ namespace ams::ncm {
std::array<CacheEntry, MaxCacheEntries> caches;
PathString *root_path;
u64 cur_counter;
os::Mutex cache_mutex;
os::SdkMutex cache_mutex;
MakePlaceHolderPathFunction make_placeholder_path_func;
bool delay_flush;
private:
@@ -43,7 +43,7 @@ namespace ams::ncm {
CacheEntry *FindInCache(PlaceHolderId placeholder_id);
CacheEntry *GetFreeEntry();;
public:
PlaceHolderAccessor() : cur_counter(0), cache_mutex(false), delay_flush(false) {
PlaceHolderAccessor() : cur_counter(0), cache_mutex(), delay_flush(false) {
for (size_t i = 0; i < MaxCacheEntries; i++) {
caches[i].id = InvalidPlaceHolderId;
}

View File

@@ -31,8 +31,8 @@ namespace ams::patcher {
constexpr size_t ModuleIpsPatchLength = 2 * sizeof(ro::ModuleId) + IpsFileExtensionLength;
/* Global data. */
os::Mutex apply_patch_lock(false);
u8 g_patch_read_buffer[os::MemoryPageSize];
constinit os::SdkMutex g_apply_patch_lock;
constinit u8 g_patch_read_buffer[os::MemoryPageSize];
/* Helpers. */
inline u8 ConvertHexNybble(const char nybble) {
@@ -213,7 +213,7 @@ namespace ams::patcher {
void LocateAndApplyIpsPatchesToModule(const char *mount_name, const char *patch_dir_name, size_t protected_size, size_t offset, const ro::ModuleId *module_id, u8 *mapped_module, size_t mapped_size) {
/* Ensure only one thread tries to apply patches at a time. */
std::scoped_lock lk(apply_patch_lock);
std::scoped_lock lk(g_apply_patch_lock);
/* Inspect all patches from /atmosphere/<patch_dir>/<*>/<*>.ips */
char path[fs::EntryNameLengthMax + 1];

View File

@@ -106,7 +106,7 @@ namespace ams::sf::cmif {
return entry->object.Clone();
}
ServerDomainManager::EntryManager::EntryManager(DomainEntryStorage *entry_storage, size_t entry_count) : lock(false) {
ServerDomainManager::EntryManager::EntryManager(DomainEntryStorage *entry_storage, size_t entry_count) : lock() {
this->entries = reinterpret_cast<Entry *>(entry_storage);
this->num_entries = entry_count;
for (size_t i = 0; i < this->num_entries; i++) {

View File

@@ -21,17 +21,17 @@ namespace ams::sm::impl {
namespace {
/* Globals. */
constinit os::Mutex g_mitm_ack_session_mutex(true);
constinit os::Mutex g_per_thread_session_mutex(true);
constinit os::SdkRecursiveMutex g_mitm_ack_session_mutex;
constinit os::SdkRecursiveMutex g_per_thread_session_mutex;
}
/* Utilities. */
os::Mutex &GetMitmAcknowledgementSessionMutex() {
os::SdkRecursiveMutex &GetMitmAcknowledgementSessionMutex() {
return g_mitm_ack_session_mutex;
}
os::Mutex &GetPerThreadSessionMutex() {
os::SdkRecursiveMutex &GetPerThreadSessionMutex() {
return g_per_thread_session_mutex;
}

View File

@@ -21,8 +21,8 @@
namespace ams::sm::impl {
/* Utilities. */
os::Mutex &GetMitmAcknowledgementSessionMutex();
os::Mutex &GetPerThreadSessionMutex();
os::SdkRecursiveMutex &GetMitmAcknowledgementSessionMutex();
os::SdkRecursiveMutex &GetPerThreadSessionMutex();
template<typename F>
Result DoWithMitmAcknowledgementSession(F f) {