Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConcurrentQueueImpl |
|
| 0.0;0 | ||||
ConcurrentQueueImpl$Entry |
|
| 0.0;0 | ||||
ConcurrentQueueImpl$HandleImpl |
|
| 0.0;0 |
1 | /* | |
2 | * | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
4 | * | |
5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
6 | * | |
7 | * The contents of this file are subject to the terms of either the GNU | |
8 | * General Public License Version 2 only ("GPL") or the Common Development | |
9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
10 | * may not use this file except in compliance with the License. You can obtain | |
11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
13 | * language governing permissions and limitations under the License. | |
14 | * | |
15 | * When distributing the software, include this License Header Notice in each | |
16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
17 | * Sun designates this particular file as subject to the "Classpath" exception | |
18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
19 | * accompanied this code. If applicable, add the following below the License | |
20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
21 | * identifying information: "Portions Copyrighted [year] | |
22 | * [name of copyright owner]" | |
23 | * | |
24 | * Contributor(s): | |
25 | * | |
26 | * If you wish your version of this file to be governed by only the CDDL or | |
27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
28 | * elects to include this software in this distribution under the [CDDL or GPL | |
29 | * Version 2] license." If you don't indicate a single choice of license, a | |
30 | * recipient has the option to distribute your version of this file under | |
31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
34 | * only if the new code is made subject to such option by the copyright | |
35 | * holder. | |
36 | * | |
37 | */ | |
38 | ||
39 | package com.sun.grizzly.connectioncache.impl.concurrent; | |
40 | ||
41 | import com.sun.grizzly.connectioncache.spi.concurrent.ConcurrentQueue; | |
42 | ||
43 | public class ConcurrentQueueImpl<V> implements ConcurrentQueue<V> { | |
44 | // This implementation of ConcurrentQueue is unsynchronized, for use in | |
45 | // other implementations that manage concurrency with locks. | |
46 | // | |
47 | // Structure: Head points to a node containing a null value, which is a special marker. | |
48 | // head.next is the first element, head.prev is the last. The queue is empty if | |
49 | // head.next == head.prev == head. | |
50 | 6 | final Entry<V> head = new Entry<V>( null ) ; |
51 | 6 | int count = 0 ; |
52 | ||
53 | 6 | public ConcurrentQueueImpl() { |
54 | 6 | head.next = head ; |
55 | 6 | head.prev = head ; |
56 | 6 | } |
57 | ||
58 | 1042 | private final class Entry<V> { |
59 | 1049 | Entry<V> next = null ; |
60 | 1049 | Entry<V> prev = null ; |
61 | private HandleImpl<V> handle ; | |
62 | ||
63 | 1049 | Entry( V value ) { |
64 | 1049 | handle = new HandleImpl<V>( this, value ) ; |
65 | 1049 | } |
66 | ||
67 | HandleImpl<V> handle() { | |
68 | 1073 | return handle ; |
69 | } | |
70 | } | |
71 | ||
72 | private final class HandleImpl<V> implements Handle<V> { | |
73 | private Entry<V> entry ; | |
74 | private final V value ; | |
75 | private boolean valid ; | |
76 | ||
77 | 1049 | HandleImpl( Entry<V> entry, V value ) { |
78 | 1049 | this.entry = entry ; |
79 | 1049 | this.value = value ; |
80 | 1049 | this.valid = true ; |
81 | 1049 | } |
82 | ||
83 | Entry<V> entry() { | |
84 | 0 | return entry ; |
85 | } | |
86 | ||
87 | public V value() { | |
88 | 15 | return value ; |
89 | } | |
90 | ||
91 | /** Delete the element corresponding to this handle | |
92 | * from the queue. Takes constant time. | |
93 | * @return element deleted from queue, (yes or no) | |
94 | */ | |
95 | public boolean remove() { | |
96 | 1042 | if (!valid) { |
97 | 0 | return false ; |
98 | } | |
99 | ||
100 | 1042 | valid = false ; |
101 | ||
102 | 1042 | entry.next.prev = entry.prev ; |
103 | 1042 | entry.prev.next = entry.next ; |
104 | 1042 | count-- ; |
105 | ||
106 | 1042 | entry.prev = null ; |
107 | 1042 | entry.next = null ; |
108 | 1042 | entry.handle = null ; |
109 | 1042 | entry = null ; |
110 | 1042 | valid = false ; |
111 | 1042 | return true ; |
112 | } | |
113 | } | |
114 | ||
115 | public int size() { | |
116 | 64 | return count ; |
117 | } | |
118 | ||
119 | /** Add a new element to the tail of the queue. | |
120 | * Returns a handle for the element in the queue. | |
121 | * @param arg element to append to the queue | |
122 | * @return {@link Handle} for the element in the queue | |
123 | */ | |
124 | public Handle<V> offer( V arg ) { | |
125 | 1043 | if (arg == null) |
126 | 0 | throw new IllegalArgumentException( "Argument cannot be null" ) ; |
127 | ||
128 | 1043 | Entry<V> entry = new Entry<V>( arg ) ; |
129 | ||
130 | 1043 | entry.next = head ; |
131 | 1043 | entry.prev = head.prev ; |
132 | 1043 | head.prev.next = entry ; |
133 | 1043 | head.prev = entry ; |
134 | 1043 | count++ ; |
135 | ||
136 | 1043 | return entry.handle() ; |
137 | } | |
138 | ||
139 | /** Return an element from the head of the queue. | |
140 | * The element is removed from the queue. | |
141 | * @return an element at the head of the queue | |
142 | */ | |
143 | public V poll() { | |
144 | 23 | Entry<V> first = null ; |
145 | 23 | V value = null ; |
146 | ||
147 | 23 | first = head.next ; |
148 | 23 | if (first == head) |
149 | 8 | return null ; |
150 | else { | |
151 | 15 | value = first.handle().value() ; |
152 | ||
153 | // assert that the following expression returns true! | |
154 | 15 | first.handle().remove() ; |
155 | } | |
156 | ||
157 | // Once first is removed from the queue, it is invisible to other threads, | |
158 | // so we don't need to synchronize here. | |
159 | 15 | first.next = null ; |
160 | 15 | first.prev = null ; |
161 | 15 | return value ; |
162 | } | |
163 | } |