1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

use string_view?

This commit is contained in:
Desour 2024-10-06 15:45:15 +02:00
parent acdb29805b
commit 7e393e01da
4 changed files with 45 additions and 43 deletions

View file

@ -27,8 +27,8 @@ TEST_CASE("benchmark_ipc_channel")
// echos back messages. stops if "" is sent // echos back messages. stops if "" is sent
for (;;) { for (;;) {
end_b.recv(); end_b.recv();
end_b.send(end_b.getRecvData(), end_b.getRecvSize()); end_b.send(end_b.getRecvData());
if (end_b.getRecvSize() == 0) if (end_b.getRecvData().size() == 0)
break; break;
} }
}); });
@ -39,8 +39,8 @@ TEST_CASE("benchmark_ipc_channel")
BENCHMARK("simple_call_1", i) { BENCHMARK("simple_call_1", i) {
char buf[16] = {}; char buf[16] = {};
buf[i & 0xf] = i; buf[i & 0xf] = i;
end_a.exchange(buf, 16); end_a.exchange({buf, 16});
return reinterpret_cast<const u8 *>(end_a.getRecvData())[i & 0xf]; return end_a.getRecvData()[i & 0xf];
}; };
BENCHMARK("simple_call_1000", i) { BENCHMARK("simple_call_1000", i) {
@ -48,14 +48,14 @@ TEST_CASE("benchmark_ipc_channel")
buf[i & 0xf] = i; buf[i & 0xf] = i;
for (int k = 0; k < 1000; ++k) { for (int k = 0; k < 1000; ++k) {
buf[0] = k & 0xff; buf[0] = k & 0xff;
end_a.exchange(buf, 16); end_a.exchange({buf, 16});
} }
return reinterpret_cast<const u8 *>(end_a.getRecvData())[i & 0xf]; return end_a.getRecvData()[i & 0xf];
}; };
// stop thread_b // stop thread_b
end_a.exchange(nullptr, 0); end_a.exchange({nullptr, 0});
REQUIRE(end_a.getRecvSize() == 0); REQUIRE(end_a.getRecvData().size() == 0);
thread_b.join(); thread_b.join();
} }

View file

