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

add some comments, and improve error handling

This commit is contained in:
Desour 2023-06-08 01:45:26 +02:00
parent 8cdf8ab95a
commit 5f13c0aa48
2 changed files with 55 additions and 32 deletions

View file

@ -41,6 +41,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
IPCChannelShared is situated in shared memory and is used by both ends of
the channel.
There are currently 3 implementations for synchronisation:
* win32: uses win32 semaphore
* linux: uses futex, and does busy waiting if on x86/x86_64
* other posix: uses posix mutex and condition variable
*/
#define IPC_CHANNEL_MSG_SIZE 8192U
@ -49,16 +54,21 @@ struct IPCChannelBuffer
{
#if !defined(_WIN32)
#if defined(__linux__)
// possible values:
// 0: futex is not posted. reader will check value before blocking => no
// notify needed when posting
// 1: futex is posted
// 2: futex is not posted. reader is waiting with futex syscall, and needs
// to be notified
std::atomic<u32> futex{0};
#else
pthread_cond_t cond;
pthread_mutex_t mutex;
// TODO: use atomic?
bool posted = false;
bool posted = false; // protected by mutex
#endif
#endif // !defined(_WIN32)
size_t size;
u8 data[IPC_CHANNEL_MSG_SIZE]; //TODO: volatile?
u8 data[IPC_CHANNEL_MSG_SIZE];
IPCChannelBuffer();
~IPCChannelBuffer();
@ -90,13 +100,14 @@ public:
static IPCChannelEnd makeA(std::unique_ptr<IPCChannelStuff> stuff);
static IPCChannelEnd makeB(std::unique_ptr<IPCChannelStuff> stuff);
// If send, recv, or exchange return false, stop using the channel.
// If send, recv, or exchange return false (=timeout), stop using the channel.
// Note: timeouts may be for receiving any response, not a whole message.
bool send(const void *data, size_t size, int timeout_ms = -1) noexcept
{
if (size <= IPC_CHANNEL_MSG_SIZE) {
return sendSmall(data, size);
sendSmall(data, size);
return true;
} else {
return sendLarge(data, size, timeout_ms);
}
@ -109,7 +120,7 @@ public:
return send(data, size, timeout_ms) && recv(timeout_ms);
}
// Get information about the last received message
// Get the content of the last received message
inline const void *getRecvData() const noexcept { return m_recv_data; }
inline size_t getRecvSize() const noexcept { return m_recv_size; }
@ -132,8 +143,9 @@ private:
{}
#endif
bool sendSmall(const void *data, size_t size) noexcept;
void sendSmall(const void *data, size_t size) noexcept;
// returns false on timeout
bool sendLarge(const void *data, size_t size, int timeout_ms) noexcept;
std::unique_ptr<IPCChannelStuff> m_stuff;
@ -143,7 +155,7 @@ private:
HANDLE m_sem_in;
HANDLE m_sem_out;
#endif
const void *m_recv_data;
size_t m_recv_size;
const void *m_recv_data = nullptr;
size_t m_recv_size = 0;
std::vector<u8> m_large_recv;
};