Index: trunk/src/VBox/Main/include/ConsoleImpl.h
===================================================================
--- trunk/src/VBox/Main/include/ConsoleImpl.h	(revision 48282)
+++ trunk/src/VBox/Main/include/ConsoleImpl.h	(revision 48297)
@@ -230,7 +230,4 @@
     HRESULT onlineMergeMedium(IMediumAttachment *aMediumAttachment,
                               ULONG aSourceIdx, ULONG aTargetIdx,
-                              IMedium *aSource, IMedium *aTarget,
-                              BOOL aMergeForward, IMedium *aParentForTarget,
-                              ComSafeArrayIn(IMedium *, aChildrenToReparent),
                               IProgress *aProgress);
     VMMDev *getVMMDev() { return m_pVMMDev; }
Index: trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- trunk/src/VBox/Main/include/MachineImpl.h	(revision 48282)
+++ trunk/src/VBox/Main/include/MachineImpl.h	(revision 48297)
@@ -1070,9 +1070,5 @@
                               IN_BSTR aEndID, BOOL fDeleteAllChildren,
                               MachineState_T *aMachineState, IProgress **aProgress);
-    STDMETHOD(FinishOnlineMergeMedium)(IMediumAttachment *aMediumAttachment,
-                                       IMedium *aSource, IMedium *aTarget,
-                                       BOOL fMergeForward,
-                                       IMedium *pParentForTarget,
-                                       ComSafeArrayIn(IMedium *, aChildrenToReparent));
+    STDMETHOD(FinishOnlineMergeMedium)();
     STDMETHOD(RestoreSnapshot)(IConsole *aInitiator,
                                ISnapshot *aSnapshot,
@@ -1143,5 +1139,5 @@
     {
         ConsoleTaskData()
-            : mLastState(MachineState_Null)
+            : mLastState(MachineState_Null), mDeleteSnapshotInfo(NULL)
         { }
 
@@ -1151,4 +1147,7 @@
         // used when taking snapshot
         ComObjPtr<Snapshot> mSnapshot;
+
+        // used when deleting online snapshot
+        void *mDeleteSnapshotInfo;
 
         // used when saving state (either as part of a snapshot or separate)
@@ -1185,12 +1184,14 @@
                                         bool &fMergeForward,
                                         ComObjPtr<Medium> &pParentForTarget,
-                                        MediaList &aChildrenToReparent,
+                                        MediumLockList * &aChildrenToReparent,
                                         bool &fNeedOnlineMerge,
-                                        MediumLockList * &aMediumLockList);
+                                        MediumLockList * &aMediumLockList,
+                                        ComPtr<IToken> &aHDLockToken);
     void cancelDeleteSnapshotMedium(const ComObjPtr<Medium> &aHD,
                                     const ComObjPtr<Medium> &aSource,
-                                    const MediaList &aChildrenToReparent,
+                                    MediumLockList *aChildrenToReparent,
                                     bool fNeedsOnlineMerge,
                                     MediumLockList *aMediumLockList,
+                                    const ComPtr<IToken> &aHDLockToken,
                                     const Guid &aMediumId,
                                     const Guid &aSnapshotId);
@@ -1200,5 +1201,5 @@
                               bool fMergeForward,
                               const ComObjPtr<Medium> &pParentForTarget,
-                              const MediaList &aChildrenToReparent,
+                              MediumLockList *aChildrenToReparent,
                               MediumLockList *aMediumLockList,
                               ComObjPtr<Progress> &aProgress,
Index: trunk/src/VBox/Main/include/MediumImpl.h
===================================================================
--- trunk/src/VBox/Main/include/MediumImpl.h	(revision 48282)
+++ trunk/src/VBox/Main/include/MediumImpl.h	(revision 48297)
@@ -132,8 +132,6 @@
     STDMETHOD(GetSnapshotIds)(IN_BSTR aMachineId,
                               ComSafeArrayOut(BSTR, aSnapshotIds));
-    STDMETHOD(LockRead)(MediumState_T *aState);
-    STDMETHOD(UnlockRead)(MediumState_T *aState);
-    STDMETHOD(LockWrite)(MediumState_T *aState);
-    STDMETHOD(UnlockWrite)(MediumState_T *aState);
+    STDMETHOD(LockRead)(IToken **aToken);
+    STDMETHOD(LockWrite)(IToken **aToken);
     STDMETHOD(Close)();
     STDMETHOD(GetProperty)(IN_BSTR aName, BSTR *aValue);
@@ -226,4 +224,6 @@
 
     HRESULT close(AutoCaller &autoCaller);
+    HRESULT unlockRead(MediumState_T *aState);
+    HRESULT unlockWrite(MediumState_T *aState);
     HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait);
     HRESULT markForDeletion();
@@ -241,17 +241,17 @@
                            bool &fMergeForward,
                            ComObjPtr<Medium> &pParentForTarget,
-                           MediaList &aChildrenToReparent,
+                           MediumLockList * &aChildrenToReparent,
                            MediumLockList * &aMediumLockList);
     HRESULT mergeTo(const ComObjPtr<Medium> &pTarget,
                     bool fMergeForward,
                     const ComObjPtr<Medium> &pParentForTarget,
-                    const MediaList &aChildrenToReparent,
+                    MediumLockList *aChildrenToReparent,
                     MediumLockList *aMediumLockList,
                     ComObjPtr<Progress> *aProgress,
                     bool aWait);
