001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.tuningfork;
014    
015    import com.ibm.tuningfork.tracegen.chunk.RawChunk;
016    
017    /**
018     * A Queue of chunks intended to keep track of meta-chunks.
019     * Therefore it can be implemented using Java-level synchronization and
020     * allocation operations to wrap the Chunks in Queue nodes.
021     */
022    public class ChunkQueue {
023    
024      private Node head = null;
025      private Node tail = null;
026    
027      public synchronized void enqueue(RawChunk c) {
028        Node newNode = new Node(c);
029        if (tail == null) {
030          head = newNode;
031          tail = newNode;
032        } else {
033          tail.next = newNode;
034          tail = newNode;
035        }
036      }
037    
038      public synchronized RawChunk dequeue() {
039        if (head != null) {
040          RawChunk result = head.chunk;
041          head = head.next;
042          if (head == null) {
043            tail = null;
044          }
045          return result;
046        } else {
047          return null;
048        }
049      }
050    
051      public boolean isEmpty() {
052        return head == null;
053      }
054    
055      private static final class Node {
056        final RawChunk chunk;
057        Node next;
058        Node(RawChunk c) { this.chunk=c; }
059      };
060    }