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.mm.mmtk;
014
015 import org.jikesrvm.VM;
016 import org.jikesrvm.scheduler.Synchronization;
017 import org.jikesrvm.runtime.Entrypoints;
018
019 import org.vmmagic.pragma.*;
020 import org.vmmagic.unboxed.Offset;
021
022 /**
023 * A counter that supports atomic increment and reset.
024 */
025 @Uninterruptible
026 public 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 public int reset() {
038 // int offset = Interface.synchronizedCounterOffset;
039 int oldValue = count;
040 int actualOldValue = Synchronization.fetchAndAdd(this, offset, -oldValue);
041 if (actualOldValue != oldValue) {
042 VM.sysWriteln("oldValue = ", oldValue);
043 VM.sysWriteln("actualOldValue = ", actualOldValue);
044 VM.sysFail("Concurrent use of SynchronizedCounter.reset");
045 }
046 return oldValue;
047 }
048
049 // Returns the value before the add
050 //
051 public int increment() {
052 if (VM.VerifyAssertions) VM._assert(!offset.isMax());
053 return Synchronization.fetchAndAdd(this, offset, 1);
054 }
055
056 public int peek() {
057 return count;
058 }
059
060 }