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

posix: abstract pthread_mutex_t as uint32_t #52087

Merged

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Nov 8, 2022

3 commits

  • posix: pthread: take care with pthread mutex resources
  • posix: mutex: abstract pthread_mutex_t as uint32_t
  • tests: posix: mutex: test to ensure there is no resource leakage

See individual commit messages for details.

@cfriedt cfriedt force-pushed the use-uint32-for-pthread-mutex branch 5 times, most recently from 972d09b to c675a04 Compare November 8, 2022 20:06
@cfriedt
Copy link
Member Author

cfriedt commented Nov 8, 2022

Compliance check failure is a false positive (pthread functions return non-negative errno values)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_create.html

@cfriedt cfriedt marked this pull request as ready for review November 8, 2022 20:57
@cfriedt cfriedt requested a review from pfalcon as a code owner November 8, 2022 20:57
@zephyrbot zephyrbot added the area: POSIX POSIX API Library label Nov 8, 2022
@cfriedt cfriedt force-pushed the use-uint32-for-pthread-mutex branch from c675a04 to 8d872af Compare November 8, 2022 21:43
@cfriedt
Copy link
Member Author

cfriedt commented Nov 9, 2022

  • improved to_posix_mutex() (use the MSB of pthread_mutex_t to indicate it has been initialized)

@cfriedt cfriedt force-pushed the use-uint32-for-pthread-mutex branch 4 times, most recently from 1c7c060 to 923a81d Compare November 9, 2022 04:44
@cfriedt cfriedt marked this pull request as draft November 9, 2022 14:35
@cfriedt cfriedt force-pushed the use-uint32-for-pthread-mutex branch 9 times, most recently from e27b2d8 to 1f1d9df Compare November 10, 2022 14:58
jeremybettis
jeremybettis previously approved these changes Nov 10, 2022
Previously, `pthread_mutex_init()` could not actually fail, and
destroying mutexes was a no-op, so it was missing in a couple of
places.

However, with the change of `pthread_mutex_t` to `uint32_t`, and
since those are embedded inside of `struct posix_thread` for the
time being, the pthread code needs to keep track that it is
relinquishes used mutex resources when a thread completes.

Signed-off-by: Chris Friedt <[email protected]>

/* Record the associated posix_mutex in mu and mark as initialized */
*mu = mark_pthread_mutex_initialized(bit);

Copy link
Member Author

@cfriedt cfriedt Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to do the same initialization that pthread_mutex_init() does. Actually, it should probably just be moved here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! (mostly, custom attributes are still applied in pthread_mutex_init()

@cfriedt
Copy link
Member Author

cfriedt commented Nov 11, 2022

  • move mutex initialization to to_posix_mutex()
  • use to_posix_mutex() instead of get_posix_mutex() in cond_wait() because passing in mutexes initialized with PTHREAD_MUTEX_INITIALIZER is a valid use case.

Consistent with the change of `pthread_t` from
`struct posix_thread` to `uint32_t`, we can now also abstract
`pthread_mutex_t` as `uint32_t` and separate `struct posix_mutex`
as an implementation detail, hidden from POSIX API consumers.

This change deprecates `PTHREAD_MUTEX_DEFINE()` in favour of the
(standardized) `PTHREAD_MUTEX_INITIALIZER`.

This change introduces `CONFIG_MAX_PTHREAD_MUTEX_COUNT`.

Signed-off-by: Chris Friedt <[email protected]>
Add a test to ensure that `pthread_mutex_t` can be used over
and over again and that there is no resource leakage with proper
usage.

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt
Copy link
Member Author

cfriedt commented Nov 13, 2022

  • add test test_posix_mutex_resource_exhausted()
  • add doxygen for test_posix_mutex_resource_exhausted(), test_posix_mutex_resource_leak()

@cfriedt
Copy link
Member Author

cfriedt commented Nov 14, 2022

@stephanosio - will need your superpowers to merge due to the compliance false positives.

@belussi
Copy link

belussi commented Nov 14, 2022

Will this solve this problem? #35986

@cfriedt
Copy link
Member Author

cfriedt commented Nov 14, 2022

Will this solve this problem? #35986

@belussi - Yes (partially). I've been fixing it in increments for the last 2 weeks or so

@stephanosio stephanosio merged commit 47d09c0 into zephyrproject-rtos:main Nov 14, 2022
@cfriedt cfriedt deleted the use-uint32-for-pthread-mutex branch November 14, 2022 20:58
@belussi
Copy link

belussi commented Nov 15, 2022

@cfriedt Good to hear that! Looks like POSIX implementation is a kind of blocker for using POSIX and C++ STL.

@cfriedt
Copy link
Member Author

cfriedt commented Nov 15, 2022

@cfriedt Good to hear that! Looks like POSIX implementation is a kind of blocker for using POSIX and C++ STL.

@belussi - the C++ STL works already and has very little dependency on POSIX (if any).

However, Newlib's (prototypical) c++11 thread support uses pthreads and requires automatic stack allocation.

That in itself is not so hard, but it really should be solved at the kernel (Zephyr) layer so that other threading implementations can simply re-use the same machinery.

That will eventually will be fixed by #44379.

Later, we can move to a direct c++ threading implementation without POSIX.

@PatrickM-ZS
Copy link

If this relates to #35986 could that issue be re-opened? I'm not using any C++ in my project. For now I've just commented out declarations of the conflicting types in _pthreadtypes.h but i'm keen to find a proper workaround, until i guess the newlib headers are introduced as mentioned on the POSIX roadmap (#51211)

@belussi
Copy link

belussi commented Nov 15, 2022

@PatrickM-ZS @cfriedt
We are seeing exactly the same issue as mentioned in #35986. Conflicting types error. I couldn't find any solution yet but will try removing _pthreadtypes.h, so only STL's POSIX declaration is seen.

@stephanosio
Copy link
Member

Please open a discussion instead of commenting on old pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: POSIX POSIX API Library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants