1 package org.jacorb.imr; 2 3 23 24 25 35 36 public class ResourceLock 37 implements java.io.Serializable 38 { 39 private int shared; 40 private int exclusive; 41 private boolean exclusives_waiting = false; 42 43 46 47 public ResourceLock() 48 { 49 shared = 0; 50 exclusive = 0; 51 } 52 53 57 58 public synchronized void gainSharedLock() 59 { 60 while(exclusive > 0 && exclusives_waiting) 61 { 62 try 63 { 64 wait(); 65 } 66 catch (java.lang.Exception _e) 67 { 68 } 69 } 70 shared++; 71 } 72 73 77 78 public synchronized void releaseSharedLock() 79 { 80 if (--shared == 0) 81 notifyAll(); 82 } 83 84 88 89 public synchronized void gainExclusiveLock() 90 { 91 while(shared > 0 || exclusive > 0) 92 { 93 try 94 { 95 exclusives_waiting = true; 96 wait(); 97 } 98 catch (java.lang.Exception _e) 99 { 100 } 101 } 102 exclusive++; 103 exclusives_waiting = false; 104 } 105 106 110 111 public synchronized void releaseExclusiveLock() 112 { 113 if (--exclusive == 0) 114 notifyAll(); 115 } 116 117 } 119 120 121 | Popular Tags |