From bc18f1d65f5d4c328f3e80fb04e71e62aae4d261 Mon Sep 17 00:00:00 2001 From: Liu Xinwu Date: Wed, 9 May 2018 17:26:54 +0800 Subject: [PATCH] tools: acrn-crashlog: event queue operations for acrnprobe There is a global queue to receive all events detected. Genarally, events are enqueued in channel, and dequeued in event handler. Signed-off-by: Liu Xinwu Reviewed-by: Zhang Yanmin Reviewed-by: Liu Chuansheng Reviewed-by: Zhao Yakui Reviewed-by: Geoffroy Van Cutsem Acked-by: Eddie Dong --- tools/acrn-crashlog/acrnprobe/event_queue.c | 67 ++++++++++++++++++- .../acrnprobe/include/event_queue.h | 3 + 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/tools/acrn-crashlog/acrnprobe/event_queue.c b/tools/acrn-crashlog/acrnprobe/event_queue.c index 51c097e2f..3288bdc8c 100644 --- a/tools/acrn-crashlog/acrnprobe/event_queue.c +++ b/tools/acrn-crashlog/acrnprobe/event_queue.c @@ -3,8 +3,73 @@ * SPDX-License-Identifier: BSD-3-Clause */ +#include +#include +#include #include "event_queue.h" +#include "log_sys.h" -void init_event(void) +static pthread_mutex_t eq_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t pcond = PTHREAD_COND_INITIALIZER; +TAILQ_HEAD(, event_t) event_q; + +/** + * Enqueue an event to event_queue. + * + * @param event Event to process. + */ +void event_enqueue(struct event_t *event) { + pthread_mutex_lock(&eq_mtx); + TAILQ_INSERT_TAIL(&event_q, event, entries); + pthread_cond_signal(&pcond); + LOGD("enqueue %d, (%d)%s\n", event->event_type, event->len, + event->path); + pthread_mutex_unlock(&eq_mtx); +} + +/** + * Count the number of events in event_queue. + * + * @return count. + */ +int events_count(void) +{ + struct event_t *e; + int count = 0; + + pthread_mutex_lock(&eq_mtx); + TAILQ_FOREACH(e, &event_q, entries) + count++; + pthread_mutex_unlock(&eq_mtx); + + return count; +} + +/** + * Dequeue an event from event_queue. + * + * @return the dequeued event. + */ +struct event_t *event_dequeue(void) +{ + struct event_t *e; + + pthread_mutex_lock(&eq_mtx); + while (TAILQ_EMPTY(&event_q)) + pthread_cond_wait(&pcond, &eq_mtx); + e = TAILQ_FIRST(&event_q); + TAILQ_REMOVE(&event_q, e, entries); + LOGD("dequeue %d, (%d)%s\n", e->event_type, e->len, e->path); + pthread_mutex_unlock(&eq_mtx); + + return e; +} + +/** + * Initailize event_queue. + */ +void init_event_queue(void) +{ + TAILQ_INIT(&event_q); } diff --git a/tools/acrn-crashlog/acrnprobe/include/event_queue.h b/tools/acrn-crashlog/acrnprobe/include/event_queue.h index 48dce4347..512668855 100644 --- a/tools/acrn-crashlog/acrnprobe/include/event_queue.h +++ b/tools/acrn-crashlog/acrnprobe/include/event_queue.h @@ -33,6 +33,9 @@ struct event_t { char path[0]; /* keep this at tail*/ }; +void event_enqueue(struct event_t *event); +int events_count(void); +struct event_t *event_dequeue(void); void init_event_queue(void); #endif