2016-10-26 01:28:35 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-10-26 01:28:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief kernel alerts.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
2016-11-08 23:36:50 +08:00
|
|
|
#include <kernel_structs.h>
|
2016-12-18 02:18:45 +08:00
|
|
|
#include <debug/object_tracing_common.h>
|
2016-10-26 01:28:35 +08:00
|
|
|
#include <atomic.h>
|
2016-10-20 05:10:46 +08:00
|
|
|
#include <init.h>
|
2016-10-26 01:28:35 +08:00
|
|
|
#include <toolchain.h>
|
2017-06-17 23:30:47 +08:00
|
|
|
#include <linker/sections.h>
|
2016-10-26 01:28:35 +08:00
|
|
|
|
2016-10-20 05:10:46 +08:00
|
|
|
extern struct k_alert _k_alert_list_start[];
|
|
|
|
extern struct k_alert _k_alert_list_end[];
|
|
|
|
|
|
|
|
struct k_alert *_trace_list_k_alert;
|
|
|
|
|
2016-12-18 19:57:45 +08:00
|
|
|
#ifdef CONFIG_OBJECT_TRACING
|
2016-10-20 05:10:46 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Complete initialization of statically defined alerts.
|
|
|
|
*/
|
|
|
|
static int init_alert_module(struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
struct k_alert *alert;
|
|
|
|
|
|
|
|
for (alert = _k_alert_list_start; alert < _k_alert_list_end; alert++) {
|
|
|
|
SYS_TRACING_OBJ_INIT(k_alert, alert);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-09 03:06:55 +08:00
|
|
|
SYS_INIT(init_alert_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
|
2016-10-20 05:10:46 +08:00
|
|
|
|
2016-12-18 19:57:45 +08:00
|
|
|
#endif /* CONFIG_OBJECT_TRACING */
|
2016-10-20 05:10:46 +08:00
|
|
|
|
2016-10-26 01:28:35 +08:00
|
|
|
void _alert_deliver(struct k_work *work)
|
|
|
|
{
|
|
|
|
struct k_alert *alert = CONTAINER_OF(work, struct k_alert, work_item);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if ((alert->handler)(alert) == 0) {
|
|
|
|
/* do nothing -- handler has processed the alert */
|
|
|
|
} else {
|
|
|
|
/* pend the alert */
|
|
|
|
k_sem_give(&alert->sem);
|
|
|
|
}
|
|
|
|
if (atomic_dec(&alert->send_count) == 1) {
|
|
|
|
/* have finished delivering alerts */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 02:42:30 +08:00
|
|
|
void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
|
|
|
|
unsigned int max_num_pending_alerts)
|
2016-10-26 01:28:35 +08:00
|
|
|
{
|
|
|
|
alert->handler = handler;
|
|
|
|
alert->send_count = ATOMIC_INIT(0);
|
2017-06-28 01:51:23 +08:00
|
|
|
alert->work_item = (struct k_work)_K_WORK_INITIALIZER(_alert_deliver);
|
2016-10-26 02:42:30 +08:00
|
|
|
k_sem_init(&alert->sem, 0, max_num_pending_alerts);
|
2016-10-20 05:10:46 +08:00
|
|
|
SYS_TRACING_OBJ_INIT(k_alert, alert);
|
2016-10-26 01:28:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void k_alert_send(struct k_alert *alert)
|
|
|
|
{
|
|
|
|
if (alert->handler == K_ALERT_IGNORE) {
|
|
|
|
/* ignore the alert */
|
|
|
|
} else if (alert->handler == K_ALERT_DEFAULT) {
|
|
|
|
/* pend the alert */
|
|
|
|
k_sem_give(&alert->sem);
|
|
|
|
} else {
|
|
|
|
/* deliver the alert */
|
|
|
|
if (atomic_inc(&alert->send_count) == 0) {
|
|
|
|
/* add alert's work item to system work queue */
|
|
|
|
k_work_submit_to_queue(&k_sys_work_q,
|
|
|
|
&alert->work_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 23:55:34 +08:00
|
|
|
int k_alert_recv(struct k_alert *alert, s32_t timeout)
|
2016-10-26 01:28:35 +08:00
|
|
|
{
|
|
|
|
return k_sem_take(&alert->sem, timeout);
|
|
|
|
}
|