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    
014    package org.mmtk.utility.heap;
015    
016    import org.mmtk.utility.Log;
017    import org.mmtk.plan.CollectorContext;
018    import org.mmtk.utility.options.Options;
019    import org.mmtk.vm.Monitor;
020    import org.mmtk.vm.VM;
021    
022    import org.vmmagic.pragma.*;
023    
024    /**
025     * This context concurrently zeroes a space when triggered.
026     */
027    @Uninterruptible
028    public class ConcurrentZeroingContext extends CollectorContext {
029    
030      private PageResource pr;
031      private Monitor lock;
032      private volatile int trigger;
033    
034      public ConcurrentZeroingContext(PageResource pr) {
035        this.pr = pr;
036        this.lock = VM.newHeavyCondLock("ConcurrentZeroingLock");
037      }
038    
039      public void trigger() {
040        lock.lock();
041        trigger++;
042        lock.broadcast();
043        lock.unlock();
044      }
045    
046      @Override
047      public void run(){
048        if (Options.verbose.getValue() >= 2) {
049          Log.writeln("ZeroingThread running");
050        }
051        while(true) {
052          lock.lock();
053          while (trigger == 0) {
054            lock.await();
055          }
056          trigger--;
057          lock.unlock();
058          pr.concurrentZeroing();
059        }
060      }
061    }