Mercurial > projects > dwt-mac
diff dwt/internal/Lock.d @ 58:6d9ec9ccdcdd
Updated Accessible to 3.514
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Tue, 09 Dec 2008 21:35:30 +0100 |
parents | d8635bb48c7c |
children |
line wrap: on
line diff
--- a/dwt/internal/Lock.d Mon Dec 08 22:02:10 2008 +0100 +++ b/dwt/internal/Lock.d Tue Dec 09 21:35:30 2008 +0100 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,7 +7,6 @@ * * Contributors: * IBM Corporation - initial API and implementation - * * Port to the D programming language: * Frank Benoit <benoit@tionex.de> *******************************************************************************/ @@ -16,75 +15,59 @@ import tango.core.Thread; import tango.core.sync.Mutex; import tango.core.sync.Condition; -import tango.core.Exception; - -import dwt.dwthelper.utils; /** - * Instances of this represent a recursive monitor. Note that this - * is an empty implementation which does not actually perform locking. + * Instance of this represent a recursive monitor. */ -public class Lock -{ +public class Lock { + int count, waitCount; + Thread owner; Mutex mutex; Condition cond; - public this () - { + public this() { mutex = new Mutex; cond = new Condition(mutex); } +/** + * Locks the monitor and returns the lock count. If + * the lock is owned by another thread, wait until + * the lock is released. + * + * @return the lock count + */ +public int lock() { + synchronized (mutex) { + Thread current = Thread.getThis(); + if (owner !is current) { + waitCount++; + while (count > 0) { + try { + cond.wait(); + } catch (SyncException e) { + /* Wait forever, just like synchronized blocks */ + } + } + --waitCount; + owner = current; + } + return ++count; + } +} - /** - * Locks the monitor and returns the lock count. If - * the lock is owned by another thread, wait until - * the lock is released. - * - * @return the lock count - */ - public int lock () - { - synchronized (mutex) - { - Thread current = Thread.getThis(); - if (owner !is current) - { - waitCount++; - while (count > 0) - { - try - { - cond.wait(); - } - catch (SyncException e) - { - } - } - --waitCount; - owner = current; - } - return ++count; - } - } - - /** - * Unlocks the monitor. If the current thread is not - * the monitor owner, do nothing. - */ - public void unlock () - { - synchronized (mutex) - { - Thread current = Thread.getThis(); - if (owner is current) - { - if (--count is 0) - { - owner = null; - if (waitCount > 0) - cond.notifyAll(); - } +/** + * Unlocks the monitor. If the current thread is not + * the monitor owner, do nothing. + */ +public void unlock() { + synchronized (mutex) { + Thread current = Thread.getThis(); + if (owner is current) { + if (--count is 0) { + owner = null; + if (waitCount > 0) cond.notifyAll(); } } } } +}