-    void cancelMergeTo(const MediaList &aChildrenToReparent,
+    void cancelMergeTo(MediumLockList *aChildrenToReparent,
                        MediumLockList *aMediumLockList);
 
-    HRESULT fixParentUuidOfChildren(const MediaList &childrenToReparent);
+    HRESULT fixParentUuidOfChildren(MediumLockList *pChildrenToReparent);
 
     HRESULT exportFile(const char *aFilename,
Index: trunk/src/VBox/Main/include/MediumLock.h
===================================================================
--- trunk/src/VBox/Main/include/MediumLock.h	(revision 48282)
+++ trunk/src/VBox/Main/include/MediumLock.h	(revision 48297)
@@ -7,5 +7,5 @@
 
 /*
- * Copyright (C) 2010-2012 Oracle Corporation
+ * Copyright (C) 2010-2013 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -90,9 +90,16 @@
 
     /**
+     * Check if this medium object has been locked by this MediumLock.
+     */
+    bool IsLocked() const;
+
+    /**
      * Acquire a medium lock.
      *
      * @return COM status code
-     */
-    HRESULT Lock();
+     * @param aIgnoreLockedMedia    If set ignore all media which is already
+     *                              locked in an incompatible way.
+     */
+    HRESULT Lock(bool aIgnoreLockedMedia = false);
 
     /**
@@ -105,4 +112,5 @@
 private:
     ComObjPtr<Medium> mMedium;
+    ComPtr<IToken> mToken;
     AutoCaller mMediumCaller;
     bool mLockWrite;
@@ -208,6 +216,12 @@
      *
      * @return COM status code
-     */
-    HRESULT Lock();
+     * @param aSkipOverLockedMedia  If set ignore all media which is already
+     *                              locked for reading or writing. For callers
+     *                              which need to know which medium objects
+     *                              have been locked by this lock list you
+     *                              can iterate over the list and check the
+     *                              MediumLock state.
+     */
+    HRESULT Lock(bool aSkipOverLockedMedia = false);
 
     /**
Index: trunk/src/VBox/Main/include/SessionImpl.h
===================================================================
--- trunk/src/VBox/Main/include/SessionImpl.h	(revision 48282)
+++ trunk/src/VBox/Main/include/SessionImpl.h	(revision 48297)
@@ -103,7 +103,4 @@
     STDMETHOD(OnlineMergeMedium)(IMediumAttachment *aMediumAttachment,
                                  ULONG aSourceIdx, ULONG aTargetIdx,
-                                 IMedium *aSource, IMedium *aTarget,
-                                 BOOL aMergeForward, IMedium *aParentForTarget,
-                                 ComSafeArrayIn(IMedium *, aChildrenToReparent),
                                  IProgress *aProgress);
     STDMETHOD(EnableVMMStatistics)(BOOL aEnable);
Index: trunk/src/VBox/Main/include/TokenImpl.h
===================================================================
--- trunk/src/VBox/Main/include/TokenImpl.h	(revision 48297)
+++ trunk/src/VBox/Main/include/TokenImpl.h	(revision 48297)
@@ -0,0 +1,107 @@
+/* $Id$ */
+
+/** @file
+ *
+ * Token COM class implementations: MachineToken and MediumLockToken
+ */
+
+/*
+ * Copyright (C) 2013 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+
+#ifndef TOKEN_IMPL_H_
+#define TOKEN_IMPL_H_
+
+#include "TokenWrap.h"
+#include "MachineImpl.h"
+
+
+/**
+ * The MachineToken class automates cleanup of a SessionMachine object.
+ */
+class ATL_NO_VTABLE MachineToken :
+    public TokenWrap
+{
+public:
+
+    DECLARE_EMPTY_CTOR_DTOR(MachineToken)
+
+    HRESULT FinalConstruct();
+    void FinalRelease();
+
+    // public initializer/uninitializer for internal purposes only
+    HRESULT init(const ComObjPtr<SessionMachine> &pSessionMachine);
+    void uninit();
+
+private:
+
+    // wrapped IToken methods
+    HRESULT abandon(AutoCaller &aAutoCaller);
+    HRESULT dummy();
+
+    // data
+    struct Data
+    {
+        Data()
+	{
+	}
+
+        ComObjPtr<SessionMachine> pSessionMachine;
+    };
+
+    Data m;
+};
+
+
+class Medium;
+
+/**
+ * The MediumLockToken class automates cleanup of a Medium lock.
+ */
+class ATL_NO_VTABLE MediumLockToken :
+    public TokenWrap
+{
+public:
+
+    DECLARE_EMPTY_CTOR_DTOR(MediumLockToken)
+
+    HRESULT FinalConstruct();
+    void FinalRelease();
+
+    // public initializer/uninitializer for internal purposes only
+    HRESULT init(const ComObjPtr<Medium> &pMedium, bool fWrite);
+    void uninit();
+
+private:
+
+    // wrapped IToken methods
+    HRESULT abandon(AutoCaller &aAutoCaller);
+    HRESULT dummy();
+
+    // data
+    struct Data
+    {
+        Data() :
+            fWrite(false)
+        {
+        }
+
+        ComObjPtr<Medium> pMedium;
+        bool fWrite;
+    };
+
+    Data m;
+};
+
+
+#endif // TOKEN_IMPL_H_
+
+/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Index: trunk/src/VBox/Main/include/VirtualBoxImpl.h
===================================================================
--- trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 48282)
+++ trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 48297)
@@ -311,4 +311,5 @@
     AutostartDb* getAutostartDb() const;
 
+    RWLockHandle& getMachinesListLockHandle();
     RWLockHandle& getMediaTreeLockHandle();
 
