sys_sem: add build time definition macros

We need a SYS_SEM_DEFINE() that works just like
K_SEM_DEFINE().

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-08-06 14:57:11 -07:00 committed by Kumar Gala
commit db48d3e22a
2 changed files with 36 additions and 3 deletions

View file

@ -39,6 +39,41 @@ struct sys_sem {
#endif #endif
}; };
/**
* @brief Statically define and initialize a sys_sem
*
* The semaphore can be accessed outside the module where it is defined using:
*
* @code extern struct sys_sem <name>; @endcode
*
* Route this to memory domains using K_APP_DMEM().
*
* @param _name Name of the semaphore.
* @param _initial_count Initial semaphore count.
* @param _count_limit Maximum permitted semaphore count.
*/
#ifdef CONFIG_USERSPACE
#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
struct sys_sem _name = { \
.futex = { _initial_count }, \
.limit = _count_limit \
}; \
BUILD_ASSERT(((_count_limit) != 0) && \
((_initial_count) <= (_count_limit)))
#else
/* Stuff this in the section with the rest of the k_sem objects, since they
* are identical and can be treated as a k_sem in the boot initialization code
*/
#define SYS_SEM_DEFINE(_name, _initial_count, _count_limit) \
Z_DECL_ALIGN(struct sys_sem) _name \
__in_section(_k_sem, static, _name) = { \
.kernel_sem = Z_SEM_INITIALIZER(_name.kernel_sem, \
_initial_count, _count_limit) \
}; \
BUILD_ASSERT(((_count_limit) != 0) && \
((_initial_count) <= (_count_limit)))
#endif
/** /**
* @brief Initialize a semaphore. * @brief Initialize a semaphore.
* *

View file

@ -21,7 +21,7 @@ ZTEST_BMEM struct sys_sem simple_sem;
ZTEST_BMEM struct sys_sem low_prio_sem; ZTEST_BMEM struct sys_sem low_prio_sem;
ZTEST_BMEM struct sys_sem mid_prio_sem; ZTEST_BMEM struct sys_sem mid_prio_sem;
ZTEST_DMEM struct sys_sem high_prio_sem; ZTEST_DMEM struct sys_sem high_prio_sem;
ZTEST_DMEM struct sys_sem multiple_thread_sem; ZTEST_DMEM SYS_SEM_DEFINE(multiple_thread_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_THREAD_STACK_DEFINE(stack_1, STACK_SIZE); K_THREAD_STACK_DEFINE(stack_1, STACK_SIZE);
K_THREAD_STACK_DEFINE(stack_2, STACK_SIZE); K_THREAD_STACK_DEFINE(stack_2, STACK_SIZE);
@ -344,7 +344,6 @@ void test_sem_take_multiple(void)
sys_sem_init(&high_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL); sys_sem_init(&high_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL);
sys_sem_init(&mid_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL); sys_sem_init(&mid_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL);
sys_sem_init(&low_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL); sys_sem_init(&low_prio_sem, SEM_INIT_VAL, SEM_MAX_VAL);
sys_sem_init(&multiple_thread_sem, SEM_INIT_VAL, SEM_MAX_VAL);
k_thread_create(&sem_tid, stack_1, STACK_SIZE, k_thread_create(&sem_tid, stack_1, STACK_SIZE,
sem_take_multiple_low_prio_helper, sem_take_multiple_low_prio_helper,
@ -512,7 +511,6 @@ void test_sem_multiple_threads_wait(void)
#endif #endif
sys_sem_init(&simple_sem, SEM_INIT_VAL, SEM_MAX_VAL); sys_sem_init(&simple_sem, SEM_INIT_VAL, SEM_MAX_VAL);
sys_sem_init(&multiple_thread_sem, SEM_INIT_VAL, SEM_MAX_VAL);
do { do {
for (int i = 0; i < TOTAL_THREADS_WAITING; i++) { for (int i = 0; i < TOTAL_THREADS_WAITING; i++) {