1 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 e) { 37 throw new RuntimeException (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 toString() 56 { 57 StringBuffer sb = new StringBuffer ("#<THREAD-LOCK @ #x"); 58 sb.append(Integer.toHexString(hashCode())); 59 sb.append(">"); 60 return sb.toString(); 61 } 62 63 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 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 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 |