Files
parallel-ps2/common/StringUtil.cpp
T
libretroadmin 53f62899ca libretro-common: resync, and WorkSema on the asymmetric eventcount
Three things, and the middle one is the reason for the other two.

libretro-common is brought level with RetroArch master. Five files
differed and one of them stays as it is -- include/compat/ifaddrs.h keeps
the extern "C" guard ps2 added for DEV9, which is not upstream and without
which the core fails to link; the other four are RetroArch being ahead,
checked line by line. Three files are new: retro_procbarrier.c, the
process-wide barrier, retro_eventcount.c, and retro_asym_eventcount.c,
which is built on both. Makefile.common builds all three.

WorkSema is rewritten on two of those. It was a hand-rolled state
machine whose NotifyOfWork -- what the EE pays per GS packet with the GS
thread awake, and what MTVU's own comment already named as the fetch_add
on its hot path -- measured 6.4 ns. It is now retro_asym_eventcount's
notify, a release store and a relaxed load with no lock prefix, measured
1.2 ns in the same binary and the same run. The symmetric retro_eventcount
was measured too, at 9.6 ns, and would have been a regression: it pays an
RMW and a seq_cst load to serve many producers, and each WorkSema here
has one. The ordering the hot side drops is paid where WaitForWork parks,
by retro_procbarrier; where no barrier exists the object runs the
symmetric protocol for its whole life, decided at init, and MTGS never
needs to know. WaitForEmpty's direction -- worker to producer, once a
frame -- is the plain retro_eventcount, whose prepare/commit contract is
what STATE_FLAG_WAITING_EMPTY was reinventing.

The seven operations keep their contract exactly, and tests/spsc/
worksema.cpp pins each: CheckForWork consumes a burst of notifies as one;
WaitForWork returns at once with work pending and parks without; a timed
wait is false only on a timeout with nothing pending; Kill unblocks every
waiter and makes WaitForEmpty false; Reset revives. One subtlety is
pinned because it bit the test first: "empty" means the worker went idle
after consuming, not merely consumed, and a WaitForEmpty issued between
the two blocks -- the old state machine did the same, checked. The
producer/consumer pair runs 50000 items with WaitForEmpty interleaved
2858 times and never sees it return with work outstanding. Plain, TSan,
ASan and UBSan are all clean; TSan sees the eventcounts' atomics, not the
barrier, so the lost-wake half is only load-bearing on more than one
core.

The last part is what the rewrite made dead, plus what an audit of every
common/ object against every importer in the finished build found
alongside it: KernelSemaphore::WaitFor, whose only caller was the old
timed wait; Mutex::TryLock; both StringUtil::ReplaceAll overloads; and
DecompressBlockBC4 and BC5 with their private helper and the two
Int8ToFloat conversions that served only them -- the replacement loader
dispatches BC1, BC2, BC3 and BC7 and nothing else. Every header in
common/ is still included by something, so nothing is removed at file
level; the audit was at symbol level and discounted vtable dispatch,
which is why SettingsInterface's methods, which nm cannot see a caller
for, are untouched.

The core links, all twelve suites pass, and the patch harness gains the
three objects its Semaphore.cpp link now needs.
2026-09-17 06:06:52 +02:00

179 lines
4.5 KiB
C++

/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "StringUtil.h"
#include <cctype>
#include <codecvt>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <utility>
#ifdef _WIN32
#include "RedtapeWindows.h"
#endif
namespace StringUtil
{
std::string StdStringFromFormat(const char* format, ...)
{
std::va_list ap;
va_start(ap, format);
std::string ret = StdStringFromFormatV(format, ap);
va_end(ap);
return ret;
}
std::string StdStringFromFormatV(const char* format, std::va_list ap)
{
std::va_list ap_copy;
va_copy(ap_copy, ap);
#ifdef _WIN32
int len = _vscprintf(format, ap_copy);
#else
int len = std::vsnprintf(nullptr, 0, format, ap_copy);
#endif
va_end(ap_copy);
std::string ret;
// If an encoding error occurs, len is -1. Which we definitely don't want to resize to.
if (len > 0)
{
ret.resize(len + 1);
std::vsnprintf(ret.data(), ret.size(), format, ap);
ret.resize(len);
}
return ret;
}
std::string toLower(const std::string_view& input)
{
std::string newStr;
std::transform(input.begin(), input.end(), std::back_inserter(newStr),
[](unsigned char c) { return std::tolower(c); });
return newStr;
}
std::string_view StripWhitespace(const std::string_view& str)
{
std::string_view::size_type start = 0;
while (start < str.size() && std::isspace(str[start]))
start++;
if (start == str.size())
return {};
std::string_view::size_type end = str.size() - 1;
while (end > start && std::isspace(str[end]))
end--;
return str.substr(start, end - start + 1);
}
void StripWhitespace(std::string* str)
{
{
const char* cstr = str->c_str();
std::string_view::size_type start = 0;
while (start < str->size() && std::isspace(cstr[start]))
start++;
if (start != 0)
str->erase(0, start);
}
{
const char* cstr = str->c_str();
std::string_view::size_type start = str->size();
while (start > 0 && std::isspace(cstr[start - 1]))
start--;
if (start != str->size())
str->erase(start);
}
}
std::vector<std::string_view> SplitString(const std::string_view& str, char delimiter, bool skip_empty /*= true*/)
{
std::vector<std::string_view> res;
std::string_view::size_type last_pos = 0;
std::string_view::size_type pos;
while (last_pos < str.size() && (pos = str.find(delimiter, last_pos)) != std::string_view::npos)
{
std::string_view part(StripWhitespace(str.substr(last_pos, pos - last_pos)));
if (!skip_empty || !part.empty())
res.push_back(std::move(part));
last_pos = pos + 1;
}
if (last_pos < str.size())
{
std::string_view part(StripWhitespace(str.substr(last_pos)));
if (!skip_empty || !part.empty())
res.push_back(std::move(part));
}
return res;
}
bool ParseAssignmentString(const std::string_view& str, std::string_view* key, std::string_view* value)
{
const std::string_view::size_type pos = str.find('=');
if (pos == std::string_view::npos)
{
*key = std::string_view();
*value = std::string_view();
return false;
}
*key = StripWhitespace(str.substr(0, pos));
if (pos != (str.size() - 1))
*value = StripWhitespace(str.substr(pos + 1));
else
*value = std::string_view();
return true;
}
#ifdef _WIN32
std::string WideStringToUTF8String(const std::wstring_view& str)
{
std::string ret;
if (!WideStringToUTF8String(ret, str))
ret.clear();
return ret;
}
bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str)
{
int mblen = WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr);
if (mblen < 0)
return false;
dest.resize(mblen);
if (mblen > 0 && WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), dest.data(), mblen,
nullptr, nullptr) < 0)
{
return false;
}
return true;
}
#endif
} // namespace StringUtil