kernel: sem: add K_SEM_MAX_LIMIT

Currently there is no way to distinguish between a caller
explicitly asking for a semaphore with a limit that
happens to be `UINT_MAX` and a semaphore that just
has a limit "as large as possible".

Add `K_SEM_MAX_LIMIT`, currently defined to `UINT_MAX`, and akin
to `K_FOREVER` versus just passing some very large wait time.

In addition, the `k_sem_*` APIs were type-confused, where
the internal data structure was `uint32_t`, but the APIs took
and returned `unsigned int`. This changes the underlying data
structure to also use `unsigned int`, as changing the APIs
would be a (potentially) breaking change.

These changes are backwards-compatible, but it is strongly suggested
to take a quick scan for `k_sem_init` and `K_SEM_DEFINE` calls with
`UINT_MAX` (or `UINT32_MAX`) and replace them with `K_SEM_MAX_LIMIT`
where appropriate.

Signed-off-by: James Harris <james.harris@intel.com>
This commit is contained in:
James Harris 2021-03-03 12:02:05 -08:00 committed by Maureen Helm
commit b10428163a
86 changed files with 110 additions and 96 deletions

View file

@ -16,6 +16,7 @@
#if !defined(_ASMLANGUAGE)
#include <kernel_includes.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <toolchain.h>
@ -2699,8 +2700,8 @@ __syscall int k_condvar_wait(struct k_condvar *condvar, struct k_mutex *mutex,
struct k_sem {
_wait_q_t wait_q;
uint32_t count;
uint32_t limit;
unsigned int count;
unsigned int limit;
_POLL_EVENT;
@ -2727,6 +2728,16 @@ struct k_sem {
* @{
*/
/**
* @brief Maximum limit value allowed for a semaphore.
*
* This is intended for use when a semaphore does not have
* an explicit maximum limit, and instead is just used for
* counting purposes.
*
*/
#define K_SEM_MAX_LIMIT UINT_MAX
/**
* @brief Initialize a semaphore.
*
@ -2736,6 +2747,8 @@ struct k_sem {
* @param initial_count Initial semaphore count.
* @param limit Maximum permitted semaphore count.
*
* @see K_SEM_MAX_LIMIT
*
* @retval 0 Semaphore created successfully
* @retval -EINVAL Invalid values
*
@ -2827,7 +2840,8 @@ static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Z_SEM_INITIALIZER(name, initial_count, count_limit); \
BUILD_ASSERT(((count_limit) != 0) && \
((initial_count) <= (count_limit)));
((initial_count) <= (count_limit)) && \
((count_limit) <= K_SEM_MAX_LIMIT));
/** @} */