Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: pkg/analysis_server/lib/src/operation/operation_queue.dart

Issue 308003009: Add ServerOperation and queue. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changes for review comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library operation.queue;
6
7 import 'dart:collection';
8
9 import 'package:analysis_server/src/analysis_server.dart';
10 import 'package:analysis_server/src/operation/operation.dart';
11
12
13 /**
14 * A queue of operations in an [AnalysisServer].
15 */
16 class ServerOperationQueue {
17 final List<ServerOperationPriority> _analysisPriorities = [
18 ServerOperationPriority.ANALYSIS_CONTINUE,
19 ServerOperationPriority.ANALYSIS];
20
21 final AnalysisServer _server;
22 final List<Queue<ServerOperation>> _queues = <Queue<ServerOperation>>[];
23
24 ServerOperationQueue(this._server) {
25 for (int i = 0; i < ServerOperationPriority.COUNT; i++) {
26 var queue = new DoubleLinkedQueue<ServerOperation>();
27 _queues.add(queue);
28 }
29 }
30
31 /**
32 * Adds the given operation to this queue. The exact position in the queue
33 * depends on the priority of the given operation relative to the priorities
34 * of the other operations in the queue.
35 */
36 void add(ServerOperation operation) {
37 int queueIndex = operation.priority.ordinal;
38 Queue<ServerOperation> queue = _queues[queueIndex];
39 queue.addLast(operation);
40 }
41
42 /**
43 * Removes all elements in the queue.
44 */
45 void clear() {
46 for (Queue<ServerOperation> queue in _queues) {
47 queue.clear();
48 }
49 }
50
51 /**
52 * Returns `true` if there are no queued [ServerOperation]s.
53 */
54 bool get isEmpty {
55 return _queues.every((queue) => queue.isEmpty);
56 }
57
58 /**
59 * Returns the next operation to perform or `null` if empty.
60 */
61 ServerOperation take() {
62 // try to find a priority analysis operarion
63 for (ServerOperationPriority priority in _analysisPriorities) {
64 Queue<ServerOperation> queue = _queues[priority.ordinal];
65 for (PerformAnalysisOperation operation in queue) {
66 if (_server.isPriorityContext(operation.context)) {
67 queue.remove(operation);
68 return operation;
69 }
70 }
71 }
72 // non-priority operations
73 for (Queue<ServerOperation> queue in _queues) {
74 if (!queue.isEmpty) {
75 return queue.removeFirst();
76 }
77 }
78 // empty
79 return null;
80 }
81 }
82
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698