Skip to contentSkip to conversation

kthread abstraction, take two

PATCH In review Christoph Hellwig 3 participants 3 messages Feb 9, 2002 – Feb 10, 2002

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kthread abstraction, take two

This is a new version of the ktread abstraction which incorporates suggestions by Andi Kleen, Jeff Garzik and Andrew Morton.

The changes are:

- kthread_start now takes a void * for the user-data, so it doesn't have to be part of struct kthread.
- the main method of struct kthread now returns an integers, if it is negative, the thread will be stopped.
- kthread_main no more does the scheduling, it has to be done by the mainloop now.

The API is now:

int kthread_start(struct kthread *kth, void *data)

Startup a new kernel thread as described by 'kth' (details below). Wait until it has finished initialization.

void kthread_stop(struct kthread *kth)

Stop the kernel thread described by 'kth'. Wait until is has finished.

int kthread_running(struct kthread *kth)

Return 1 if the kernel thread described by 'kth' is running.

The 'kthread' structure contains all information for this thread. Two fields _must_ be initialized:

const char *name;

Name of the thread.

int (*main)(struct kthread *, void);

Mainloop of the thread. This loop is repeated until the thread is stopped. Stopping is done by either kthread_stop or a negative return value of this method. This routine has to release the timeslice after finishing! (i.e. call schedule() or yield()).

Others may be filled out if needed:

int (*init)(struct kthread *, void *);

Initialize thread before the mainloop is called.

void (*cleanup)(struct kthread *, void *);

Cleanup after the mainloop is done.

void *data;

Opaque data for the thread's use

Patch for 2.5.4-pre5 is below.

Christoph

--

include/linux/kthread.h renamed from master/linux-2.5.4-pre5/include/linux/kthread.h +22 -0
diff -uNr -Xdontdiff ../master/linux-2.5.4-pre5/include/linux/kthread.h linux/include/linux/kthread.h
@@ -0,0 +1,22 @@
+#ifndef _LINUX_KTHREAD_H
+#define _LINUX_KTHREAD_H
+
+struct task_struct;
+
+struct kthread {
+	const char *name;
+	struct task_struct *task;
+	struct completion done;
+#define KTH_RUNNING	1
+#define KTH_SHUTDOWN	2
+	long state;
+	int (*init)(struct kthread *, void *);
+	void (*cleanup)(struct kthread *, void *);
+	int (*main)(struct kthread *, void *);
+};
+
+extern int kthread_start(struct kthread *, void *);
+extern void kthread_stop(struct kthread *);
+extern int kthread_running(struct kthread *);
+
+#endif /* _LINUX_KTHREAD_H */
kernel/Makefile renamed from master/linux-2.5.4-pre5/kernel/Makefile +2 -2
diff -uNr -Xdontdiff ../master/linux-2.5.4-pre5/kernel/Makefile linux/kernel/Makefile
@@ -10,12 +10,12 @@
 O_TARGET := kernel.o
 
 export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o \
-		printk.o 
+		printk.o kthread.o
 
 obj-y     = sched.o dma.o fork.o exec_domain.o panic.o printk.o \
 	    module.o exit.o itimer.o info.o time.o softirq.o resource.o \
 	    sysctl.o acct.o capability.o ptrace.o timer.o user.o \
-	    signal.o sys.o kmod.o context.o 
+	    signal.o sys.o kmod.o context.o kthread.o
 
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += ksyms.o
kernel/kthread.c renamed from master/linux-2.5.4-pre5/kernel/kthread.c +148 -0

148 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Christoph Hellwig

This is a new version of the ktread abstraction which incorporates suggestions by Andi Kleen, Jeff Garzik and Andrew Morton.

Quoted 8 lines

The changes are:

- kthread_start now takes a void * for the user-data, so it doesn't have to be part of struct kthread.
- the main method of struct kthread now returns an integers, if it is negative, the thread will be stopped.
- kthread_main no more does the scheduling, it has to be done by the mainloop now.

Nice. I like it.

Would you consider converting a few places in the kernel to use kthread, just as an example to show people what a sample implementation would look like?

Jeff

Signature · 3 lines
Jeff Garzik      | "I went through my candy like hot oatmeal
Building 1024    |  through an internally-buttered weasel."
MandrakeSoft     |             - goats.com
#2 lore raw
In reply to Christoph Hellwig

Hi!

Quoted 2 lines

This is a new version of the ktread abstraction which incorporates suggestions by Andi Kleen, Jeff Garzik and Andrew Morton.

Could you convert some kernel thread to the new interface to show how it simplifies things?
Pavel

Signature · 2 lines
(about SSSCA) "I don't say this lightly.  However, I really think that the U.S.
no longer is classifiable as a democracy, but rather as a plutocracy." --hpa