Index: trunk/src/VBox/Main/src-server/darwin/USBProxyServiceDarwin.cpp
===================================================================
--- trunk/src/VBox/Main/src-server/darwin/USBProxyServiceDarwin.cpp	(revision 40845)
+++ trunk/src/VBox/Main/src-server/darwin/USBProxyServiceDarwin.cpp	(revision 41528)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2010 Oracle Corporation
+ * Copyright (C) 2005-2012 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -39,6 +39,6 @@
  * Initialize data members.
  */
-USBProxyServiceDarwin::USBProxyServiceDarwin (Host *aHost)
-    : USBProxyService (aHost), mServiceRunLoopRef (NULL), mNotifyOpaque (NULL), mWaitABitNextTime (false), mUSBLibInitialized (false)
+USBProxyServiceDarwin::USBProxyServiceDarwin(Host *aHost)
+    : USBProxyService(aHost), mServiceRunLoopRef(NULL), mNotifyOpaque(NULL), mWaitABitNextTime(false), mUSBLibInitialized(false)
 {
     LogFlowThisFunc(("aHost=%p\n", aHost));
@@ -101,13 +101,13 @@
 
 #ifdef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
-void *USBProxyServiceDarwin::insertFilter (PCUSBFILTER aFilter)
-{
-    return USBLibAddFilter (aFilter);
-}
-
-
-void USBProxyServiceDarwin::removeFilter (void *aId)
-{
-    USBLibRemoveFilter (aId);
+void *USBProxyServiceDarwin::insertFilter(PCUSBFILTER aFilter)
+{
+    return USBLibAddFilter(aFilter);
+}
+
+
+void USBProxyServiceDarwin::removeFilter(void *aId)
+{
+    USBLibRemoveFilter(aId);
 }
 #endif /* VBOX_WITH_NEW_USB_CODE_ON_DARWIN */
@@ -120,6 +120,9 @@
      */
     AssertReturn(aDevice, VERR_GENERAL_FAILURE);
+    AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
+
+    AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
     LogFlowThisFunc(("aDevice=%s\n", aDevice->getName().c_str()));
-    AssertReturn(aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
+
     Assert(aDevice->getUnistate() == kHostUSBDeviceState_Capturing);
 
@@ -129,4 +132,5 @@
      */
     ASMAtomicWriteBool(&mFakeAsync, true);
+    devLock.release();
     interruptWait();
     return VINF_SUCCESS;
@@ -161,4 +165,5 @@
 void USBProxyServiceDarwin::captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
 {
+    AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
 #ifdef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
     /*
@@ -179,6 +184,9 @@
      */
     AssertReturn(aDevice, VERR_GENERAL_FAILURE);
+    AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
+
+    AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
     LogFlowThisFunc(("aDevice=%s\n", aDevice->getName().c_str()));
-    AssertReturn(aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
+
     Assert(aDevice->getUnistate() == kHostUSBDeviceState_ReleasingToHost);
 
@@ -188,4 +196,5 @@
      */
     ASMAtomicWriteBool(&mFakeAsync, true);
+    devLock.release();
     interruptWait();
     return VINF_SUCCESS;
@@ -222,4 +231,5 @@
 void USBProxyServiceDarwin::releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
 {
+    AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
 #ifdef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
     /*
@@ -234,8 +244,9 @@
 
 
+/** @todo unused */
 void USBProxyServiceDarwin::detachingDevice(HostUSBDevice *aDevice)
 {
 #ifndef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
-    aDevice->setLogicalReconnect (HostUSBDevice::kDetachingPendingDetach);
+    aDevice->setLogicalReconnect(HostUSBDevice::kDetachingPendingDetach);
 #else
     NOREF(aDevice);
@@ -246,4 +257,6 @@
 bool USBProxyServiceDarwin::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
 {
+    AssertReturn(aDevice, false);
+    AssertReturn(!aDevice->isWriteLockOnCurrentThread(), false);
 #ifndef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
     /* We're faking async state stuff. */
@@ -264,5 +277,5 @@
 #endif
 
-    SInt32 rc = CFRunLoopRunInMode(CFSTR (VBOX_IOKIT_MODE_STRING),
+    SInt32 rc = CFRunLoopRunInMode(CFSTR(VBOX_IOKIT_MODE_STRING),
                                    mWaitABitNextTime && aMillies >= 1000
                                    ? 1.0 /* seconds */
@@ -277,13 +290,13 @@
 
 
-int USBProxyServiceDarwin::interruptWait (void)
+int USBProxyServiceDarwin::interruptWait(void)
 {
     if (mServiceRunLoopRef)
-        CFRunLoopStop (mServiceRunLoopRef);
+        CFRunLoopStop(mServiceRunLoopRef);
     return 0;
 }
 
 
-PUSBDEVICE USBProxyServiceDarwin::getDevices (void)
+PUSBDEVICE USBProxyServiceDarwin::getDevices(void)
 {
     /* call iokit.cpp */
@@ -292,5 +305,5 @@
 
 
-void USBProxyServiceDarwin::serviceThreadInit (void)
+void USBProxyServiceDarwin::serviceThreadInit(void)
 {
     mServiceRunLoopRef = CFRunLoopGetCurrent();
@@ -299,7 +312,7 @@
 
 
-void USBProxyServiceDarwin::serviceThreadTerm (void)
-{
-    DarwinUnsubscribeUSBNotifications (mNotifyOpaque);
+void USBProxyServiceDarwin::serviceThreadTerm(void)
+{
+    DarwinUnsubscribeUSBNotifications(mNotifyOpaque);
     mServiceRunLoopRef = NULL;
 }
@@ -311,7 +324,7 @@
  * @param   pCur    The USB device to free.
  */
-void DarwinFreeUSBDeviceFromIOKit (PUSBDEVICE pCur)
-{
-    USBProxyService::freeDevice (pCur);
-}
-
+void DarwinFreeUSBDeviceFromIOKit(PUSBDEVICE pCur)
+{
+    USBProxyService::freeDevice(pCur);
+}
+
