kernel: stack: error handling

Add runtime error checking for both k_stack_push and k_stack_cleanup.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2019-06-16 08:58:10 -04:00
commit 1ed67d1d51
2 changed files with 20 additions and 13 deletions

View file

@ -2708,9 +2708,11 @@ __syscall s32_t k_stack_alloc_init(struct k_stack *stack,
* if the buffer wasn't dynamically allocated.
*
* @param stack Address of the stack.
* @retval 0 on success
* @retval -EAGAIN when object is still in use
* @req K-STACK-001
*/
void k_stack_cleanup(struct k_stack *stack);
int k_stack_cleanup(struct k_stack *stack);
/**
* @brief Push an element onto a stack.
@ -2722,10 +2724,11 @@ void k_stack_cleanup(struct k_stack *stack);
* @param stack Address of the stack.
* @param data Value to push onto the stack.
*
* @return N/A
* @retval 0 on success
* @retval -ENOMEM if stack is full
* @req K-STACK-001
*/
__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
/**
* @brief Pop an element from a stack.

View file

@ -15,7 +15,7 @@
#include <linker/sections.h>
#include <ksched.h>
#include <wait_q.h>
#include <sys/__assert.h>
#include <sys/check.h>
#include <init.h>
#include <syscall_handler.h>
#include <kernel_internal.h>
@ -81,23 +81,28 @@ static inline s32_t z_vrfy_k_stack_alloc_init(struct k_stack *stack,
#include <syscalls/k_stack_alloc_init_mrsh.c>
#endif
void k_stack_cleanup(struct k_stack *stack)
int k_stack_cleanup(struct k_stack *stack)
{
__ASSERT_NO_MSG(z_waitq_head(&stack->wait_q) == NULL);
CHECKIF(z_waitq_head(&stack->wait_q) != NULL) {
return -EAGAIN;
}
if ((stack->flags & K_STACK_FLAG_ALLOC) != (u8_t)0) {
k_free(stack->base);
stack->base = NULL;
stack->flags &= ~K_STACK_FLAG_ALLOC;
}
return 0;
}
void z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
int z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
{
struct k_thread *first_pending_thread;
k_spinlock_key_t key;
__ASSERT(stack->next != stack->top, "stack is full");
CHECKIF(stack->next == stack->top) {
return -ENOMEM;
}
key = k_spin_lock(&stack->lock);
@ -109,22 +114,21 @@ void z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
z_thread_return_value_set_with_data(first_pending_thread,
0, (void *)data);
z_reschedule(&stack->lock, key);
return;
} else {
*(stack->next) = data;
stack->next++;
k_spin_unlock(&stack->lock, key);
}
return 0;
}
#ifdef CONFIG_USERSPACE
static inline void z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
static inline int z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
{
Z_OOPS(Z_SYSCALL_OBJ(stack, K_OBJ_STACK));
Z_OOPS(Z_SYSCALL_VERIFY_MSG(stack->next != stack->top,
"stack is full"));
z_impl_k_stack_push(stack, data);
return z_impl_k_stack_push(stack, data);
}
#include <syscalls/k_stack_push_mrsh.c>
#endif