summaryrefslogtreecommitdiffstats
path: root/lib/libkse/thread/thr_priority_queue.c
blob: 2822aa81e7c45c710006a128ce009a9b3dce76c0 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Daniel Eischen.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */
#include <stdlib.h>
#include <sys/queue.h>
#include <string.h>
#include <pthread.h>
#include "thr_private.h"

/* Prototypes: */
static void pq_insert_prio_list(pq_queue_t *pq, int prio);

#if defined(_PTHREADS_INVARIANTS)

#define PQ_IN_SCHEDQ	(THR_FLAGS_IN_RUNQ | THR_FLAGS_IN_WAITQ)

#define PQ_SET_ACTIVE(pq)		(pq)->pq_flags |= PQF_ACTIVE
#define PQ_CLEAR_ACTIVE(pq)		(pq)->pq_flags &= ~PQF_ACTIVE
#define PQ_ASSERT_ACTIVE(pq, msg)	do {		\
	if (((pq)->pq_flags & PQF_ACTIVE) == 0)		\
		PANIC(msg);				\
} while (0)
#define PQ_ASSERT_INACTIVE(pq, msg)	do {		\
	if (((pq)->pq_flags & PQF_ACTIVE) != 0)		\
		PANIC(msg);				\
} while (0)
#define PQ_ASSERT_IN_WAITQ(thrd, msg)	do {		\
	if (((thrd)->flags & THR_FLAGS_IN_WAITQ) == 0) \
		PANIC(msg);				\
} while (0)
#define PQ_ASSERT_IN_RUNQ(thrd, msg)	do {		\
	if (((thrd)->flags & THR_FLAGS_IN_RUNQ) == 0) \
		PANIC(msg);				\
} while (0)
#define PQ_ASSERT_NOT_QUEUED(thrd, msg) do {		\
	if (((thrd)->flags & PQ_IN_SCHEDQ) != 0)	\
		PANIC(msg);				\
} while (0)

#else

#define PQ_SET_ACTIVE(pq)
#define PQ_CLEAR_ACTIVE(pq)
#define PQ_ASSERT_ACTIVE(pq, msg)
#define PQ_ASSERT_INACTIVE(pq, msg)
#define PQ_ASSERT_IN_WAITQ(thrd, msg)
#define PQ_ASSERT_IN_RUNQ(thrd, msg)
#define PQ_ASSERT_NOT_QUEUED(thrd, msg)

#endif

int
_pq_alloc(pq_queue_t *pq, int minprio, int maxprio)
{
	int ret = 0;
	int prioslots = maxprio - minprio + 1;

	if (pq == NULL)
		ret = -1;

	/* Create the priority queue with (maxprio - minprio + 1) slots: */
	else if	((pq->pq_lists =
	    (pq_list_t *) malloc(sizeof(pq_list_t) * prioslots)) == NULL)
		ret = -1;

	else {
		/* Remember the queue size: */
		pq->pq_size = prioslots;
		ret = _pq_init(pq);
	}
	return (ret);
}

void
_pq_free(pq_queue_t *pq)
{
	if ((pq != NULL) && (pq->pq_lists != NULL))
		free(pq->pq_lists);
}

int
_pq_init(pq_queue_t *pq)
{
	int i, ret = 0;

	if ((pq == NULL) || (pq->pq_lists == NULL))
		ret = -1;

	else {
		/* Initialize the queue for each priority slot: */
		for (i = 0; i < pq->pq_size; i++) {
			TAILQ_INIT(&pq->pq_lists[i].pl_head);
			pq->pq_lists[i].pl_prio = i;
			pq->pq_lists[i].pl_queued = 0;
		}
		/* Initialize the priority queue: */
		TAILQ_INIT(&pq->pq_queue);
		pq->pq_flags = 0;
		pq->pq_threads = 0;
	}
	return (ret);
}

