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 */
013package org.jikesrvm.mm.mmtk;
014
015import org.jikesrvm.VM;
016import org.jikesrvm.scheduler.Synchronization;
017import org.jikesrvm.runtime.Entrypoints;
018
019import org.vmmagic.pragma.*;
020import org.vmmagic.unboxed.Offset;
021
022/**
023 * A counter that supports atomic increment and reset.
024 */
025@Uninterruptible
026public final class SynchronizedCounter extends org.mmtk.vm.SynchronizedCounter {
027
028  private static Offset offset = Offset.max();
029
030  public static void boot() {
031    offset = Entrypoints.synchronizedCounterField.getOffset();
032  }
033
034  @Entrypoint
035  private int count = 0;
036
037  @Override
038  public int reset() {
039    //    int offset = Interface.synchronizedCounterOffset;
040    int oldValue = count;
041    int actualOldValue = Synchronization.fetchAndAdd(this, offset, -oldValue);
042    if (actualOldValue != oldValue) {
043      VM.sysWriteln("oldValue = ", oldValue);
044      VM.sysWriteln("actualOldValue = ", actualOldValue);
045      VM.sysFail("Concurrent use of SynchronizedCounter.reset");
046    }
047    return oldValue;
048  }
049
050  @Override
051  public int increment() {
052    if (VM.VerifyAssertions) VM._assert(!offset.isMax());
053    return Synchronization.fetchAndAdd(this, offset, 1);
054  }
055
056  @Override
057  public int peek() {
058    return count;
059  }
060
061}