#include #include #include #include #include #include #include "../include/dts_thread.h" #define THREAD_WAKEUP 0x01 extern void dts_set_bit(char * , int ); int dts_thread(void * arg) { dts_thread_t *thread = arg; lock_kernel(); /* * Detach thread */ daemonize(thread->name); current->exit_signal = SIGCHLD; allow_signal(SIGKILL); thread->tsk = current; unlock_kernel(); complete(thread->event); while (thread->run) { void (*run)(void *); wait_event_interruptible(thread->wqueue, test_bit(THREAD_WAKEUP, &thread->flags)); if (current->flags & PF_FREEZE) refrigerator(PF_FREEZE); clear_bit(THREAD_WAKEUP, &thread->flags); run = thread->run; if (run) run(thread->private); if (signal_pending(current)) flush_signals(current); } complete(thread->event); return 0; } void dts_wakeup_thread(dts_thread_t *thread) { if (thread) { dts_set_bit((char *)&thread->flags, THREAD_WAKEUP); wake_up(&thread->wqueue); } else printk("dts_wakeup_thread:.........thread is NULL\n"); } dts_thread_t *dts_register_thread(void (*run) (void *), const char *name, void * private) { dts_thread_t *thread=NULL; int ret; struct completion event; thread = (dts_thread_t *) kmalloc (sizeof(dts_thread_t), GFP_KERNEL); if (!thread) return NULL; memset(thread, 0, sizeof(dts_thread_t)); init_waitqueue_head(&thread->wqueue); init_completion(&event); thread->event = &event; thread->run = run; thread->name = name; thread->private = private; ret = kernel_thread(dts_thread, thread, 0); if (ret < 0) { printk("\ndts_register_thread:.......unable to register kernel thread\n"); kfree(thread); return NULL; } wait_for_completion(&event); // printk("Thread Allocated Successfully\n "); return thread; } void dts_interrupt_thread(dts_thread_t *thread) { if (!thread->tsk) { BUG(); return; } // dprintk("interrupting dts-thread pid %d\n", thread->tsk->pid); send_sig(SIGKILL, thread->tsk, 1); } void dts_unregister_thread(dts_thread_t *thread) { struct completion event; init_completion(&event); thread->event = &event; thread->run = NULL; thread->name = NULL; dts_interrupt_thread(thread); wait_for_completion(&event); kfree(thread); } EXPORT_SYMBOL(dts_wakeup_thread); EXPORT_SYMBOL(dts_unregister_thread); EXPORT_SYMBOL(dts_register_thread); EXPORT_SYMBOL(dts_interrupt_thread);