void
_pq_remove(pq_queue_t *pq, pthread_t pthread)
{
	int prio = pthread->active_priority;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	PQ_ASSERT_INACTIVE(pq, "_pq_remove: pq_active");
	PQ_SET_ACTIVE(pq);
	PQ_ASSERT_IN_RUNQ(pthread, "_pq_remove: Not in priority queue");

	/*
	 * Remove this thread from priority list.  Note that if
	 * the priority list becomes empty, it is not removed
	 * from the priority queue because another thread may be
	 * added to the priority list (resulting in a needless
	 * removal/insertion).  Priority lists are only removed
	 * from the priority queue when _pq_first is called.
	 */
	TAILQ_REMOVE(&pq->pq_lists[prio].pl_head, pthread, pqe);
	pq->pq_threads--;
	/* This thread is now longer in the priority queue. */
	pthread->flags &= ~THR_FLAGS_IN_RUNQ;
	
	PQ_CLEAR_ACTIVE(pq);
}


void
_pq_insert_head(pq_queue_t *pq, pthread_t pthread)
{
	int prio;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	PQ_ASSERT_INACTIVE(pq, "_pq_insert_head: pq_active");
	PQ_SET_ACTIVE(pq);
	PQ_ASSERT_NOT_QUEUED(pthread,
	    "_pq_insert_head: Already in priority queue");

	prio = pthread->active_priority;
	TAILQ_INSERT_HEAD(&pq->pq_lists[prio].pl_head, pthread, pqe);
	if (pq->pq_lists[prio].pl_queued == 0)
		/* Insert the list into the priority queue: */
		pq_insert_prio_list(pq, prio);
	pq->pq_threads++;
	/* Mark this thread as being in the priority queue. */
	pthread->flags |= THR_FLAGS_IN_RUNQ;

	PQ_CLEAR_ACTIVE(pq);
}


void
_pq_insert_tail(pq_queue_t *pq, pthread_t pthread)
{
	int prio;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	PQ_ASSERT_INACTIVE(pq, "_pq_insert_tail: pq_active");
	PQ_SET_ACTIVE(pq);
	PQ_ASSERT_NOT_QUEUED(pthread,
	    "_pq_insert_tail: Already in priority queue");

	prio = pthread->active_priority;
	TAILQ_INSERT_TAIL(&pq->pq_lists[prio].pl_head, pthread, pqe);
	if (pq->pq_lists[prio].pl_queued == 0)
		/* Insert the list into the priority queue: */
		pq_insert_prio_list(pq, prio);
	pq->pq_threads++;
	/* Mark this thread as being in the priority queue. */
	pthread->flags |= THR_FLAGS_IN_RUNQ;

	PQ_CLEAR_ACTIVE(pq);
}


pthread_t
_pq_first(pq_queue_t *pq)
{
	pq_list_t *pql;
	pthread_t pthread = NULL;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	PQ_ASSERT_INACTIVE(pq, "_pq_first: pq_active");
	PQ_SET_ACTIVE(pq);

	while (((pql = TAILQ_FIRST(&pq->pq_queue)) != NULL) &&
	    (pthread == NULL)) {
		if ((pthread = TAILQ_FIRST(&pql->pl_head)) == NULL) {
			/*
			 * The priority list is empty; remove the list
			 * from the queue.
			 */
			TAILQ_REMOVE(&pq->pq_queue, pql, pl_link);

			/* Mark the list as not being in the queue: */
			pql->pl_queued = 0;
		}
	}

	PQ_CLEAR_ACTIVE(pq);
	return (pthread);
}


static void
pq_insert_prio_list(pq_queue_t *pq, int prio)
{
	pq_list_t *pql;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	PQ_ASSERT_ACTIVE(pq, "pq_insert_prio_list: pq_active");

	/*
	 * The priority queue is in descending priority order.  Start at
	 * the beginning of the queue and find the list before which the
	 * new list should be inserted.
	 */
	pql = TAILQ_FIRST(&pq->pq_queue);
	while ((pql != NULL) && (pql->pl_prio > prio))
		pql = TAILQ_NEXT(pql, pl_link);

	/* Insert the list: */
	if (pql == NULL)
		TAILQ_INSERT_TAIL(&pq->pq_queue, &pq->pq_lists[prio], pl_link);
	else
		TAILQ_INSERT_BEFORE(pql, &pq->pq_lists[prio], pl_link);

	/* Mark this list as being in the queue: */
	pq->pq_lists[prio].pl_queued = 1;
}
OpenPOWER on IntegriCloud