-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcarp_queue.c
More file actions
183 lines (147 loc) · 4.1 KB
/
carp_queue.c
File metadata and controls
183 lines (147 loc) · 4.1 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* carp_queue.c
*
* 2004 Copyright (c) Evgeniy Polyakov <xx>
* 2012 Copyright (c) Damien Churchill <damoxc@gmail.com>
* All rights reserved.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/workqueue.h>
#include <asm/spinlock.h>
#include "carp.h"
#include "carp_log.h"
#include "carp_queue.h"
static DEFINE_SPINLOCK(carp_queue_lock);
static struct workqueue_struct *carp_queue[2];
static struct list_head carp_works[2];
static int carp_work_counter;
static void carp_queue_wrapper(struct work_struct *ws)
{
struct carp_master_task *t = container_of(ws, struct carp_master_task, work);
atomic_inc(&t->task->refcnt);
t->task->callback(t->task->data);
atomic_dec(&t->task->refcnt);
}
struct carp_master_task *carp_alloc_task(struct __carp_master_task *t)
{
struct carp_master_task *newt;
newt = kmalloc(sizeof(struct carp_master_task), GFP_KERNEL);
if (!newt)
{
log("Failed to create new CARP master task.\n");
return NULL;
}
memset(newt, 0, sizeof(struct carp_master_task));
atomic_set(&t->refcnt, 1);
newt->task = t;
newt->task->id = carp_work_counter++;
INIT_WORK(&newt->work, &carp_queue_wrapper);
return newt;
}
static void carp_free_task(struct carp_master_task *t)
{
// TODO: read up and see if this is an okay method to use or not, it was
// cancel_delayed_work before, which seems a bit out of place as we aren't
// using any delayed work stuff here.
cancel_work_sync(&t->work);
while(atomic_read(&t->task->refcnt))
schedule_timeout(10);
kfree(t);
}
int carp_add_task(struct __carp_master_task *mt, int num)
{
struct carp_master_task *newt;
if (num != MASTER_QUEUE && num != BACKUP_QUEUE)
return -EINVAL;
newt = carp_alloc_task(mt);
if (!newt)
return -ENOMEM;
list_add_tail(&newt->entry, &carp_works[num]);
spin_unlock(&carp_queue_lock);
return 0;
}
void carp_del_task(struct __carp_master_task *mt, int num)
{
struct list_head *ent, *n;
struct carp_master_task *t = NULL;
int found = 0;
if (num != MASTER_QUEUE && num != BACKUP_QUEUE)
return;
spin_lock(&carp_queue_lock);
list_for_each_safe(ent, n, &carp_works[num])
{
t = list_entry(ent, struct carp_master_task, entry);
if (t->task->id == mt->id) {
list_del(&t->entry);
found = 1;
break;
}
}
spin_unlock(&carp_queue_lock);
if (found)
{
atomic_dec(&t->task->refcnt);
carp_free_task(t);
}
}
void carp_call_queue(int num)
{
struct list_head *ent, *n;
struct carp_master_task *t;
if (num != MASTER_QUEUE && num != BACKUP_QUEUE)
return;
spin_lock(&carp_queue_lock);
list_for_each_safe(ent, n, &carp_works[num])
{
t = list_entry(ent, struct carp_master_task, entry);
queue_work(carp_queue[num], &t->work);
}
spin_unlock(&carp_queue_lock);
}
int carp_init_queues(void)
{
int i;
for (i=0; i<2; ++i)
INIT_LIST_HEAD(&carp_works[i]);
carp_queue[MASTER_QUEUE] = create_workqueue("CARP_m");
if (!carp_queue[MASTER_QUEUE])
{
log("Failed to create master CARP queue.\n");
return -1;
}
carp_queue[BACKUP_QUEUE] = create_workqueue("CARP_b");
if (!carp_queue[BACKUP_QUEUE])
{
destroy_workqueue(carp_queue[MASTER_QUEUE]);
log("Failed to create backup CARP queue.\n");
return -1;
}
return 0;
}
void carp_flush_queue(int num)
{
if (num != MASTER_QUEUE && num != BACKUP_QUEUE)
return;
flush_workqueue(carp_queue[num]);
}
void carp_fini_queues(void)
{
carp_flush_queue(MASTER_QUEUE);
destroy_workqueue(carp_queue[MASTER_QUEUE]);
carp_flush_queue(BACKUP_QUEUE);
destroy_workqueue(carp_queue[BACKUP_QUEUE]);
}