FreeRTOS / Lab-Project-FreeRTOS-POSIX

This repository contains FreeRTOS+POSIX source code, which could be consumed as a submodule.
MIT License
95 stars 60 forks source link

conflict with clock function #9

Closed joaovitorteixeira closed 2 years ago

joaovitorteixeira commented 3 years ago

I'm working on a project that uses FreeRTOS + POSIX and IAR compiler. I need to implement some functions of time library, but FreeRTOS already implements the clock function. I wonder if adding the __weak descriptor would be the best solution. If so, why is it done this way?

Thanks for attention!

cobusve commented 3 years ago

Having some of the posix functions implemented by the toolchain is a very common problem.

Since __weak is not part of the C standard, and it's availability and behavior depends on the toolchain in use it is not a portable way to solve this problem. If you want to add a weak modifier for your project specifically it would be a reasonable solution as long as you are confined to a specific toolchain.

The general solution we use to resolve this is to remove the definitions of the conflicting types. The clock function is one of these that you can remove via configuration. The config item can be found here https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX/blob/32039f19330c5926b8e547de8544b4e0905dc72d/FreeRTOS-Plus-POSIX/include/portable/FreeRTOS_POSIX_portable_default.h#L89-L91

You can also look at this example for how to use these defines to remove the definitions of specific posix types. https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX/blob/32039f19330c5926b8e547de8544b4e0905dc72d/FreeRTOS-Plus-POSIX/include/portable/espressif/esp32_devkitc_esp_wrover_kit/FreeRTOS_POSIX_portable.h#L34-L46

joaovitorteixeira commented 3 years ago

About ___ weak, perfect! I understood. But the posixconfigENABLE_CLOCK_T setting does not remove the clock function, it does remove the clock_t type.

https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX/blob/0c6cec978cd8163c8640c7c157cccba457dedabc/include/FreeRTOS_POSIX/sys/types.h#L51-L53

https://github.com/FreeRTOS/Lab-Project-FreeRTOS-POSIX/blob/0c6cec978cd8163c8640c7c157cccba457dedabc/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_clock.c#L50-L55

The IAR documentation states that it is necessary to write some functions:

To make the time and date functions work, you must implement the functions clock, time32, time64, and __getzone.

I'm trying not to modify FreeRTOS, but I can add the descriptor __weak or posixconfigENABLE_CLOCK_T. I just wanted to report this conflict.

Thank you!

cobusve commented 3 years ago

I am going to make a PR to change that clock function into an example in a comment block so that people who need it can do a real implementation. The idea with turning off the type is that the compiler may actually be defining it which causes a conflict, when that is the case the function should be kept, but since this function seems to cause more pain than help, and it is essentially not implemented/not supported I think it is better to move this to an example in a comment block.

We may have to do this in 2 steps where we first warn of the deprecation.

joaovitorteixeira commented 3 years ago

Perfect! if i can help with anything let me know

dan4thewin commented 2 years ago

Given the apparent conclusion and age of this issue, I'll close it. If more attention is needed, please open a new issue. Thank you.