Index: trunk/src/VBox/Main/include/AudioAdapterImpl.h
===================================================================
--- trunk/src/VBox/Main/include/AudioAdapterImpl.h	(revision 25289)
+++ trunk/src/VBox/Main/include/AudioAdapterImpl.h	(revision 25310)
@@ -94,6 +94,6 @@
     HRESULT saveSettings(settings::AudioAdapter &data);
 
-    bool isModified() { AutoWriteLock alock (this); return mData.isBackedUp(); }
-    bool isReallyModified() { AutoWriteLock alock (this); return mData.hasActualChanges(); }
+    bool isModified() { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); return mData.isBackedUp(); }
+    bool isReallyModified() { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); return mData.hasActualChanges(); }
     bool rollback();
     void commit();
Index: trunk/src/VBox/Main/include/AutoLock.h
===================================================================
--- trunk/src/VBox/Main/include/AutoLock.h	(revision 25289)
+++ trunk/src/VBox/Main/include/AutoLock.h	(revision 25310)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2008 Sun Microsystems, Inc.
+ * Copyright (C) 2006-2009 Sun Microsystems, Inc.
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -25,4 +25,24 @@
 #include <iprt/types.h>
 
+#ifdef DEBUG
+# ifdef VBOX_WITH_LOCK_VALIDATOR
+#  define VBOX_WITH_DEBUG_LOCK_VALIDATOR
+# endif
+#endif
+
+#ifdef VBOX_WITH_DEBUG_LOCK_VALIDATOR
+# define COMMA_LOCKVAL_SRC_POS , RT_SRC_POS
+# define LOCKVAL_SRC_POS_DECL RT_SRC_POS_DECL
+# define COMMA_LOCKVAL_SRC_POS_DECL , RT_SRC_POS_DECL
+# define LOCKVAL_SRC_POS_ARGS RT_SRC_POS_ARGS
+# define COMMA_LOCKVAL_SRC_POS_ARGS , RT_SRC_POS_ARGS
+#else
+# define COMMA_LOCKVAL_SRC_POS
+# define LOCKVAL_SRC_POS_DECL
+# define COMMA_LOCKVAL_SRC_POS_DECL
+# define LOCKVAL_SRC_POS_ARGS
+# define COMMA_LOCKVAL_SRC_POS_ARGS
+#endif
+
 namespace util
 {
@@ -35,12 +55,5 @@
 
 /**
- * Abstract read/write semaphore handle.
- *
- * This is a base class to implement semaphores that provide read/write locking.
- * Subclasses must implement all pure virtual methods of this class together
- * with pure methods of ReadLockOps and WriteLockOps classes.
- *
- * See the AutoWriteLock class documentation for the detailed description of
- * read and write locks.
+ * Abstract base class for semaphore handles (RWLockHandle and WriteLockHandle).
  */
 class LockHandle
@@ -66,8 +79,15 @@
     virtual uint32_t writeLockLevel() const = 0;
 
-    virtual void lockWrite() = 0;
+#ifdef VBOX_WITH_DEBUG_LOCK_VALIDATOR
+    void validateLock(LOCKVAL_SRC_POS_DECL);
+    void validateUnlock();
+#endif
+
+    virtual void lockWrite(LOCKVAL_SRC_POS_DECL) = 0;
     virtual void unlockWrite() = 0;
-    virtual void lockRead() = 0;
+    virtual void lockRead(LOCKVAL_SRC_POS_DECL) = 0;
     virtual void unlockRead() = 0;
+
+    static RTTLS s_lockingStackTlsIndex;
 
 private:
@@ -93,7 +113,7 @@
     virtual bool isWriteLockOnCurrentThread() const;
 
-    virtual void lockWrite();
+    virtual void lockWrite(LOCKVAL_SRC_POS_DECL);
     virtual void unlockWrite();
-    virtual void lockRead();
+    virtual void lockRead(LOCKVAL_SRC_POS_DECL);
     virtual void unlockRead();
 
@@ -125,7 +145,7 @@
     virtual bool isWriteLockOnCurrentThread() const;
 
-    virtual void lockWrite();
+    virtual void lockWrite(LOCKVAL_SRC_POS_DECL);
     virtual void unlockWrite();
-    virtual void lockRead();
+    virtual void lockRead(LOCKVAL_SRC_POS_DECL);
     virtual void unlockRead();
     virtual uint32_t writeLockLevel() const;
@@ -193,6 +213,9 @@
 {
 protected:
-    AutoLockBase(uint32_t cHandles);
-    AutoLockBase(uint32_t cHandles, LockHandle *pHandle);
+    AutoLockBase(uint32_t cHandles
+                 COMMA_LOCKVAL_SRC_POS_DECL);
+    AutoLockBase(uint32_t cHandles,
+                 LockHandle *pHandle
+                 COMMA_LOCKVAL_SRC_POS_DECL);
     virtual ~AutoLockBase();
 
@@ -245,6 +268,8 @@
      * runtime.
      */
-    AutoReadLock()
-        : AutoLockBase(1, NULL)
+    AutoReadLock(LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(1,
+                       NULL
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     { }
 
@@ -253,6 +278,9 @@
      * semaphore by requesting a read lock.
      */
-    AutoReadLock(LockHandle *aHandle)
-        : AutoLockBase(1, aHandle)
+    AutoReadLock(LockHandle *aHandle
+                 COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(1,
+                       aHandle
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -263,6 +291,9 @@
      * semaphore by requesting a read lock.
      */
-    AutoReadLock(LockHandle &aHandle)
-        : AutoLockBase(1, &aHandle)
+    AutoReadLock(LockHandle &aHandle
+                 COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(1,
+                       &aHandle
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -273,6 +304,9 @@
      * semaphore by requesting a read lock.
      */
-    AutoReadLock(const Lockable &aLockable)
-        : AutoLockBase(1, aLockable.lockHandle())
+    AutoReadLock(const Lockable &aLockable
+                 COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(1,
+                       aLockable.lockHandle()
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -283,6 +317,9 @@
      * semaphore by requesting a read lock.
      */
-    AutoReadLock(const Lockable *aLockable)
-        : AutoLockBase(1, aLockable ? aLockable->lockHandle() : NULL)
+    AutoReadLock(const Lockable *aLockable
+                 COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(1,
+                       aLockable ? aLockable->lockHandle() : NULL
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -314,10 +351,16 @@
 {
 protected:
-    AutoWriteLockBase(uint32_t cHandles)
-        : AutoLockBase(cHandles)
+    AutoWriteLockBase(uint32_t cHandles
+                      COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(cHandles
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     { }
 
-    AutoWriteLockBase(uint32_t cHandles, LockHandle *pHandle)
-        : AutoLockBase(cHandles, pHandle)
+    AutoWriteLockBase(uint32_t cHandles,
+                      LockHandle *pHandle
+                      COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoLockBase(cHandles,
+                       pHandle
+                       COMMA_LOCKVAL_SRC_POS_ARGS)
     { }
 
@@ -366,6 +409,8 @@
      * runtime.
      */
-    AutoWriteLock()
-        : AutoWriteLockBase(1, NULL)
+    AutoWriteLock(LOCKVAL_SRC_POS_DECL)
+        : AutoWriteLockBase(1,
+                            NULL
+                            COMMA_LOCKVAL_SRC_POS_ARGS)
     { }
 
@@ -374,6 +419,9 @@
      * semaphore by requesting a write lock.
      */
-    AutoWriteLock(LockHandle *aHandle)
-        : AutoWriteLockBase(1, aHandle)
+    AutoWriteLock(LockHandle *aHandle
+                  COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoWriteLockBase(1,
+                            aHandle
+                            COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -384,6 +432,9 @@
      * semaphore by requesting a write lock.
      */
-    AutoWriteLock(LockHandle &aHandle)
-        : AutoWriteLockBase(1, &aHandle)
+    AutoWriteLock(LockHandle &aHandle
+                  COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoWriteLockBase(1,
+                            &aHandle
+                            COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -394,6 +445,9 @@
      * semaphore by requesting a write lock.
      */
-    AutoWriteLock(const Lockable &aLockable)
-        : AutoWriteLockBase(1, aLockable.lockHandle())
+    AutoWriteLock(const Lockable &aLockable
+                  COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoWriteLockBase(1,
+                            aLockable.lockHandle()
+                            COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -404,6 +458,9 @@
      * semaphore by requesting a write lock.
      */
-    AutoWriteLock(const Lockable *aLockable)
-        : AutoWriteLockBase(1, aLockable ? aLockable->lockHandle() : NULL)
+    AutoWriteLock(const Lockable *aLockable
+                  COMMA_LOCKVAL_SRC_POS_DECL)
+        : AutoWriteLockBase(1,
+                            aLockable ? aLockable->lockHandle() : NULL
+                            COMMA_LOCKVAL_SRC_POS_ARGS)
     {
         acquire();
@@ -461,6 +518,10 @@
 {
 public:
-    AutoMultiWriteLock2(Lockable *pl1, Lockable *pl2);
-    AutoMultiWriteLock2(LockHandle *pl1, LockHandle *pl2);
+    AutoMultiWriteLock2(Lockable *pl1,
+                        Lockable *pl2
+                        COMMA_LOCKVAL_SRC_POS_DECL);
+    AutoMultiWriteLock2(LockHandle *pl1,
+                        LockHandle *pl2
+                        COMMA_LOCKVAL_SRC_POS_DECL);
 
     virtual ~AutoMultiWriteLock2()
@@ -477,6 +538,12 @@
 {
 public:
-    AutoMultiWriteLock3(Lockable *pl1, Lockable *pl2, Lockable *pl3);
-    AutoMultiWriteLock3(LockHandle *pl1, LockHandle *pl2, LockHandle *pl3);
+    AutoMultiWriteLock3(Lockable *pl1,
+                        Lockable *pl2,
+                        Lockable *pl3
+                        COMMA_LOCKVAL_SRC_POS_DECL);
+    AutoMultiWriteLock3(LockHandle *pl1,
+                        LockHandle *pl2,
+                        LockHandle *pl3
+                        COMMA_LOCKVAL_SRC_POS_DECL);
 
     virtual ~AutoMultiWriteLock3()
Index: trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- trunk/src/VBox/Main/include/MachineImpl.h	(revision 25289)
+++ trunk/src/VBox/Main/include/MachineImpl.h	(revision 25310)
@@ -761,5 +761,5 @@
                             bool aSetError = false)
     {
-        AutoWriteLock alock(this);
+        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
         return findSharedFolder(aName, aSharedFolder, aSetError);
     }
Index: trunk/src/VBox/Main/include/NetworkAdapterImpl.h
===================================================================
--- trunk/src/VBox/Main/include/NetworkAdapterImpl.h	(revision 25289)
+++ trunk/src/VBox/Main/include/NetworkAdapterImpl.h	(revision 25310)
@@ -140,6 +140,6 @@
     HRESULT saveSettings(settings::NetworkAdapter &data);
 
-    bool isModified() { AutoWriteLock alock (this); return mData.isBackedUp(); }
-    bool isReallyModified() { AutoWriteLock alock (this); return mData.hasActualChanges(); }
+    bool isModified() { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); return mData.isBackedUp(); }
+    bool isReallyModified() { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); return mData.hasActualChanges(); }
     bool rollback();
     void commit();
Index: trunk/src/VBox/Main/include/Shareable.h
===================================================================
--- trunk/src/VBox/Main/include/Shareable.h	(revision 25289)
+++ trunk/src/VBox/Main/include/Shareable.h	(revision 25310)
@@ -106,8 +106,8 @@
  *      {
  *          // acquire the lock until the end of the block
- *          AutoWriteLock alock (mData);
+ *          AutoWriteLock alock(mData COMMA_LOCKVAL_SRC_POS);
  *          // share with another instance (thatData defined somewhere else)
  *          {
- *              AutoReadLock thatLock (thatData);
+ *              AutoReadLock thatLock(thatData COMMA_LOCKVAL_SRC_POS);
  *              mData = thatData;
  *          }
@@ -130,6 +130,6 @@
  *      {
  *          // read-only data access
- *          AutoReadLock lock (mData); // protect Shareable members (read-only)
- *          AutoReadLock dataLock (mData.data()); // protect Data members (read-only)
+ *          AutoReadLock lock(mData COMMA_LOCKVAL_SRC_POS); // protect Shareable members (read-only)
+ *          AutoReadLock dataLock(mData.data() COMMA_LOCKVAL_SRC_POS); // protect Data members (read-only)
  *          if (mData->mSomeVield) ...
  *      }
@@ -137,6 +137,6 @@
  *      {
  *          // data modification
- *          AutoReadLock lock (mData); // protect Shareable members (still read-only)
- *          AutoWriteLock dataLock (mData.data()); // protect Data members (exclusive)
+ *          AutoReadLock lock(mData COMMA_LOCKVAL_SRC_POS); // protect Shareable members (still read-only)
+ *          AutoWriteLock dataLock(mData.data() COMMA_LOCKVAL_SRC_POS); // protect Data members (exclusive)
  *          mData->mSomeVield = someValue;
  *      }
Index: trunk/src/VBox/Main/include/VRDPServerImpl.h
===================================================================
--- trunk/src/VBox/Main/include/VRDPServerImpl.h	(revision 25289)
+++ trunk/src/VBox/Main/include/VRDPServerImpl.h	(revision 25310)
@@ -113,6 +113,6 @@
     HRESULT saveSettings(settings::VRDPSettings &data);
 
-    bool isModified() { AutoWriteLock alock (this); return mData.isBackedUp(); }
-    bool isReallyModified() { AutoWriteLock alock (this); return mData.hasActualChanges(); }
+    bool isModified() { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); return mData.isBackedUp(); }
+    bool isReallyModified() { AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); return mData.hasActualChanges(); }
     bool rollback();
     void commit();
Index: trunk/src/VBox/Main/include/objectslist.h
===================================================================
--- trunk/src/VBox/Main/include/objectslist.h	(revision 25289)
+++ trunk/src/VBox/Main/include/objectslist.h	(revision 25310)
@@ -89,5 +89,5 @@
     void addChild(MyType p)
     {
-        AutoWriteLock al(m_lock);
+        AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
         m_ll.push_back(p);
     }
@@ -100,5 +100,5 @@
     void removeChild(MyType p)
     {
-        AutoWriteLock al(m_lock);
+        AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
         m_ll.remove(p);
     }
@@ -113,5 +113,5 @@
     void appendOtherList(ObjectsList<T> &ll)
     {
-        AutoReadLock alr(ll.getLockHandle());
+        AutoReadLock alr(ll.getLockHandle() COMMA_LOCKVAL_SRC_POS);
         for (const_iterator it = ll.begin();
              it != ll.end();
@@ -128,5 +128,5 @@
     void uninitAll()
     {
-        AutoWriteLock al(m_lock);
+        AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
         for (iterator it = m_ll.begin();
              it != m_ll.end();
@@ -145,5 +145,5 @@
     size_t size()
     {
-        AutoReadLock al(m_lock);
+        AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
         return m_ll.size();
     }
@@ -165,5 +165,5 @@
     MyType front()
     {
-        AutoReadLock al(m_lock);
+        AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
         return m_ll.front();
     }
