KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > Mutex


1 /*
2  * Mutex.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: Mutex.java,v 1.2 2004/09/09 14:59:29 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 /*
23   File: Mutex.java
24
25   Originally written by Doug Lea and released into the public domain.
26   This may be used for any purposes whatsoever without acknowledgment.
27   Thanks for the assistance and support of Sun Microsystems Labs,
28   and everyone contributing, testing, and using this code.
29
30   History:
31   Date Who What
32   11Jun1998 dl Create public version
33 */

34
35 package org.armedbear.lisp;
36
37 public final class Mutex extends LispObject
38 {
39     private boolean inUse;
40
41     public void acquire() throws InterruptedException JavaDoc
42     {
43         if (Thread.interrupted())
44             throw new InterruptedException JavaDoc();
45         synchronized (this) {
46             try {
47                 while (inUse)
48                     wait();
49                 inUse = true;
50             }
51             catch (InterruptedException JavaDoc e) {
52                 notify();
53                 throw e;
54             }
55         }
56     }
57
58     public synchronized void release() {
59         inUse = false;
60         notify();
61     }
62
63
64     public String JavaDoc writeToString()
65     {
66         return unreadableString("MUTEX");
67     }
68
69     // ### make-mutex => mutex
70
private static final Primitive MAKE_MUTEX =
71         new Primitive("make-mutex", PACKAGE_EXT, true, "")
72     {
73         public LispObject execute() throws ConditionThrowable
74         {
75             return new Mutex();
76         }
77     };
78
79     // ### get-mutex mutex => generalized-boolean
80
private static final Primitive GET_MUTEX =
81         new Primitive("get-mutex", PACKAGE_EXT, true, "mutex")
82     {
83         public LispObject execute(LispObject arg) throws ConditionThrowable
84         {
85             try {
86                 ((Mutex)arg).acquire();
87                 return T;
88             }
89             catch (ClassCastException JavaDoc e) {
90                 return signal(new TypeError("The value " + arg.writeToString() +
91                                             " is not a mutex."));
92             }
93             catch (InterruptedException JavaDoc e) {
94                 return signal(new LispError(
95                     "The thread " + LispThread.currentThread().writeToString() +
96                     " was interrupted."));
97             }
98         }
99     };
100
101     // ### release-mutex mutex
102
private static final Primitive RELEASE_MUTEX =
103         new Primitive("release-mutex", PACKAGE_EXT, true, "mutex")
104     {
105         public LispObject execute(LispObject arg) throws ConditionThrowable
106         {
107             try {
108                 ((Mutex)arg).release();
109                 return T;
110             }
111             catch (ClassCastException JavaDoc e) {
112                 return signal(new TypeError("The value " + arg.writeToString() +
113                                             " is not a mutex."));
114             }
115         }
116     };
117 }
118
Popular Tags