KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > synchro > MutexImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq, Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.synchro;
26
27 import org.objectweb.dream.AbstractComponent;
28 import org.objectweb.dream.util.EmptyStringArray;
29
30 /**
31  * Basic implementation of the {@link Mutex}interface. This implementation is
32  * unsafe, i.e. the identity of components acquiring/releasing the Mutex is
33  * unknown. As a consequence, the acquired mutex can be released by every
34  * component. <br/>Inspired by Doug Lea's implementation.
35  */

36 public class MutexImpl extends AbstractComponent implements Mutex
37 {
38
39   /** The lock status. */
40   protected boolean inuse = false;
41
42   //---------------------------------------------------------------------------
43
// Implementation of the Mutex interface.
44
// ---------------------------------------------------------------------------
45

46   /**
47    * @see Mutex#lock()
48    */

49   public void lock() throws InterruptedException JavaDoc
50   {
51     if (Thread.interrupted())
52       throw new InterruptedException JavaDoc();
53     synchronized (this)
54     {
55       try
56       {
57         while (inuse)
58           wait();
59         inuse = true;
60       }
61       catch (InterruptedException JavaDoc ex)
62       {
63         notify();
64         throw ex;
65       }
66     }
67   }
68
69   /**
70    * @see Mutex#timedLock(long)
71    */

72   public boolean timedLock(long msecs) throws InterruptedException JavaDoc
73   {
74     if (Thread.interrupted())
75       throw new InterruptedException JavaDoc();
76     synchronized (this)
77     {
78       if (!inuse)
79       {
80         inuse = true;
81         return true;
82       }
83       else if (msecs <= 0)
84         return false;
85       else
86       {
87         long waitTime = msecs;
88         long start = System.currentTimeMillis();
89         try
90         {
91           for (;;)
92           {
93             wait(waitTime);
94             if (!inuse)
95             {
96               inuse = true;
97               return true;
98             }
99             else
100             {
101               waitTime = msecs - (System.currentTimeMillis() - start);
102               if (waitTime <= 0)
103                 return false;
104             }
105           }
106         }
107         catch (InterruptedException JavaDoc ex)
108         {
109           notify();
110           throw ex;
111         }
112       }
113     }
114   }
115
116   /**
117    * @see Mutex#unlock()
118    */

119   public synchronized void unlock()
120   {
121     inuse = false;
122     notify();
123   }
124
125   // ---------------------------------------------------------------------------
126
// Implementation of the BindingController interface.
127
// ---------------------------------------------------------------------------
128

129   /**
130    * @see org.objectweb.fractal.api.control.BindingController#listFc()
131    */

132   public String JavaDoc[] listFc()
133   {
134     return EmptyStringArray.EMPTY_STRING_ARRAY;
135   }
136
137 }
Popular Tags