1 21 22 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 42 { 43 if (Thread.interrupted()) 44 throw new InterruptedException (); 45 synchronized (this) { 46 try { 47 while (inUse) 48 wait(); 49 inUse = true; 50 } 51 catch (InterruptedException 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 writeToString() 65 { 66 return unreadableString("MUTEX"); 67 } 68 69 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 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 e) { 90 return signal(new TypeError("The value " + arg.writeToString() + 91 " is not a mutex.")); 92 } 93 catch (InterruptedException e) { 94 return signal(new LispError( 95 "The thread " + LispThread.currentThread().writeToString() + 96 " was interrupted.")); 97 } 98 } 99 }; 100 101 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 e) { 112 return signal(new TypeError("The value " + arg.writeToString() + 113 " is not a mutex.")); 114 } 115 } 116 }; 117 } 118 | Popular Tags |