eclipse-iceoryx / iceoryx

Eclipse iceoryx™ - true zero-copy inter-process-communication
https://iceoryx.io
Apache License 2.0
1.65k stars 384 forks source link

Simplify platform abstraction #2107

Open elBoberido opened 10 months ago

elBoberido commented 10 months ago

Brief feature description

Currently one needs to provide all headers for all platform, even if they are simply forwarding to the system header. This can be simplified by the C++17 feature __has_include

Detailed information

The platform would provide a generic implementation of the header. If the specific platform does not provide a custoom header, the generic one would be used.

This is an example for iceoryx_platform/include/iox/platform/semaphor.hpp

#ifndef IOX_PLATFORM_SEMAPHORE_HPP
#define IOX_PLATFORM_SEMAPHORE_HPP

#if __has_include("iox/platform/override/semaphore.hpp")
#include "iox/platform/override/semaphore.hpp"
#else
#include "iox/platform/generic/semaphore.hpp"
#endif // __has_include

#endif // IOX_PLATFORM_SEMAPHORE_HPP

The generic implementation in iceoryx_platform/generic/include/iox/platform/generic/semaphore.hpp would look like this

#ifndef IOX_PLATFORM_GENERIC_SEMAPHORE_HPP
#define IOX_PLATFORM_GENERIC_SEMAPHORE_HPP

#include <cstdint>
#include <semaphore.h>
#include <sys/types.h>

using iox_sem_t = sem_t;

#define IOX_SEM_FAILED SEM_FAILED
constexpr uint32_t IOX_SEM_VALUE_MAX = SEM_VALUE_MAX;

#ifndef IOX_PLATFORM_OVERRIDE_SEM_GETVALUE
inline int iox_sem_getvalue(iox_sem_t* sem, int* sval)
{
    return sem_getvalue(sem, sval);
}
#endif

#ifndef IOX_PLATFORM_OVERRIDE_SEM_POST
inline int iox_sem_post(iox_sem_t* sem)
{
    return sem_post(sem);
}
#endif

// ...

#ifndef IOX_PLATFORM_OVERRIDE_SEM_UNLINK
inline int iox_sem_unlink(const char* name)
{
    return sem_unlink(name);
}
#endif

#endif // IOX_PLATFORM_GENERIC_SEMAPHORE_HPP

For Linux, Unix, QNX and potentially other POSIX operating systems it would work out of the box.

FreeRTOS would specify a iceoryx_platform/freertos/include/iox/platform/override/semaphore.hpp header and partially reuse the generic header

#ifndef IOX_PLATFORM_FREERTOS_SEMAPHORE_HPP
#define IOX_PLATFORM_FREERTOS_SEMAPHORE_HPP

#define SEM_FAILED nullptr
#define IOX_PLATFORM_OVERRIDE_SEM_UNLINK

#include "iox/platform/generic/semaphore.hpp"

inline int iox_sem_unlink(const char*)
{
    // Named semaphores are not supported in FreeRTOS+POSIX
    configASSERT(false);
    return 0;
}

#endif // IOX_PLATFORM_FREERTOS_SEMAPHORE_HPP

Windows (and macOS) would have a full re-implementation in iceoryx_platform/windows/iox/platform/override/semaphore.hpp

#ifndef IOX_PLATFORM_WINDOWS_SEMAPHORE_HPP
#define IOX_PLATFORM_WINDOWS_SEMAPHORE_HPP

// fully custom implementation without using the generic header

#endif // IOX_PLATFORM_WINDOWS_SEMAPHORE_HPP

To use an out-of-tree platform one just needs to specify the path to iceoryx_platform/os/ like it is nowadays but it would be much simpler to add an maintain a platform which is mostly compatible with the generic platform.

Tasks

elBoberido commented 10 months ago

@budrus @elfenpiff @FerdinandSpitzschnueffler @mossmaurice @MatthiasKillat @dkroenke any objections to the proposed refactoring of the platform? It would be part of a 4.0 release and nothing in the short term but we might make it a prerequisite for Android support.