Re: [RFC PATCH v3 2/6] hv: sched: simple event implemention


Eddie Dong
 

Acked-by: Eddie Dong <eddie.dong@...>


Thx Eddie

-----Original Message-----
From: Liu, Shuo A <shuo.a.liu@...>
Sent: Thursday, January 2, 2020 11:49 AM
To: acrn-dev@...
Cc: Dong, Eddie <eddie.dong@...>; Liu, Shuo A <shuo.a.liu@...>
Subject: [RFC PATCH v3 2/6] hv: sched: simple event implemention

This simple event implemention can only support exclusive waiting at same
time. It mainly used by thread who want to wait for special event happens.
Thread A who want to wait for some events calls
wait_event(struct sched_event *);

Thread B who can give the event signal calls
signal_event(struct sched_event *);

Signed-off-by: Shuo A Liu <shuo.a.liu@...>
---
hypervisor/common/schedule.c | 45
++++++++++++++++++++++++++++++++++++
hypervisor/include/common/schedule.h | 11 +++++++++
2 files changed, 56 insertions(+)

diff --git a/hypervisor/common/schedule.c b/hypervisor/common/schedule.c
index 3ce6a41..2f72bc9 100644
--- a/hypervisor/common/schedule.c
+++ b/hypervisor/common/schedule.c
@@ -274,3 +274,48 @@ void run_thread(struct thread_object *obj)
obj->thread_entry(obj);
}
}
+
+void init_event(struct sched_event *event) {
+ spinlock_init(&event->lock);
+ event->done = 0UL;
+ event->waiting_thread = NULL;
+}
+
+void reset_event(struct sched_event *event) {
+ uint64_t rflag;
+
+ spinlock_irqsave_obtain(&event->lock, &rflag);
+ event->done = 0UL;
+ event->waiting_thread = NULL;
+ spinlock_irqrestore_release(&event->lock, rflag); }
+
+/* support exclusive waiting only */
+void wait_event(struct sched_event *event) {
+ uint64_t rflag;
+
+ spinlock_irqsave_obtain(&event->lock, &rflag);
+ ASSERT((event->waiting_thread == NULL), "only support exclusive
waiting");
+ if (event->done == 0UL) {
+ event->waiting_thread = sched_get_current(get_pcpu_id());
+ sleep_thread(event->waiting_thread);
+ }
+ spinlock_irqrestore_release(&event->lock, rflag); }
+
+void signal_event(struct sched_event *event) {
+ uint64_t rflag;
+
+ spinlock_irqsave_obtain(&event->lock, &rflag);
+ event->done++;
+ if (event->waiting_thread != NULL) {
+ wake_thread(event->waiting_thread);
+ event->done = 0UL;
+ event->waiting_thread = NULL;
+ }
+ spinlock_irqrestore_release(&event->lock, rflag); }
diff --git a/hypervisor/include/common/schedule.h
b/hypervisor/include/common/schedule.h
index 1526865..630f130 100644
--- a/hypervisor/include/common/schedule.h
+++ b/hypervisor/include/common/schedule.h
@@ -90,6 +90,17 @@ struct sched_iorr_control {
struct hv_timer tick_timer;
};

+struct sched_event {
+ spinlock_t lock;
+ uint32_t done;
+ struct thread_object* waiting_thread;
+};
+
+void init_event(struct sched_event *event); void reset_event(struct
+sched_event *event); void wait_event(struct sched_event *event); void
+signal_event(struct sched_event *event);
+
bool is_idle_thread(const struct thread_object *obj); uint16_t
sched_get_pcpuid(const struct thread_object *obj); struct thread_object
*sched_get_current(uint16_t pcpu_id);
--
2.8.3

Join acrn-dev@lists.projectacrn.org to automatically receive all group messages.