[PATCH 08/12] poller: implement basic Linux-like completion API

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Feb 15 05:37:01 EST 2021


So far, completions made sense only in one direction: The main thread
can wait for pollers, but the other way around didn't work. With the new
yielding poller support, pollers can wait for completions as well. Wrap
this up using the Linux poller API to make porting threaded code easier.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/linux/completion.h | 61 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 include/linux/completion.h

diff --git a/include/linux/completion.h b/include/linux/completion.h
new file mode 100644
index 000000000000..f2db80716c7f
--- /dev/null
+++ b/include/linux/completion.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_COMPLETION_H
+#define __LINUX_COMPLETION_H
+
+/*
+ * (C) Copyright 2021 Ahmad Fatoum
+ *
+ * Async wait-for-completion handler data structures.
+ * This allows pollers to wait for main thread and vice versa
+ * Requires poller_yield() support
+ */
+
+#include <poller.h>
+#include <linux/bug.h>
+
+struct completion {
+	unsigned int done;
+};
+
+static inline void init_completion(struct completion *x)
+{
+	x->done = 0;
+}
+
+static inline void reinit_completion(struct completion *x)
+{
+	x->done = 0;
+}
+
+static inline int wait_for_completion_interruptible(struct completion *x)
+{
+	int ret;
+
+	while (!x->done) {
+		ret = poller_reschedule();
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static inline bool completion_done(struct completion *x)
+{
+	return x->done;
+}
+
+static inline void complete(struct completion *x)
+{
+	x->done = 1;
+}
+
+static inline void __noreturn complete_and_exit(struct completion *x, int ret)
+{
+	BUG_ON(!in_poller());
+	complete(x);
+	for (;;)
+		poller_yield();
+}
+
+#endif
-- 
2.29.2




More information about the barebox mailing list