Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2525fb0dd0aa018328a0f013d0d23d4b10836194 (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
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
/*******************************************************************************
 * Copyright (c) 1997, 2008 by ProSyst Software GmbH
 * http://www.prosyst.com
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.util.impl.tpt.timer;

/**
 * @author Pavlin Dobrev
 * @version 1.0
 */

class TimerQueue {

	static final int POOL_SIZE = 20;
	QueueElement[] pool = new QueueElement[POOL_SIZE];
	int filled = 0;

	QueueElement first;
	QueueElement lastInserted;

	TimerQueue() {
	}

	/**
	 * Adds a new task to the priority queue.
	 */
	void add(TimerQueueNode task) {
		QueueElement toAdd = getQueueElement();
		toAdd.node = task;

		if (first == null) {
			first = toAdd;
		} else {
			insertElement(toAdd);
		}
	}

	/**
	 * Return the "head task" of the priority queue. (The head task is an task
	 * with the lowest nextExecutionTime.)
	 */
	TimerQueueNode getMin() {
		return (first != null) ? first.node : null;
	}

	/**
	 * Remove the head task from the priority queue.
	 */
	void removeMin() {
		if (first != null) {
			if (lastInserted == first) {
				lastInserted = null;
			}
			if (filled < POOL_SIZE) {
				QueueElement toFree = first;
				first = first.next;
				freeQueueElement(toFree);
			} else {
				first = first.next;
			}
		}
	}

	/**
	 * Sets the nextExecutionTime associated with the head task to the specified
	 * value, and adjusts priority queue accordingly.
	 */
	void rescheduleMin(long newTime) {
		first.node.runOn = newTime;
		if (first.next != null) {
			if (lastInserted == first) {
				lastInserted = null;
			}
			QueueElement el = first;
			first = first.next;
			el.next = null;
			insertElement(el);
		}
	}

	/**
	 * Returns true if the priority queue contains no elements.
	 */
	boolean isEmpty() {
		return first == null;
	}

	/**
	 * Removes all elements from the priority queue.
	 */
	void clear() {
		while (first != null) {
			first.node.returnInPool();
			first = first.next;
		}
		lastInserted = null;
	}

	void insertElement(QueueElement newElement) {
		if (first.node.runOn >= newElement.node.runOn) {
			/* private case - insert in the beginning of the queue */
			newElement.next = first;
			first = newElement;
		} else if (lastInserted != null) {
			if (lastInserted.node.runOn == newElement.node.runOn) {
				QueueElement tmp = lastInserted.next;
				lastInserted.next = newElement;
				newElement.next = tmp;
			} else if (lastInserted.node.runOn > newElement.node.runOn) {
				// System.out.println("insert 1");
				doInsertElement(first, newElement);
			} else {
				// System.out.println("insert 2");
				doInsertElement(lastInserted, newElement);
			}
		} else {
			// System.out.println("insert 3");
			doInsertElement(first, newElement);
		}
	}

	/**
	 * elToInsert should not be placed before firstEl, because firstEl may not
	 * be the first element of the queue
	 */
	void doInsertElement(QueueElement firstEl, QueueElement elToInsert) {
		QueueElement tmp = firstEl;
		QueueElement prev = firstEl;
		while (tmp != null && tmp.node.runOn < elToInsert.node.runOn) {
			prev = tmp;
			tmp = tmp.next;
		}
		if (tmp == null) {
			/* reached the end of the queue */
			prev.next = elToInsert;
		} else {
			prev.next = elToInsert;
			elToInsert.next = tmp;
		}
		lastInserted = elToInsert;
	}

	void removeTimerNode(TimerQueueNode node) {
		QueueElement tmp = first;
		QueueElement prev = null;
		while (tmp != null) {
			if (node.listener == tmp.node.listener && node.event == tmp.node.event) {
				if (prev != null) {
					if (lastInserted == tmp) {
						lastInserted = prev;
					}
					prev.next = tmp.next;
					if (filled < POOL_SIZE) {
						freeQueueElement(tmp);
					}
				} else {
					/* removing the first element */
					if (lastInserted == first) {
						lastInserted = null;
					}
					if (filled < POOL_SIZE) {
						QueueElement toFree = first;
						first = first.next;
						freeQueueElement(toFree);
					} else {
						first = first.next;
					}
				}
				break;
			}
			prev = tmp;
			tmp = tmp.next;
		}
	}

	private QueueElement getQueueElement() {
		return (filled > 0) ? pool[--filled] : new QueueElement();
	}

	private void freeQueueElement(QueueElement toFree) {
		if (filled < POOL_SIZE) {
			pool[filled] = toFree;
			filled++;
			toFree.next = null;
			toFree.node = null;
		}
	}

	private class QueueElement {
		QueueElement next;
		TimerQueueNode node;

		public QueueElement() {
		}
	}

}

Back to the top