mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-06 17:41:04 +00:00
IPCChannelEnd: make the signature of makeA and makeB platform independent
and let the IPCChannelEnd own the shared mem and other stuff
This commit is contained in:
parent
bda818840e
commit
8cdf8ab95a
3 changed files with 104 additions and 48 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Minetest
|
Minetest
|
||||||
Copyright (C) 2022 Desour <vorunbekannt75@web.de>
|
Copyright (C) 2022 DS
|
||||||
Copyright (C) 2022 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
|
Copyright (C) 2022 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
@ -188,6 +188,37 @@ static struct timespec *set_timespec(struct timespec *ts, int ms)
|
||||||
}
|
}
|
||||||
#endif // !defined(_WIN32)
|
#endif // !defined(_WIN32)
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
IPCChannelEnd IPCChannelEnd::makeA(std::unique_ptr<IPCChannelStuff> stuff)
|
||||||
|
{
|
||||||
|
IPCChannelShared *shared = stuff->getShared();
|
||||||
|
HANDLE sem_a = stuff->getSemA();
|
||||||
|
HANDLE sem_b = stuff->getSemB();
|
||||||
|
return IPCChannelEnd(std::move(stuff), &shared->a, &shared->b, sem_a, sem_b);
|
||||||
|
}
|
||||||
|
|
||||||
|
IPCChannelEnd IPCChannelEnd::makeB(std::unique_ptr<IPCChannelStuff> stuff)
|
||||||
|
{
|
||||||
|
IPCChannelShared *shared = stuff->getShared();
|
||||||
|
HANDLE sem_a = stuff->getSemA();
|
||||||
|
HANDLE sem_b = stuff->getSemB();
|
||||||
|
return IPCChannelEnd(std::move(stuff), &shared->b, &shared->a, sem_b, sem_a);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else // defined(_WIN32)
|
||||||
|
IPCChannelEnd IPCChannelEnd::makeA(std::unique_ptr<IPCChannelStuff> stuff)
|
||||||
|
{
|
||||||
|
IPCChannelShared *shared = stuff->getShared();
|
||||||
|
return IPCChannelEnd(std::move(stuff), &shared->a, &shared->b);
|
||||||
|
}
|
||||||
|
|
||||||
|
IPCChannelEnd IPCChannelEnd::makeB(std::unique_ptr<IPCChannelStuff> stuff)
|
||||||
|
{
|
||||||
|
IPCChannelShared *shared = stuff->getShared();
|
||||||
|
return IPCChannelEnd(std::move(stuff), &shared->b, &shared->a);
|
||||||
|
}
|
||||||
|
#endif // !defined(_WIN32)
|
||||||
|
|
||||||
bool IPCChannelEnd::sendSmall(const void *data, size_t size) noexcept
|
bool IPCChannelEnd::sendSmall(const void *data, size_t size) noexcept
|
||||||
{
|
{
|
||||||
m_out->size = size;
|
m_out->size = size;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Minetest
|
Minetest
|
||||||
Copyright (C) 2022 Desour <vorunbekannt75@web.de>
|
Copyright (C) 2022 DS
|
||||||
Copyright (C) 2022 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
|
Copyright (C) 2022 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
@ -21,9 +21,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "irrlichttypes.h"
|
#include "irrlichttypes.h"
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
|
@ -47,7 +49,7 @@ struct IPCChannelBuffer
|
||||||
{
|
{
|
||||||
#if !defined(_WIN32)
|
#if !defined(_WIN32)
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
std::atomic<u32> futex = ATOMIC_VAR_INIT(0U);
|
std::atomic<u32> futex{0};
|
||||||
#else
|
#else
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
@ -56,7 +58,7 @@ struct IPCChannelBuffer
|
||||||
#endif
|
#endif
|
||||||
#endif // !defined(_WIN32)
|
#endif // !defined(_WIN32)
|
||||||
size_t size;
|
size_t size;
|
||||||
u8 data[IPC_CHANNEL_MSG_SIZE];
|
u8 data[IPC_CHANNEL_MSG_SIZE]; //TODO: volatile?
|
||||||
|
|
||||||
IPCChannelBuffer();
|
IPCChannelBuffer();
|
||||||
~IPCChannelBuffer();
|
~IPCChannelBuffer();
|
||||||
|
@ -68,32 +70,25 @@ struct IPCChannelShared
|
||||||
IPCChannelBuffer b;
|
IPCChannelBuffer b;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// opaque owner for the shared mem and stuff
|
||||||
|
// users have to implement this
|
||||||
|
struct IPCChannelStuff
|
||||||
|
{
|
||||||
|
virtual ~IPCChannelStuff() = default;
|
||||||
|
virtual IPCChannelShared *getShared() = 0;
|
||||||
|
#ifdef _WIN32
|
||||||
|
virtual HANDLE getSemA() = 0;
|
||||||
|
virtual HANDLE getSemB() = 0;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
class IPCChannelEnd
|
class IPCChannelEnd
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IPCChannelEnd() = default;
|
IPCChannelEnd() = default;
|
||||||
|
|
||||||
#if defined(_WIN32)
|
static IPCChannelEnd makeA(std::unique_ptr<IPCChannelStuff> stuff);
|
||||||
static IPCChannelEnd makeA(IPCChannelShared *shared, HANDLE sem_a, HANDLE sem_b)
|
static IPCChannelEnd makeB(std::unique_ptr<IPCChannelStuff> stuff);
|
||||||
{
|
|
||||||
return IPCChannelEnd(&shared->a, &shared->b, sem_a, sem_b);
|
|
||||||
}
|
|
||||||
|
|
||||||
static IPCChannelEnd makeB(IPCChannelShared *shared, HANDLE sem_a, HANDLE sem_b)
|
|
||||||
{
|
|
||||||
return IPCChannelEnd(&shared->b, &shared->a, sem_b, sem_a);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static IPCChannelEnd makeA(IPCChannelShared *shared)
|
|
||||||
{
|
|
||||||
return IPCChannelEnd(&shared->a, &shared->b);
|
|
||||||
}
|
|
||||||
|
|
||||||
static IPCChannelEnd makeB(IPCChannelShared *shared)
|
|
||||||
{
|
|
||||||
return IPCChannelEnd(&shared->b, &shared->a);
|
|
||||||
}
|
|
||||||
#endif // !defined(_WIN32)
|
|
||||||
|
|
||||||
// If send, recv, or exchange return false, stop using the channel.
|
// If send, recv, or exchange return false, stop using the channel.
|
||||||
// Note: timeouts may be for receiving any response, not a whole message.
|
// Note: timeouts may be for receiving any response, not a whole message.
|
||||||
|
@ -120,17 +115,28 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
IPCChannelEnd(IPCChannelBuffer *in, IPCChannelBuffer *out, HANDLE sem_in, HANDLE sem_out):
|
IPCChannelEnd(
|
||||||
m_in(in), m_out(out), m_sem_in(sem_in), m_sem_out(sem_out)
|
std::unique_ptr<IPCChannelStuff> stuff,
|
||||||
|
IPCChannelBuffer *in, IPCChannelBuffer *out,
|
||||||
|
HANDLE sem_in, HANDLE sem_out) :
|
||||||
|
m_stuff(std::move(stuff)),
|
||||||
|
m_in(in), m_out(out),
|
||||||
|
m_sem_in(sem_in), m_sem_out(sem_out)
|
||||||
{}
|
{}
|
||||||
#else
|
#else
|
||||||
IPCChannelEnd(IPCChannelBuffer *in, IPCChannelBuffer *out): m_in(in), m_out(out) {}
|
IPCChannelEnd(
|
||||||
|
std::unique_ptr<IPCChannelStuff> stuff,
|
||||||
|
IPCChannelBuffer *in, IPCChannelBuffer *out) :
|
||||||
|
m_stuff(std::move(stuff)),
|
||||||
|
m_in(in), m_out(out)
|
||||||
|
{}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool sendSmall(const void *data, size_t size) noexcept;
|
bool sendSmall(const void *data, size_t size) noexcept;
|
||||||
|
|
||||||
bool sendLarge(const void *data, size_t size, int timeout_ms) noexcept;
|
bool sendLarge(const void *data, size_t size, int timeout_ms) noexcept;
|
||||||
|
|
||||||
|
std::unique_ptr<IPCChannelStuff> m_stuff;
|
||||||
IPCChannelBuffer *m_in = nullptr;
|
IPCChannelBuffer *m_in = nullptr;
|
||||||
IPCChannelBuffer *m_out = nullptr;
|
IPCChannelBuffer *m_out = nullptr;
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
|
@ -236,28 +236,53 @@ void TestThreading::testTLS()
|
||||||
|
|
||||||
void TestThreading::testIPCChannel()
|
void TestThreading::testIPCChannel()
|
||||||
{
|
{
|
||||||
|
struct Stuff
|
||||||
|
{
|
||||||
|
IPCChannelShared shared{};
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
HANDLE sem_a = CreateSemaphoreA(nullptr, 0, 1, nullptr);
|
HANDLE sem_a;
|
||||||
UASSERT(sem_a != INVALID_HANDLE_VALUE);
|
HANDLE sem_b;
|
||||||
|
|
||||||
HANDLE sem_b = CreateSemaphoreA(nullptr, 0, 1, nullptr);
|
|
||||||
UASSERT(sem_b != INVALID_HANDLE_VALUE);
|
|
||||||
#endif
|
#endif
|
||||||
|
Stuff()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
HANDLE sem_a = CreateSemaphoreA(nullptr, 0, 1, nullptr);
|
||||||
|
UASSERT(sem_a != INVALID_HANDLE_VALUE);
|
||||||
|
|
||||||
IPCChannelShared shared, *sharedp = &shared;
|
HANDLE sem_b = CreateSemaphoreA(nullptr, 0, 1, nullptr);
|
||||||
|
UASSERT(sem_b != INVALID_HANDLE_VALUE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
~Stuff()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
CloseHandle(sem_b);
|
||||||
|
CloseHandle(sem_a);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IPCChannelStuffSingleProcess final : public IPCChannelStuff
|
||||||
|
{
|
||||||
|
std::shared_ptr<Stuff> stuff;
|
||||||
|
|
||||||
|
IPCChannelStuffSingleProcess(std::shared_ptr<Stuff> stuff) : stuff(std::move(stuff)) {}
|
||||||
|
~IPCChannelStuffSingleProcess() override = default;
|
||||||
|
|
||||||
|
IPCChannelShared *getShared() override { return &stuff->shared; }
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
IPCChannelEnd end_a = IPCChannelEnd::makeA(sharedp, sem_a, sem_b);
|
HANDLE getSemA() override { return stuff->sem_a; }
|
||||||
#else
|
HANDLE getSemB() override { return stuff->sem_b; }
|
||||||
IPCChannelEnd end_a = IPCChannelEnd::makeA(sharedp);
|
|
||||||
#endif
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
auto stuff = std::make_shared<Stuff>();
|
||||||
|
|
||||||
|
IPCChannelEnd end_a = IPCChannelEnd::makeA(std::make_unique<IPCChannelStuffSingleProcess>(stuff));
|
||||||
|
|
||||||
std::thread thread_b([=] {
|
std::thread thread_b([=] {
|
||||||
#if defined(_WIN32)
|
IPCChannelEnd end_b = IPCChannelEnd::makeB(std::make_unique<IPCChannelStuffSingleProcess>(stuff));
|
||||||
IPCChannelEnd end_b = IPCChannelEnd::makeB(sharedp, sem_a, sem_b);
|
|
||||||
#else
|
|
||||||
IPCChannelEnd end_b = IPCChannelEnd::makeB(sharedp);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
end_b.recv();
|
end_b.recv();
|
||||||
|
@ -281,10 +306,4 @@ void TestThreading::testIPCChannel()
|
||||||
thread_b.join();
|
thread_b.join();
|
||||||
|
|
||||||
UASSERT(!end_a.exchange(buf, 0, 1000));
|
UASSERT(!end_a.exchange(buf, 0, 1000));
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
CloseHandle(sem_b);
|
|
||||||
|
|
||||||
CloseHandle(sem_a);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue