Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

LockLit.java

00001 package edu.ksu.cis.bandera.bir;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   James Corbett (corbett@hawaii.edu)     *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import ca.mcgill.sable.util.*;
00036 
00037 import java.util.*;
00038 
00039 /**
00040  * A lock literal value.
00041  * <p>
00042  * Can only appear in a BirState (not in transition system source).
00043  */
00044 
00045 public class LockLit implements Literal, BirConstants, Cloneable {
00046 
00047     int heldCount = 0;       // number of times acquired (0 if free)
00048     BirThread owner = null;  // acquiring thread
00049     boolean [] waiting;
00050     int [] waitHeldCount;
00051     TransSystem system;
00052     int numThreads;
00053 
00054     public LockLit(TransSystem system) {
00055     this.system = system;
00056     numThreads = system.getThreads().size();
00057     waiting = new boolean[numThreads];
00058     waitHeldCount = new int[numThreads];
00059     }
00060     public void apply(Switch sw)
00061     {
00062         ((ExprSwitch) sw).caseLockLit(this);
00063     }
00064     public BirThread getOwner() { return owner; }
00065     public TransSystem getSystem() { return system; }
00066     public Type getType() { return Type.WR_lockType; }
00067     public ThreadVector getWaitingThreads() {
00068     ThreadVector waitThreads = new ThreadVector();
00069     ThreadVector threads = system.getThreads();
00070     for (int i = 0; i < threads.size(); i++) {
00071         BirThread thread = threads.elementAt(i);
00072         if (waiting[thread.getId()])
00073         waitThreads.addElement(thread);
00074     }
00075     return waitThreads;
00076     }
00077     public LockLit nextState(int operation, BirThread thread, int choice) {
00078     LockLit nextState;
00079     try {
00080         nextState = (LockLit) this.clone();
00081     } catch (CloneNotSupportedException e) { return null; }
00082     switch (operation) {
00083     case LOCK: 
00084         if (owner == null)
00085         nextState.owner = thread;
00086         else 
00087         nextState.heldCount++;
00088         break;
00089     case UNLOCK: 
00090         if (heldCount == 0)
00091         nextState.owner = null;
00092         else 
00093         nextState.heldCount--;
00094         break;
00095     case WAIT: 
00096         nextState.waiting = (boolean []) waiting.clone();
00097         nextState.waiting[thread.getId()] = true;
00098         nextState.waitHeldCount = (int []) waitHeldCount.clone();
00099         nextState.waitHeldCount[thread.getId()] = heldCount;
00100         nextState.owner = null;
00101         break;
00102     case UNWAIT:
00103         nextState.heldCount = waitHeldCount[thread.getId()];
00104         nextState.owner = thread;
00105         break;
00106     case NOTIFY:
00107         nextState.waiting = (boolean []) waiting.clone();
00108         if (choice < numThreads)
00109         nextState.waiting[choice] = false;
00110         break;
00111     case NOTIFYALL:
00112         nextState.waiting = (boolean []) waiting.clone();
00113         for (int i = 0; i < numThreads; i++)
00114         nextState.waiting[i] = false;
00115         break;
00116     default:
00117         throw new RuntimeException("Bad operation code");
00118     }
00119     return nextState;
00120     }
00121     public boolean queryState(int operation, BirThread thread) {
00122     switch (operation) {
00123     case LOCK_AVAILABLE: 
00124         return owner == null;
00125     case HAS_LOCK: 
00126         return owner == thread;
00127     case WAS_NOTIFIED: 
00128         return ! waiting[thread.getId()];
00129     default:
00130         throw new RuntimeException("Bad operation code");
00131     }
00132     }
00133     public String toString() {
00134     String result = (owner == null) ? "Lock:*" : "Lock:" + owner.getName();
00135     for (int i = 0; i < numThreads; i++)
00136         if (waiting[i])
00137         result += "," + i;
00138     return "<" + result + ">";
00139     }
00140 }

Generated at Thu Feb 7 06:49:57 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001