summaryrefslogtreecommitdiff
path: root/include/bthread.h
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-03-10 09:47:58 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-03-22 10:46:47 +0100
commit64242776de86d46d914aa8f3ab6cfee68420ca6a (patch)
tree33b339b551b65557d3f15c2af6e4beaae17da631 /include/bthread.h
parent7ce21703892fac7084d417d296fc2723a85e7c10 (diff)
downloadbarebox-64242776de86d46d914aa8f3ab6cfee68420ca6a.tar.gz
common: introduce bthreads, co-operative barebox threads
With the new setjmp/longjmp/initjmp support, we have all the architecture support in place to have suspendable green threads in barebox. These are expected to replace pollers and workqueues. For now we still have a differentiation between the main and secondary threads. The main thread is allowed I/O access unconditionally. If it's in a delay loop, a secondary thread running needs to be wary of not entering the same driver and doing hardware manipulation. We already have slices as mechanism to guard against this, but they aren't used as widely as needed. Preferably, in the end, threads will automatically yield until they can claim a resource (i.e. lock a mutex). Until we are there, take the same care when using bthreads as with pollers. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include/bthread.h')
-rw-r--r--include/bthread.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/include/bthread.h b/include/bthread.h
new file mode 100644
index 0000000000..e3871fb115
--- /dev/null
+++ b/include/bthread.h
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
+ */
+
+#ifndef __BTHREAD_H_
+#define __BTHREAD_H_
+
+#include <linux/stddef.h>
+
+struct bthread;
+
+extern struct bthread *current;
+
+struct bthread *bthread_create(int (*threadfn)(void *), void *data, const char *namefmt, ...);
+void bthread_free(struct bthread *bthread);
+
+void bthread_schedule(struct bthread *);
+void bthread_wake(struct bthread *bthread);
+void bthread_suspend(struct bthread *bthread);
+int bthread_should_stop(void);
+int bthread_stop(struct bthread *bthread);
+void bthread_info(void);
+const char *bthread_name(struct bthread *bthread);
+bool bthread_is_main(struct bthread *bthread);
+
+/**
+ * bthread_run - create and wake a thread.
+ * @threadfn: the function to run for coming reschedule cycles
+ * @data: data ptr for @threadfn.
+ * @namefmt: printf-style name for the thread.
+ *
+ * Description: Convenient wrapper for bthread_create() followed by
+ * bthread_wakeup(). Returns the bthread or NULL
+ */
+#define bthread_run(threadfn, data, namefmt, ...) \
+({ \
+ struct bthread *__b \
+ = bthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
+ if (__b) \
+ bthread_wake(__b); \
+ __b; \
+})
+
+#ifdef CONFIG_BTHREAD
+void bthread_reschedule(void);
+#else
+static inline void bthread_reschedule(void)
+{
+}
+#endif
+
+#endif