@ -278,35 +278,38 @@ IPCChannelEnd IPCChannelEnd::makeB(std::unique_ptr<IPCChannelResources> resource
#endif // !defined(IPC_CHANNEL_IMPLEMENTATION_WIN32) #endif // !defined(IPC_CHANNEL_IMPLEMENTATION_WIN32)
} }
void IPCChannelEnd::sendSmall(const void *data, size_t size) noexcept void IPCChannelEnd::sendSmall(std::string_view data) noexcept
{ {
write_once(&m_dir.buf_out->size, size); write_once(&m_dir.buf_out->size, data.size());
if (size != 0) if (data.size() != 0)
memcpy(m_dir.buf_out->data, data, size); memcpy(m_dir.buf_out->data, data.data(), data.size());
post_out(&m_dir); post_out(&m_dir);
} }
bool IPCChannelEnd::sendLarge(const void *data, size_t size, int timeout_ms) noexcept bool IPCChannelEnd::sendLarge(std::string_view data, int timeout_ms) noexcept
{ {
u64 timeout_ms_abs = timeout_ms < 0 ? 0 : porting::getTimeMs() + timeout_ms; u64 timeout_ms_abs = timeout_ms < 0 ? 0 : porting::getTimeMs() + timeout_ms;
write_once(&m_dir.buf_out->size, size); write_once(&m_dir.buf_out->size, data.size());
do { size_t size = data.size();
memcpy(m_dir.buf_out->data, data, IPC_CHANNEL_MSG_SIZE); const u8 *ptr = reinterpret_cast<const u8 *>(data.data());
while (size > IPC_CHANNEL_MSG_SIZE) {
memcpy(m_dir.buf_out->data, ptr, IPC_CHANNEL_MSG_SIZE);
post_out(&m_dir); post_out(&m_dir);
if (!wait_in(&m_dir, timeout_ms_abs)) if (!wait_in(&m_dir, timeout_ms_abs))
return false; return false;
size -= IPC_CHANNEL_MSG_SIZE; size -= IPC_CHANNEL_MSG_SIZE;
data = (u8 *)data + IPC_CHANNEL_MSG_SIZE; ptr = ptr + IPC_CHANNEL_MSG_SIZE;
} while (size > IPC_CHANNEL_MSG_SIZE); }
if (size != 0) if (size != 0)
memcpy(m_dir.buf_out->data, data, size); memcpy(m_dir.buf_out->data, ptr, size);
post_out(&m_dir); post_out(&m_dir);
return true; return true;

View file

@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <string_view>
#if defined(_WIN32) #if defined(_WIN32)
#define IPC_CHANNEL_IMPLEMENTATION_WIN32 #define IPC_CHANNEL_IMPLEMENTATION_WIN32
@ -193,20 +194,20 @@ public:
// Returns false on timeout // Returns false on timeout
[[nodiscard]] [[nodiscard]]
bool sendWithTimeout(const void *data, size_t size, int timeout_ms) noexcept bool sendWithTimeout(std::string_view data, int timeout_ms) noexcept
{ {
if (size <= IPC_CHANNEL_MSG_SIZE) { if (data.size() <= IPC_CHANNEL_MSG_SIZE) {
sendSmall(data, size); sendSmall(data);
return true; return true;
} else { } else {
return sendLarge(data, size, timeout_ms); return sendLarge(data, timeout_ms);
} }
} }
// Same as above // Same as above
void send(const void *data, size_t size) noexcept void send(std::string_view data) noexcept
{ {
(void)sendWithTimeout(data, size, -1); (void)sendWithTimeout(data, -1);
} }
// Returns false on timeout. // Returns false on timeout.
@ -223,33 +224,31 @@ public:
// Returns false on timeout // Returns false on timeout
// Otherwise returns true, and data is available via getRecvData(). // Otherwise returns true, and data is available via getRecvData().
[[nodiscard]] [[nodiscard]]
bool exchangeWithTimeout(const void *data, size_t size, int timeout_ms) noexcept bool exchangeWithTimeout(std::string_view data, int timeout_ms) noexcept
{ {
return sendWithTimeout(data, size, timeout_ms) return sendWithTimeout(data, timeout_ms)
&& recvWithTimeout(timeout_ms); && recvWithTimeout(timeout_ms);
} }
// Same as above // Same as above
void exchange(const void *data, size_t size) noexcept void exchange(std::string_view data) noexcept
{ {
(void)exchangeWithTimeout(data, size, -1); (void)exchangeWithTimeout(data, -1);
} }
// Get the content of the last received message // Get the content of the last received message
// TODO: u8 *, or string_view? std::string_view getRecvData() const noexcept
const void *getRecvData() const noexcept { return m_large_recv.data(); } { return {reinterpret_cast<const char *>(m_large_recv.data()), m_recv_size}; }
size_t getRecvSize() const noexcept { return m_recv_size; }
private: private:
IPCChannelEnd(std::unique_ptr<IPCChannelResources> resources, Dir dir) : IPCChannelEnd(std::unique_ptr<IPCChannelResources> resources, Dir dir) :
m_resources(std::move(resources)), m_dir(dir) m_resources(std::move(resources)), m_dir(dir)
{} {}
// TODO: u8 *, or string_view? void sendSmall(std::string_view data) noexcept;
void sendSmall(const void *data, size_t size) noexcept;
// returns false on timeout // returns false on timeout
bool sendLarge(const void *data, size_t size, int timeout_ms) noexcept; bool sendLarge(std::string_view data, int timeout_ms) noexcept;
std::unique_ptr<IPCChannelResources> m_resources; std::unique_ptr<IPCChannelResources> m_resources;
Dir m_dir; Dir m_dir;

View file

@ -237,8 +237,8 @@ void TestThreading::testIPCChannel()
// echos back messages. stops if "" is sent // echos back messages. stops if "" is sent
for (;;) { for (;;) {
UASSERT(end_b.recvWithTimeout(-1)); UASSERT(end_b.recvWithTimeout(-1));
UASSERT(end_b.sendWithTimeout(end_b.getRecvData(), end_b.getRecvSize(), -1)); UASSERT(end_b.sendWithTimeout(end_b.getRecvData(), -1));
if (end_b.getRecvSize() == 0) if (end_b.getRecvData().size() == 0)
break; break;
} }
}); });
@ -246,17 +246,17 @@ void TestThreading::testIPCChannel()
char buf[20000] = {}; char buf[20000] = {};
for (int i = sizeof(buf); i > 0; i -= 100) { for (int i = sizeof(buf); i > 0; i -= 100) {
buf[i - 1] = 123; buf[i - 1] = 123;
UASSERT(end_a.exchangeWithTimeout(buf, i, -1)); UASSERT(end_a.exchangeWithTimeout({buf, (size_t)i}, -1));
UASSERTEQ(int, end_a.getRecvSize(), i); UASSERTEQ(int, end_a.getRecvData().size(), i);
UASSERTEQ(int, ((const char *)end_a.getRecvData())[i - 1], 123); UASSERTEQ(int, end_a.getRecvData().data()[i - 1], 123);
} }
// stop thread_b // stop thread_b
UASSERT(end_a.exchangeWithTimeout(buf, 0, -1)); UASSERT(end_a.exchangeWithTimeout({buf, 0}, -1));
UASSERTEQ(int, end_a.getRecvSize(), 0); UASSERTEQ(int, end_a.getRecvData().size(), 0);
thread_b.join(); thread_b.join();
// other side dead ==> should time out // other side dead ==> should time out
UASSERT(!end_a.exchangeWithTimeout(buf, 0, 200)); UASSERT(!end_a.exchangeWithTimeout({buf, 0}, 200));
} }