[RFC PATCH 07/20] kthread: Add kthread_kill_stop()
From: Mathieu Desnoyers
Date: Tue Aug 17 2010 - 19:23:51 EST
Allow use of "interruptible" functions in kernel threads by creating this
kthread_stop_kill() variant. Instead of just waking up the thread, it also sends
a signal after setting the "must exit" variable to 1.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
include/linux/kthread.h | 1 +
kernel/kthread.c | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
Index: linux.trees.git/include/linux/kthread.h
===================================================================
--- linux.trees.git.orig/include/linux/kthread.h 2010-08-17 16:28:25.000000000 -0400
+++ linux.trees.git/include/linux/kthread.h 2010-08-17 16:32:36.000000000 -0400
@@ -29,6 +29,7 @@ struct task_struct *kthread_create(int (
void kthread_bind(struct task_struct *k, unsigned int cpu);
int kthread_stop(struct task_struct *k);
+int kthread_kill_stop(struct task_struct *k, int signo);
int kthread_should_stop(void);
void *kthread_data(struct task_struct *k);
Index: linux.trees.git/kernel/kthread.c
===================================================================
--- linux.trees.git.orig/kernel/kthread.c 2010-08-17 16:28:26.000000000 -0400
+++ linux.trees.git/kernel/kthread.c 2010-08-17 16:32:36.000000000 -0400
@@ -228,6 +228,46 @@ int kthread_stop(struct task_struct *k)
}
EXPORT_SYMBOL(kthread_stop);
+/**
+ * kthread_kill_stop - kill and stop a thread created by kthread_create().
+ * @k: thread created by kthread_create().
+ * @signo: signal number to send.
+ *
+ * Sets kthread_should_stop() for @k to return true, sends a signal, and
+ * waits for it to exit. This can also be called after kthread_create()
+ * instead of calling wake_up_process(): the thread will exit without
+ * calling threadfn().
+ *
+ * If threadfn() may call do_exit() itself, the caller must ensure
+ * task_struct can't go away.
+ *
+ * Returns the result of threadfn(), or %-EINTR if wake_up_process()
+ * was never called.
+ */
+int kthread_kill_stop(struct task_struct *k, int signo)
+{
+ struct kthread *kthread;
+ int ret;
+
+ trace_sched_kthread_stop(k);
+ get_task_struct(k);
+
+ kthread = to_kthread(k);
+ barrier(); /* it might have exited */
+ if (k->vfork_done != NULL) {
+ kthread->should_stop = 1;
+ force_sig(signo, k);
+ wait_for_completion(&kthread->exited);
+ }
+ ret = k->exit_code;
+
+ put_task_struct(k);
+ trace_sched_kthread_stop_ret(ret);
+
+ return ret;
+}
+EXPORT_SYMBOL(kthread_kill_stop);
+
int kthreadd(void *unused)
{
struct task_struct *tsk = current;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/