~ubuntu-branches/ubuntu/maverick/ndiswrapper/maverick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *  Copyright (C) 2006 Giridhar Pemmasani
 *
 *  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.
 *
 */

#include "ntoskernel.h"

/* workqueue implementation for 2.4 kernels */

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
#define SIG_LOCK(t) (&(t)->sigmask_lock)
#else
#define SIG_LOCK(t) (&(t)->sighand->siglock)
#endif

static int workq_thread(void *data)
{
	workqueue_struct_t *workq = data;
	work_struct_t *work;
	unsigned long flags;

	lock_kernel();
	strncpy(current->comm, workq->name, sizeof(current->comm));
	current->comm[sizeof(current->comm) - 1] = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
	daemonize();
#else
	daemonize(workq->name);
#endif
	unlock_kernel();
#ifdef PF_NOFREEZE
	current->flags |= PF_NOFREEZE;
	set_user_nice(current, -5);
#endif
	while (1) {
		if (wait_event_interruptible(workq->waitq_head,
					     workq->pending)) {
			/* TODO: deal with signal */
			spin_lock_irq(SIG_LOCK(current));
			flush_signals(current);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
			recalc_sigpending(current);
#else
			recalc_sigpending();
#endif
			spin_unlock_irq(SIG_LOCK(current));
			continue;
		}
		spin_lock_irqsave(&workq->lock, flags);
		if (workq->pending-- < 0)
			break;
		if (list_empty(&workq->work_list))
			work = NULL;
		else {
			struct list_head *entry = workq->work_list.next;
			work = list_entry(entry, work_struct_t, list);
			list_del(entry);
			if (work->workq) {
				assert(work->workq == workq);
				work->workq = NULL;
			} else
				work = NULL;
		}
		spin_unlock_irqrestore(&workq->lock, flags);
		if (work)
			work->func(work->data);
	}
	/* set workq for each work to NULL so if cancel_delayed_work
	 * is called later, it won't access workq */
	list_for_each_entry(work, &workq->work_list, list) {
		work->workq = NULL;
	}
	spin_unlock_irqrestore(&workq->lock, flags);
	WORKTRACE("%s exiting", workq->name);
	workq->pid = 0;
	return 0;
}

wfastcall void wrap_queue_work(workqueue_struct_t *workq, work_struct_t *work)
{
	unsigned long flags;

	spin_lock_irqsave(&workq->lock, flags);
	if (!work->workq) {
		work->workq = workq;
		list_add_tail(&work->list, &workq->work_list);
		workq->pending++;
		wake_up_interruptible(&workq->waitq_head);
	}
	spin_unlock_irqrestore(&workq->lock, flags);
}

void wrap_cancel_delayed_work(work_struct_t *work)
{
	workqueue_struct_t *workq;
	unsigned long flags;

	if ((workq = xchg(&work->workq, NULL))) {
		spin_lock_irqsave(&workq->lock, flags);
		list_del(&work->list);
		/* don't decrement workq->pending here; otherwise, it
		 * may prematurely terminate the thread, as this work
		 * may already have been done (pending may have been
		 * decremented for it) */
		spin_unlock_irqrestore(&workq->lock, flags);
	}
}

workqueue_struct_t *wrap_create_wq(const char *name)
{
	workqueue_struct_t *workq = kmalloc(sizeof(*workq), GFP_KERNEL);
	if (!workq) {
		WARNING("couldn't allocate memory");
		return NULL;
	}
	memset(workq, 0, sizeof(*workq));
	init_waitqueue_head(&workq->waitq_head);
	spin_lock_init(&workq->lock);
	workq->name = name;
	INIT_LIST_HEAD(&workq->work_list);
	/* we don't need to wait for thread to start, so completion
	 * not used */
	workq->pid = kernel_thread(workq_thread, workq, 0);
	if (workq->pid <= 0) {
		kfree(workq);
		WARNING("couldn't start thread %s", name);
		return NULL;
	}
	return workq;
}

void wrap_destroy_wq(workqueue_struct_t *workq)
{
	while (workq->pid) {
		workq->pending = -1;
		wake_up_interruptible(&workq->waitq_head);
		schedule();
	}
	kfree(workq);
}