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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* @(#)SynchQueue.java 0.9.0 04/26/2000 - 13:39:11
*
* Copyright (C) 2000,,2003 2002 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.util.datastruct.v1;
/**
* A Queue optimized for synchronized access. If a request is made to dequeue()
* an item on an empty list, then the requesting thread waits until
* an object is placed on the queue.
* <P>
* Care has been taken to ensure proper synchronization. Synchronization
* has been optimized so that adding an element to the list does not stop
* access to removing an item off the list. The only conflicts occur when
* the list has 1 or less items. All synchronization is performed on
* internal objects, so the queue itself is still available for outside
* classes to use as a synchronization key.
* <P>
* An additional optimization can be made by pooling the ListElement objects,
* although it leads to another syncrhonization collision. An alternative
* would be to make a ListElement specific synch-queue which only stores
* ListElement objects, and doesn't care about the stored values. To prevent
* it from having a major memory footprint, it could set a cap on the number
* of elements it stores.
* <P>
* A small memory leak is present, for performance purposes. If the
* list is "emptied", then there still remains a reference to a list element
* on the tail. I have optimized this by nulling out the referenced value,
* but the ListElement remains. Hopefully, a single ListElement won't be
* much of a memory burden.
* <P>
* <H3>Changes made for 0.9.1:</H3>
* <UL>
* <LI>The inner ListElement class has been changed to be a private
* static class. This reduces a bit of a memory overhead caused by the
* original implementation of having the class be non-static.
* <LI>Because of the ordering of the <tt>size</tt> assignment, and
* that the {@link #size()} method is unsynchronized, a situation
* could occur where {@link #size()} can return an invalid number
* (see <a href="http://sourceforge.net/tracker/index.php?func=detail&aid=459457&group_id=22594&atid=375589">
* bug #459457 </a>), such as -9.
* <P>
* A quick analysis this may happen during the enqueue method
* when an optimizer sets the tail to the new value before it
* sets the size.
* <P>
* The fix involves adding another lock in the {@link
* #enqueue( Object )}
* method around the new element (which will become the next tail
* element), and making the {@link SynchQueue.ListElement.next}
* and <tt>size</tt> values volatile (this will force their setting
* to be in the same order specified in the code).
* <LI>Removed the double-check locking optimization due to possible
* failures occuring with it.
* </UL>
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @since April 26, 2000 (0.9.0 Alpha)
* @version $Date: 2003/02/10 22:52:44 $
*/
public class SynchQueue
{
/**
* Inner class to maintain the list's objects, and a next pointer.
*/
private static class ListElement
{
public Object value;
public volatile ListElement next;
public ListElement() {}
public ListElement( Object o )
{
this.value = o;
}
}
/**
* List pointers; head points to the removal point, and tail points
* to the addition point.
*/
private ListElement head, tail;
/**
* Internal size of the queue.
*/
private volatile int size = 0;
/**
* Create a new Synchronized Queue with an empty list.
*/
public SynchQueue()
{
this.head = new ListElement();
this.tail = new ListElement();
this.tail.next = new ListElement();
}
/**
* Adds an element to the end of the queue. If the list is empty,
* then the next waiting thread is woken up. If the list has one or
* fewer elements, this this method will block any access to the queue,
* otherwise this only blocks access to adding to the list.
*
* @param o the object to place at the end of the list.
*/
public void enqueue( Object o )
{
ListElement le = new ListElement( o );
synchronized( le )
{
// note: double-checked locking does not work. See
// http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
// HOWEVER:
// we are doing an object pointer assignment, not a new Object()
// operation.
// "It will work for 32-bit primitive values
// "Although the double-checked locking idiom cannot be used
// for references to objects, it can work for 32-bit primitive
// values (e.g., int's or float's). Note that it does not work
// for long's or double's, since unsynchronized reads/writes of
// 64-bit primitives are not guaranteed to be atomic."
//
// So... will it work? Probably not, due to additional logic
// besides just the assignment.
synchronized( this.head )
{
if (this.head.next == null)
{
this.head.next = le;
this.head.notify();
}
}
synchronized( this.tail.next )
{
this.size++;
this.tail.next.next = le;
this.tail.next = le;
}
}
}
/**
* Removes and returns the first element in the list. If the list is
* empty, then the thread waits until a new element is placed onto the list.
* In general, this method will not be blocked by the enqueue() method.
*
* @see java.lang.Thread#interrupt()
* @see #enqueue( Object )
* @see #dequeue( long, int )
* @see #dequeue( long )
* @return the first element on the list, or <tt>null</tt> if a time-out
* occured.
* @exception InterruptedException thrown if the thread, while waiting,
* is interrupted.
*/
public Object dequeue()
throws InterruptedException
{
return dequeue( 0, 0 );
}
/**
* Removes and returns the first element in the list. If the list is
* empty, then the thread waits until a new element is placed onto the list.
* In general, this method will not be blocked by the enqueue() method.
* <P>
* The wait can be given a maximum time by specifying the <tt>timeout</tt>
* as a non-zero value. This means that if the given
* time expires, and nothing is in the queue, then <tt>null</tt> is
* returned. This allows for polling-like features for the queue.
* If <tt>timeout</tt> is zero, then real time is not taken into
* consideration and the thread simply waits until the object is added to
* the queue. If <tt>timeout</tt> is less than zero, then no waiting
* is performed if the queue is empty, and <tt>null</tt> is returned
* immediately.
*
* @param timeout the maximum time to wait in milliseconds.
* @see java.lang.Thread#interrupt()
* @see #enqueue( Object )
* @see #dequeue( long, int )
* @see #dequeue()
* @return the first element on the list, or <tt>null</tt> if a time-out
* occured.
* @exception InterruptedException thrown if the thread, while waiting,
* is interrupted.
*/
public Object dequeue( long timeout )
throws InterruptedException
{
return dequeue( timeout, 0 );
}
/**
* Removes and returns the first element in the list. If the list is
* empty, then the thread waits until a new element is placed onto the list.
* In general, this method will not be blocked by the enqueue() method.
* <P>
* The wait can be given a maximum time by specifying the <tt>timeout</tt>
* or <tt>nanos</tt> as non-zero values. This means that if the given
* time expires, and nothing is in the queue, then <tt>null</tt> is
* returned. This allows for polling-like features for the queue.
* The total wait time in milliseconds <tt> = 1000000*timeout + nanos</tt>.
* If the total wait is zero, then real time is not taken into
* consideration and the thread simply waits until the object is added to
* the queue. If <tt>timeout</tt> is less than zero, then no waiting
* is performed if the queue is empty, and <tt>null</tt> is returned
* immediately.
*
* @param timeout the maximum time to wait in milliseconds.
* @param nanos additional time, in nanoseconds range 0-999999.
* @see java.lang.Thread#interrupt()
* @see #enqueue( Object )
* @see #dequeue()
* @see #dequeue( long )
* @return the first element on the list, or <tt>null</tt> if a time-out
* occured.
* @exception InterruptedException thrown if the thread, while waiting,
* is interrupted.
*/
public Object dequeue( long timeout, int nanos )
throws InterruptedException
{
Object o;
float dTimeout = (float)(timeout + nanos);
synchronized( this.head )
{
// this used to be a while (head.next == null) loop,
// but now it's ugly to reduce the number of "if" checks.
if (this.head.next == null)
{
// quick check, but needs synchronization
if (timeout < 0)
{
return null;
}
while (true)
{
this.head.wait( timeout, nanos );
// check if the element was indeed added...
if (this.head.next != null)
{
// yep! Early out!
break;
}
// Check if we timed-out, or if it was a flakey
// notify
// - include an epsilon for the floating check...
if (dTimeout > 0.9f)
{
// time-out and a null, so crap out without doing
// anything.
return null;
}
}
} // end null checking block
// guaranteed to not have a null at this point
o = this.head.next.value;
// remove the minor memory leak
this.head.next.value = null;
synchronized( this.head.next )
{
this.head.next = this.head.next.next;
this.size--;
}
}
return o;
}
/**
* Retrieves the top-level object from the list without removing it.
* It momentarily blocks the retrieval from the list, but does not
* wait if the list is empty. Currently, there is no way to test if
* a null was placed in the list, or if the list is empty.
*
* @return if the list is empty, then <code>null</code> is returned,
* otherwise the contents of the top level element in the list is
* returned without removing it from the list.
*/
public Object peek()
{
Object o = null;
synchronized( this.head )
{
if (this.head.next != null)
{
o = this.head.next.value;
}
// else o = null;
}
return o;
}
/**
* Checks if the list is empty, by a simple, non-blocking check on
* the head element.
*
* @return <code>true</code> if the list contains no user elements,
* otherwise <code>false</code> if at least one user element is present
* on the list.
*/
public boolean isEmpty()
{
return (this.head.next == null);
}
/**
* @return the current size of the list. Since this method is
* completely unsynchronized, the returned value may be off by 1,
* due to threading issues.
*/
public int size()
{
return this.size;
}
/**
* Removes all the current data in the queue.
*/
public void removeAll()
{
synchronized( this.head )
{
synchronized( this.tail.next )
{
this.head.next = null;
// remove the minor memory leak
this.tail.next.value = null;
this.size = 0;
}
}
}
}
|