Changeset 7627


Ignore:
Timestamp:
09/09/04 14:59:29 (17 years ago)
Author:
piso
Message:

Work in progress.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/org/armedbear/lisp/Mutex.java

    r7623 r7627  
    33 *
    44 * Copyright (C) 2004 Peter Graves
    5  * $Id: Mutex.java,v 1.1 2004-09-09 12:41:29 piso Exp $
     5 * $Id: Mutex.java,v 1.2 2004-09-09 14:59:29 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    2020 */
    2121
     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
    2235package org.armedbear.lisp;
    2336
    2437public final class Mutex extends LispObject
    2538{
    26     private Thread owner;
    27     private int count;
     39    private boolean inUse;
    2840
    29     private void acquire() throws ConditionThrowable, InterruptedException
     41    public void acquire() throws InterruptedException
    3042    {
    31         Thread currentThread = Thread.currentThread();
    32         if (currentThread == owner) {
    33             ++count;
    34         } else {
    35             synchronized (this) {
    36                 try {
    37                     while (owner != null)
    38                         wait();
    39                     owner = currentThread;
    40                     count = 1;
    41                 }
    42                 catch (InterruptedException e) {
    43                     notify();
    44                     throw e;
    45                 }
     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;
    4654            }
    4755        }
    4856    }
    4957
    50     private void release() throws ConditionThrowable
    51     {
    52         if (Thread.currentThread() != owner)
    53             signal(new LispError("Attempt to release mutex by a thread other than its current owner."));
    54         synchronized (this) {
    55             if (--count == 0) {
    56                 owner = null;
    57                 notify();
    58             }
    59         }
     58    public synchronized void release()  {
     59        inUse = false;
     60        notify();
    6061    }
     62
    6163
    6264    public String writeToString()
     
    108110            }
    109111            catch (ClassCastException e) {
    110                 return signal(new TypeError(arg.writeToString() + " is not a mutex."));
     112                return signal(new TypeError("The value " + arg.writeToString() +
     113                                            " is not a mutex."));
    111114            }
    112115        }
Note: See TracChangeset for help on using the changeset viewer.