refactor: use std::mutex in ArchStrings

port b0e415de03
build: link base to arch
This commit is contained in:
Povilas Kanapickas 2025-06-10 19:37:04 -04:00 committed by Nick Bolton
parent fb3c8eb965
commit f89168d00a
3 changed files with 6 additions and 25 deletions

View file

@ -6,28 +6,21 @@
*/ */
#include "arch/ArchString.h" #include "arch/ArchString.h"
#include "arch/Arch.h"
#include <climits> #include <climits>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <mutex>
static ArchMutex s_mutex = nullptr; std::mutex s_mutex;
// //
// use C library non-reentrant multibyte conversion with mutex // use C library non-reentrant multibyte conversion with mutex
// //
ArchString::~ArchString()
{
if (s_mutex != nullptr) {
ARCH->closeMutex(s_mutex);
s_mutex = nullptr;
}
}
int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool *errors) const int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool *errors) const
{ {
std::lock_guard<std::mutex> lock(s_mutex);
ptrdiff_t len = 0; ptrdiff_t len = 0;
bool dummyErrors; bool dummyErrors;
@ -36,12 +29,6 @@ int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool
} }
*errors = false; *errors = false;
if (s_mutex == nullptr) {
s_mutex = ARCH->newMutex();
}
ARCH->lockMutex(s_mutex);
if (dst == nullptr) { if (dst == nullptr) {
char dummy[MB_LEN_MAX]; char dummy[MB_LEN_MAX];
const wchar_t *scan = src; const wchar_t *scan = src;
@ -75,7 +62,6 @@ int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool
} }
len = dst - dst0; len = dst - dst0;
} }
ARCH->unlockMutex(s_mutex);
return static_cast<int>(len); return static_cast<int>(len);
} }
@ -91,6 +77,7 @@ ArchString::EWideCharEncoding ArchString::getWideCharEncoding() const
int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool *errors) const int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool *errors) const
{ {
std::lock_guard<std::mutex> lock(s_mutex);
ptrdiff_t len = 0; ptrdiff_t len = 0;
wchar_t dummy; wchar_t dummy;
@ -100,12 +87,6 @@ int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool
} }
*errors = false; *errors = false;
if (s_mutex == nullptr) {
s_mutex = ARCH->newMutex();
}
ARCH->lockMutex(s_mutex);
if (dst == nullptr) { if (dst == nullptr) {
const char *scan = src; const char *scan = src;
while (n > 0) { while (n > 0) {
@ -177,7 +158,6 @@ int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool
} }
len = dst - dst0; len = dst - dst0;
} }
ARCH->unlockMutex(s_mutex);
return static_cast<int>(len); return static_cast<int>(len);
} }

View file

@ -24,7 +24,7 @@ public:
ArchString() = default; ArchString() = default;
ArchString(const ArchString &) = delete; ArchString(const ArchString &) = delete;
ArchString(ArchString &&) = delete; ArchString(ArchString &&) = delete;
~ArchString() override; ~ArchString() = default;
ArchString &operator=(const ArchString &) = delete; ArchString &operator=(const ArchString &) = delete;
ArchString &operator=(ArchString &&) = delete; ArchString &operator=(ArchString &&) = delete;

View file

@ -41,3 +41,4 @@ add_library(base STATIC
XBase.h XBase.h
) )
target_link_libraries(base PRIVATE arch)