KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > Mutex


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import com.protomatter.syslog.Syslog;
54 import java.util.*;
55
56 /**
57  * A mutex.
58  */

59 public class Mutex
60 {
61   private MutexObject mutex = new MutexObject();
62   private MutexToken token = null;
63   private Thread JavaDoc currentThread = null;
64
65   /**
66    * Create a new mutex.
67    */

68   public Mutex()
69   {
70     super();
71   }
72
73   /**
74    * Obtain the lock on the mutex. This method blocks, and threads
75    * are stacked in the order they call this method.
76    */

77   public void getLock()
78   {
79     try
80     {
81       token = (MutexToken)mutex.checkout();
82       this.currentThread = Thread.currentThread();
83     }
84     catch (Exception JavaDoc x)
85     {
86       Syslog.log(this, x);
87       ; // this cannot happen the way the MutexObject is written.
88
// it's part of the generic object pooling stuff.
89
}
90   }
91
92   /**
93    * Determine if this mutex is currently locked by anyone.
94    */

95   public boolean isLocked()
96   {
97     return (this.currentThread != null);
98   }
99
100   /**
101    * Get a reference to the thread that currently has the lock.
102    * Returns null if nobody has the lock.
103    */

104   public Thread JavaDoc getLockingThread()
105   {
106     return this.currentThread;
107   }
108
109   /**
110    * Release the lock on the mutex. Users of this class should be
111    * nice about using this method -- don't call it unless you've
112    * already called <tt>getLock()</tt>, since this does not check
113    * to make sure the caller is really the lock owner.
114    */

115   public void releaseLock()
116   {
117     try
118     {
119       synchronized (mutex.getSyncObject())
120       {
121         mutex.checkin(token);
122         this.currentThread = null;
123       }
124     }
125     catch (Exception JavaDoc x)
126     {
127       Syslog.log(this, x);
128       ; // this cannot happen the way the MutexObject is written.
129
// it's part of the generic object pooling stuff.
130
}
131   }
132 }
133
Popular Tags