📄 kthread.c
字号:
/* * Copyright (c) 2002 Christoph Hellwig. * All rights resered. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/completion.h>#include <linux/kernel.h>#include "kthread.h"#include <linux/module.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/signal.h>#include <linux/smp_lock.h>#include <linux/spinlock.h>#include <asm/bitops.h>#define KTHREAD_FLAGS \ (CLONE_FS|CLONE_FILES|CLONE_SIGHAND)struct kthread_args { struct kthread *kth; void *data;};static int kthread_stopped(struct kthread *kth){ struct task_struct *task = kth->task; unsigned long signr; siginfo_t info; spin_lock_irq(&task->sigmask_lock); signr = dequeue_signal(&task->blocked, &info); spin_unlock_irq(&task->sigmask_lock); if (signr == SIGKILL && test_bit(KTH_SHUTDOWN, &kth->state)) return 1; return 0;}static int kthread_main(void *p){ struct kthread_args *args = p; struct kthread *kth = args->kth; void *data = args->data; lock_kernel(); daemonize(); reparent_to_init(); strcpy(current->comm, kth->name); unlock_kernel(); kth->task = current; spin_lock_irq(¤t->sigmask_lock); siginitsetinv(¤t->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT)); spin_unlock_irq(¤t->sigmask_lock); if (kth->init) kth->init(kth, data); complete(&kth->done); do { if (kth->main(kth, data) < 0) break; } while (!kthread_stopped(kth)); if (kth->cleanup) kth->cleanup(kth, data); clear_bit(KTH_RUNNING, &kth->state); complete(&kth->done); return 0;}/** * kthread_start - start a new kernel thread * @kth: kernel thread description * @data: opaque data for use with the methods * * For off a new kernel thread as described by @kth. */int kthread_start(struct kthread *kth, void *data){ struct kthread_args args; pid_t pid; if (!kth->name || !kth->main) return -EINVAL; args.kth = kth; args.data = data; kth->state = 0; init_completion(&kth->done); if ((pid = kernel_thread(kthread_main, &args, KTHREAD_FLAGS)) < 0) return pid; set_bit(KTH_RUNNING, &kth->state); wait_for_completion(&kth->done); return 0;}/** * kthread_stop - stop a kernel thread * @kth: kernel thread description * * Stop the kernel thread described by @kth. */void kthread_stop(struct kthread *kth){ if (kth->task) { init_completion(&kth->done); set_bit(KTH_SHUTDOWN, &kth->state); send_sig(SIGKILL, kth->task, 1); wait_for_completion(&kth->done); kth->task = NULL; clear_bit(KTH_SHUTDOWN, &kth->state); }}/** * kthread_running - check whether a kernel thread is running * @kth: kernel thread description * * Checks whether the kernel thread described by @kth is running. */int kthread_running(struct kthread *kth){ return test_bit(KTH_RUNNING, &kth->state);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -