summaryrefslogtreecommitdiffstats
path: root/src/mpq.h
blob: 5a861ca08b59732570801cf4193c46d0fa15e6a9 (plain) (blame)
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
#ifndef _MPQ_H_
#define _MPQ_H_

#include <sys/mutex.h>
#include <sys/kthread.h>

struct mpq_item {
	struct wg_pkt pkt;
};

struct mpq {
	struct mutex	mtx;
	int	destroy;
	void	(*parallel)(struct mpq_item *);
	void	(*serial)(struct mpq_item *);
	size_t	parallel_head, serial_head, tail, len;
	struct mpq_queue_item {
		enum mpq_item_state {
			MPQ_ITEM_NONE = 0,
			MPQ_ITEM_WAITING,
			MPQ_ITEM_PROCESSING,
			MPQ_ITEM_DONE,
		} state;
		struct mpq_item data;
	} *items;
};

/*
 * H = Head, T = Tail, N = len
 * H	T	T-H	N-(H-T)
 * 5	5	0	8
 * 5	6	1	9
 * 5	7	2	10
 * 5	0	-5	3
 * 5	1	-4	4
 * 5	2	-3	5
 * 5	3	-2	6
 * 5	4	-1	7
*/

#define INC_WRAP(v, max) (v) = ((v) >= (max - 1) ? 0 : (v) + 1)
#define CIRC_LEN(h, t, n) ((t) >= (h) ? ((t) - (h)) : ((n) - ((h) - (t))))

#define MPQ_SERIAL_LEN(q) CIRC_LEN((q)->serial_head, (q)->tail, (q)->len)
#define MPQ_SERIAL_FULL(q) (MPQ_SERIAL_LEN(q) >= ((q)->len - 1))
#define MPQ_SERIAL_EMPTY(q) (MPQ_SERIAL_LEN(q) == 0)

#define MPQ_PARALLEL_LEN(q) CIRC_LEN((q)->parallel_head, (q)->tail, (q)->len)
#define MPQ_PARALLEL_FULL(q) (MPQ_PARALLEL_LEN(q) >= ((q)->len - 1))
#define MPQ_PARALLEL_EMPTY(q) (MPQ_PARALLEL_LEN(q) == 0)


void mpq_init(struct mpq *, int ipl, uint8_t, void (*)(struct mpq_item *),
		void (*)(struct mpq_item *));
int mpq_resize(struct mpq *, size_t);
int mpq_add(struct mpq *, struct mpq_item *);
void mpq_destroy(struct mpq *);

void
mpq_parallel_worker(void *_q)
{
	struct mpq *q = _q;
	struct mpq_queue_item *i;
	while (1) {
		while (MPQ_PARALLEL_EMPTY(q)) {
			tsleep(&q->tail, PZERO, "bored", 0);
		}

		mtx_enter(&q->mtx);
		i = &q->items[q->parallel_head];
		if (i->state != MPQ_ITEM_WAITING) {
			mtx_leave(&q->mtx);
			continue;
		}

		INC_WRAP(q->parallel_head, q->len);
		i->state = MPQ_ITEM_PROCESSING;
		mtx_leave(&q->mtx);

		q->parallel(&i->data);

		mtx_enter(&q->mtx);
		i->state = MPQ_ITEM_DONE;
		mtx_leave(&q->mtx);

		wakeup_one(&q->parallel_head);
	}
}

void
mpq_serial_worker(void *_q)
{
	struct mpq *q = _q;
	struct mpq_queue_item *i;
	while (1) {
		while (q->items[q->serial_head].state != MPQ_ITEM_DONE) {
			tsleep(&q->parallel_head, PZERO, "bored", 0);
		}

		i = &q->items[q->serial_head];

		q->serial(&i->data);

		INC_WRAP(q->serial_head, q->len);

		mtx_enter(&q->mtx);
		i->state = MPQ_ITEM_NONE;
		mtx_leave(&q->mtx);
	}
}

int
mpq_add(struct mpq *q, struct mpq_item *i)
{
	mtx_enter(&q->mtx);
	if (MPQ_SERIAL_FULL(q)) {
		mtx_leave(&q->mtx);
		return 1;
	}

	q->items[q->tail].data = *i;
	q->items[q->tail].state = MPQ_ITEM_WAITING;
	INC_WRAP(q->tail, q->len);
	mtx_leave(&q->mtx);
	wakeup_one(&q->tail);
	return 0;
}

void
mpq_init(struct mpq *q, int ipl, uint8_t nthreads, void (*parallel)(struct mpq_item *), void(*serial)(struct mpq_item *))
{
	mtx_init(&q->mtx, ipl);
	q->parallel = parallel;
	q->serial = serial;
	q->destroy = 0;
	q->parallel_head = 0;
	q->serial_head = 0;
	q->tail = 0;
	q->len = 0;

	kthread_create(mpq_serial_worker, q, NULL, "mpq_serial");
	while(nthreads--)
		kthread_create(mpq_parallel_worker, q, NULL, "mpq_parallel");
}

int
mpq_resize(struct mpq *q, size_t len)
{
	q->len = len;
	q->items = mallocarray(q->len, sizeof(struct mpq_queue_item), M_DEVBUF, M_WAIT | M_ZERO);
	return 0;
}

void
mpq_destroy(struct mpq *q)
{
	q->destroy = 1;
}

#endif /* _MPQ_H_ */