Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify platform abstraction #2107

Open
3 tasks
elBoberido opened this issue Nov 20, 2023 · 1 comment
Open
3 tasks

Simplify platform abstraction #2107

elBoberido opened this issue Nov 20, 2023 · 1 comment
Labels
refactoring Refactor code without adding features

Comments

@elBoberido
Copy link
Member

elBoberido commented Nov 20, 2023

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

  • Design document
  • Implementation
  • Update website/advanced/custom-iceoryx-platform.md
@elBoberido elBoberido added the refactoring Refactor code without adding features label Nov 20, 2023
@elBoberido
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Refactor code without adding features
Projects
Status: To do
Development

No branches or pull requests

1 participant