summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-06-22 10:26:14 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-06-25 09:31:03 +0200
commit7ead63d835f70a56ca6b51ee8c15ac48d0ed068b (patch)
treefd8213c2dd8a894ba810f8c6a51ae4f590623bcf
parenta921a60f4d6f4df5637c835f1d5492f6c77e0a56 (diff)
downloadbarebox-7ead63d835f70a56ca6b51ee8c15ac48d0ed068b.tar.gz
barebox-7ead63d835f70a56ca6b51ee8c15ac48d0ed068b.tar.xz
bthread: implement basic Linux-like completion API
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 bthread support, any bthread can wait for another to complete(). Wrap this up using the Linux completion API to make porting threaded code easier. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20210622082617.18011-6-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--include/linux/completion.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/include/linux/completion.h b/include/linux/completion.h
new file mode 100644
index 0000000000..e897e4f65b
--- /dev/null
+++ b/include/linux/completion.h
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * (C) Copyright 2021 Ahmad Fatoum
+ *
+ * Async wait-for-completion handler data structures.
+ * This allows one bthread to wait for another
+ */
+
+#ifndef __LINUX_COMPLETION_H
+#define __LINUX_COMPLETION_H
+
+#include <stdio.h>
+#include <errno.h>
+#include <bthread.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)
+{
+ while (!x->done) {
+ switch (bthread_should_stop()) {
+ case -EINTR:
+ if (!ctrlc())
+ continue;
+ case 1:
+ return -ERESTARTSYS;
+ }
+ }
+
+ return 0;
+}
+
+static inline bool completion_done(struct completion *x)
+{
+ return x->done;
+}
+
+static inline void complete(struct completion *x)
+{
+ x->done = 1;
+}
+
+#endif