KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ThreadLock.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: ThreadLock.java,v 1.2 2004/09/09 10:51:15 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 package org.armedbear.lisp;
23
24 public final class ThreadLock extends LispObject
25 {
26     private LispThread thread;
27
28     private void lock() throws ConditionThrowable
29     {
30         LispThread currentThread = LispThread.currentThread();
31         if (!currentThread.equals(thread)) {
32             while (thread != null) {
33                 synchronized(this) {
34                     try {
35                         wait();
36                     } catch(InterruptedException JavaDoc e) {
37                         throw new RuntimeException JavaDoc(e);
38                     }
39                 }
40             }
41             thread = currentThread;
42         }
43     }
44
45     private void unlock() throws ConditionThrowable
46     {
47         if (thread.equals(LispThread.currentThread())) {
48             synchronized(this) {
49                 thread = null;
50                 notifyAll();
51             }
52         }
53     }
54
55     public String JavaDoc toString()
56     {
57         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#<THREAD-LOCK @ #x");
58         sb.append(Integer.toHexString(hashCode()));
59         sb.append(">");
60         return sb.toString();
61     }
62
63     // ### make-thread-lock
64
private static final Primitive0 MAKE_THREAD_LOCK =
65         new Primitive0("make-thread-lock", PACKAGE_EXT, true)
66     {
67         public LispObject execute() throws ConditionThrowable
68         {
69             return new ThreadLock();
70         }
71     };
72
73     // ### thread-lock lock
74
private static final Primitive1 THREAD_LOCK =
75         new Primitive1("thread-lock", PACKAGE_EXT, true)
76     {
77         public LispObject execute(LispObject arg) throws ConditionThrowable
78         {
79             ThreadLock threadLock = (ThreadLock) arg;
80             threadLock.lock();
81             return NIL;
82         }
83     };
84
85     // ### thread-unlock lock
86
private static final Primitive1 THREAD_UNLOCK =
87         new Primitive1("thread-unlock", PACKAGE_EXT, true)
88     {
89         public LispObject execute(LispObject arg) throws ConditionThrowable
90         {
91             ThreadLock threadLock = (ThreadLock) arg;
92             threadLock.unlock();
93             return NIL;
94         }
95     };
96 }
97
Popular Tags