- 论坛徽章:
- 0
|
楼上这位大哥说的很对。critical_enter是指临界区处理吧??
现在我已经改好,可以正确退出了:
#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kthread.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
struct my_kthread_s {
unsigned int flags;
#define MKH_THREAD_EXIT 0x01
#define MKH_THREAD_ALIVE 0x02
struct mtx lock;
struct proc *my_proc;
} mkh;
static void mkh_run(void *args);
static void mkh_init(void *args);
static void mkh_exit(void);
static void mkh_init(void *args)
{
(void)args;
mtx_init(&mkh.lock, "my_mkh_lock", NULL, MTX_DEF);
mkh.flags = 0;
mkh.flags |= MKH_THREAD_ALIVE;
}
static void mkh_exit(void)
{
mkh.flags |= MKH_THREAD_EXIT;
mtx_lock(&mkh.lock);
while (mkh.flags & MKH_THREAD_ALIVE) {
msleep(&mkh.my_proc, &mkh.lock, PRIBIO, "mkh_exit", 0);
}
mtx_unlock(&mkh.lock);
}
static void mkh_run(void *args)
{
int i;
(void)args;
for (i = 0; ; ++i) {
printf("%c", (i % 2) ? 'A' : '~');
mtx_lock(&mkh.lock);
if (mkh.flags & MKH_THREAD_EXIT)
break;
mtx_unlock(&mkh.lock);
}
mkh.flags &= ~(MKH_THREAD_EXIT | MKH_THREAD_ALIVE);
wakeup(&mkh.my_proc);
mtx_unlock(&mkh.lock);
kthread_exit(0);
}
static int
my_test_thread_loader(struct module *m, int what, void *arg)
{
int err = 0;
switch (what) {
case MOD_LOAD:
mkh_init(NULL);
kthread_create(mkh_run, NULL, &mkh.my_proc, 0, 0, "__my_test_ktread");
break;
case MOD_UNLOAD:
mkh_exit();
break;
default:
err = EINVAL;
break;
}
return err;
}
static moduledata_t my_test_thread_mod = {
"__my_test_kernel_thread",
my_test_thread_loader,
NULL
};
DECLARE_MODULE(my_test_thread, my_test_thread_mod, SI_SUB_KLD, SI_ORDER_ANY); |
|