Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 *); Tracked-On: #4329 Signed-off-by: Shuo A Liu <shuo.a.liu@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2018 Intel Corporation. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
#include <cpu.h> | ||
#include <schedule.h> | ||
#include <event.h> | ||
#include <logmsg.h> | ||
|
||
void init_event(struct sched_event *event) | ||
{ | ||
spinlock_init(&event->lock); | ||
event->set = false; | ||
event->waiting_thread = NULL; | ||
} | ||
|
||
void reset_event(struct sched_event *event) | ||
{ | ||
uint64_t rflag; | ||
|
||
spinlock_irqsave_obtain(&event->lock, &rflag); | ||
event->set = false; | ||
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"); | ||
while (!event->set) { | ||
event->waiting_thread = sched_get_current(get_pcpu_id()); | ||
sleep_thread(event->waiting_thread); | ||
spinlock_irqrestore_release(&event->lock, rflag); | ||
schedule(); | ||
spinlock_irqsave_obtain(&event->lock, &rflag); | ||
} | ||
event->set = false; | ||
event->waiting_thread = NULL; | ||
spinlock_irqrestore_release(&event->lock, rflag); | ||
} | ||
|
||
void signal_event(struct sched_event *event) | ||
{ | ||
uint64_t rflag; | ||
|
||
spinlock_irqsave_obtain(&event->lock, &rflag); | ||
event->set = true; | ||
if (event->waiting_thread != NULL) { | ||
wake_thread(event->waiting_thread); | ||
} | ||
spinlock_irqrestore_release(&event->lock, rflag); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef EVENT_H | ||
#define EVENT_H | ||
#include <spinlock.h> | ||
|
||
struct sched_event { | ||
spinlock_t lock; | ||
bool set; | ||
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); | ||
|
||
#endif /* EVENT_H */ |