Bug 647003 - Remove use of nsAutoLock from LDAP SDK code r=standard8 f=neil
authorIan Neal <iann_cvs@blueyonder.co.uk>
Wed, 06 Apr 2011 22:16:28 +0100
changeset 7513 000c04a87624ce94e1efbb9ee22c4a1184f2bb14
parent 7512 c831c107de255938939497b8b70e07da333410f7
child 7514 85be50c0b00f108108926331c1a9db77e901f530
push idunknown
push userunknown
push dateunknown
reviewersstandard8
bugs647003
Bug 647003 - Remove use of nsAutoLock from LDAP SDK code r=standard8 f=neil
ldap/xpcom/src/nsLDAPModification.cpp
ldap/xpcom/src/nsLDAPModification.h
ldap/xpcom/src/nsLDAPService.cpp
ldap/xpcom/src/nsLDAPService.h
--- a/ldap/xpcom/src/nsLDAPModification.cpp
+++ b/ldap/xpcom/src/nsLDAPModification.cpp
@@ -33,49 +33,40 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsLDAPModification.h"
-#include "nsAutoLock.h"
 #include "nsILDAPBERValue.h"
 #include "nsISimpleEnumerator.h"
 #include "nsServiceManagerUtils.h"
 
+using namespace mozilla;
+
 NS_IMPL_THREADSAFE_ISUPPORTS1(nsLDAPModification, nsILDAPModification)
 
 // constructor
 //
-nsLDAPModification::nsLDAPModification() : mValuesLock(nsnull)
+nsLDAPModification::nsLDAPModification()
+    : mValuesLock("nsLDAPModification.mValuesLock")
 {
 }
 
 // destructor
 //
 nsLDAPModification::~nsLDAPModification()
 {
-  if (mValuesLock) {
-    PR_DestroyLock(mValuesLock);
-  }
 }
 
 nsresult
 nsLDAPModification::Init()
 {
-  if (!mValuesLock) {
-    mValuesLock = PR_NewLock();
-    if (!mValuesLock) {
-      NS_ERROR("nsLDAPModification::Init: out of memory");
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
-  }
-
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLDAPModification::GetOperation(PRInt32 *aOperation)
 {
   NS_ENSURE_ARG_POINTER(aOperation);
 
@@ -103,32 +94,32 @@ nsLDAPModification::SetType(const nsACSt
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLDAPModification::GetValues(nsIArray** aResult)
 {
   NS_ENSURE_ARG_POINTER(aResult);
 
-  nsAutoLock lock(mValuesLock);
+  MutexAutoLock lock(mValuesLock);
 
   if (!mValues)
     return NS_ERROR_NOT_INITIALIZED;
 
   NS_ADDREF(*aResult = mValues);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLDAPModification::SetValues(nsIArray* aValues)
 {
   NS_ENSURE_ARG_POINTER(aValues);
 
-  nsAutoLock lock(mValuesLock);
+  MutexAutoLock lock(mValuesLock);
   nsresult rv;
 
   if (!mValues)
     mValues = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
   else
     rv = mValues->Clear();
 
   NS_ENSURE_SUCCESS(rv, rv);
@@ -162,32 +153,32 @@ NS_IMETHODIMP
 nsLDAPModification::SetUpModification(PRInt32 aOperation,
                                       const nsACString &aType,
                                       nsIArray *aValues)
 {
   // Set the values using our local function before entering lock
   // to avoid deadlocks due to holding the same lock twice.
   nsresult rv = SetValues(aValues);
 
-  nsAutoLock lock(mValuesLock);
+  MutexAutoLock lock(mValuesLock);
 
   mOperation = aOperation;
   mType.Assign(aType);
 
   return rv;
 }
 
 NS_IMETHODIMP
 nsLDAPModification::SetUpModificationOneValue(PRInt32 aOperation,
                                               const nsACString &aType,
                                               nsILDAPBERValue *aValue)
 {
   NS_ENSURE_ARG_POINTER(aValue);
 
-  nsAutoLock lock(mValuesLock);
+  MutexAutoLock lock(mValuesLock);
 
   mOperation = aOperation;
   mType.Assign(aType);
 
   nsresult rv;
 
   if (!mValues)
     mValues = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
--- a/ldap/xpcom/src/nsLDAPModification.h
+++ b/ldap/xpcom/src/nsLDAPModification.h
@@ -38,16 +38,17 @@
 
 #ifndef _nsLDAPModification_h_
 #define _nsLDAPModification_h_
 
 #include "nsILDAPModification.h"
 #include "nsIMutableArray.h"
 #include "nsString.h"
 #include "nsCOMPtr.h"
+#include "mozilla/Mutex.h"
 
 // 5b0f4d00-062e-11d6-a7f2-fc943c3c039c
 //
 #define NS_LDAPMODIFICATION_CID \
 { 0x5b0f4d00, 0x062e, 0x11d6, \
   { 0xa7, 0xf2, 0xfc, 0x94, 0x3c, 0x3c, 0x03, 0x9c }}
 
 class nsLDAPModification : public nsILDAPModification
@@ -62,12 +63,12 @@ public:
   virtual ~nsLDAPModification();
 
   nsresult Init();
 
 private:
   PRInt32 mOperation;
   nsCString mType;
   nsCOMPtr<nsIMutableArray> mValues;
-  PRLock* mValuesLock;
+  mozilla::Mutex mValuesLock;
 };
 
 #endif // _nsLDAPModification_h_
--- a/ldap/xpcom/src/nsLDAPService.cpp
+++ b/ldap/xpcom/src/nsLDAPService.cpp
@@ -42,20 +42,21 @@
 #include "nsLDAPService.h"
 #include "nsLDAPConnection.h"
 #include "nsLDAPOperation.h"
 #include "nsXPIDLString.h"
 #include "nsReadableUtils.h"
 #include "nsIServiceManager.h"
 #include "nsIConsoleService.h"
 #include "nsILDAPURL.h"
-#include "nsAutoLock.h"
 #include "nsCRT.h"
 #include "nsILDAPErrors.h"
 
+using namespace mozilla;
+
 // Constants for CIDs used here.
 //
 static NS_DEFINE_CID(kLDAPConnectionCID, NS_LDAPCONNECTION_CID);
 static NS_DEFINE_CID(kLDAPOperationCID, NS_LDAPOPERATION_CID);
 
 // First we provide all the methods for the "local" class
 // nsLDAPServiceEntry.
 //
@@ -222,17 +223,17 @@ PRBool nsLDAPServiceEntry::DeleteEntry()
 NS_IMPL_THREADSAFE_ISUPPORTS2(nsLDAPService,
                               nsILDAPService,
                               nsILDAPMessageListener)
 
 
 // constructor
 //
 nsLDAPService::nsLDAPService()
-    : mLock(0),
+    : mLock("nsLDAPService.mLock"),
       mServers(0),
       mConnections(0)
 {
 }
 
 // destructor
 //
 nsLDAPService::~nsLDAPService()
@@ -241,35 +242,22 @@ nsLDAPService::~nsLDAPService()
     if (mServers) {
         delete mServers;
     }
 
     // Delete the hash holding the "reverse" lookups from conn to server
     if (mConnections) {
         delete mConnections;
     }
-
-    // Delete the lock object
-    if (mLock) {
-        PR_DestroyLock(mLock);
-    }
 }
 
 // Initializer, create some internal hash tables etc.
 //
 nsresult nsLDAPService::Init()
 {
-    if (!mLock) {
-        mLock = PR_NewLock();
-        if (!mLock) {
-            NS_ERROR("nsLDAPService::Init: out of memory ");
-            return NS_ERROR_OUT_OF_MEMORY;
-        }
-    }
-
     if (!mServers) {
         mServers = new nsHashtable(16, PR_FALSE);
         if (!mServers) {
             NS_ERROR("nsLDAPService::Init: out of memory ");
             return NS_ERROR_OUT_OF_MEMORY;
         }
     }
 
@@ -331,17 +319,17 @@ NS_IMETHODIMP nsLDAPService::AddServer(n
     }
 
     // We increment the refcount here for the server entry, when
     // we purge a server completely from the service (TBD), we
     // need to decrement the counter as well.
     //
     {
         nsStringKey hashKey(key);
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
 
         if (mServers->Exists(&hashKey)) {
             // Collision detected, lets just throw away this service entry
             // and keep the old one.
             //
             delete entry;
             return NS_ERROR_FAILURE;
         }
@@ -352,17 +340,17 @@ NS_IMETHODIMP nsLDAPService::AddServer(n
     return NS_OK;
 }
 
 // void deleteServer (in wstring aKey);
 NS_IMETHODIMP nsLDAPService::DeleteServer(const PRUnichar *aKey)
 {
     nsLDAPServiceEntry *entry;
     nsStringKey hashKey(aKey, -1, nsStringKey::NEVER_OWN);
-    nsAutoLock lock(mLock);
+    MutexAutoLock lock(mLock);
         
     // We should probably rename the key for this entry now that it's
     // "deleted", so that we can add in a new one with the same ID.
     // This is bug #77669.
     //
     entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
     if (entry) {
         if (entry->GetLeases() > 0) {
@@ -379,17 +367,17 @@ NS_IMETHODIMP nsLDAPService::DeleteServe
 }
 
 // nsILDAPServer getServer (in wstring aKey);
 NS_IMETHODIMP nsLDAPService::GetServer(const PRUnichar *aKey,
                                        nsILDAPServer **_retval)
 {
     nsLDAPServiceEntry *entry;
     nsStringKey hashKey(aKey, -1, nsStringKey::NEVER_OWN);
-    nsAutoLock lock(mLock);
+    MutexAutoLock lock(mLock);
 
     if (!_retval) {
         NS_ERROR("nsLDAPService::GetServer: null pointer ");
         return NS_ERROR_NULL_POINTER;
     }
 
     entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
     if (!entry) {
@@ -417,17 +405,17 @@ NS_IMETHODIMP nsLDAPService::RequestConn
     if (!aListener) {
         NS_ERROR("nsLDAPService::RequestConection: null pointer ");
         return NS_ERROR_NULL_POINTER;
     }
 
     // Try to find a possibly cached connection and LDAP message.
     //
     {
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
 
         entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
         if (!entry) {
             return NS_ERROR_FAILURE;
         }
         entry->SetTimestamp();
 
         conn = entry->GetConnection();
@@ -450,17 +438,17 @@ NS_IMETHODIMP nsLDAPService::RequestConn
         }
 
     }
 
     // We got a new connection, now push the listeners on our stack,
     // until we get the LDAP message back.
     //
     {
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
             
         entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
         if (!entry ||
             !entry->PushListener(static_cast<nsILDAPMessageListener *>
                                             (aListener))) {
             return NS_ERROR_FAILURE;
         }
     }
@@ -469,17 +457,17 @@ NS_IMETHODIMP nsLDAPService::RequestConn
 }
 
 // nsILDAPConnection getConnection (in wstring aKey);
 NS_IMETHODIMP nsLDAPService::GetConnection(const PRUnichar *aKey,
                                            nsILDAPConnection **_retval)
 {
     nsLDAPServiceEntry *entry;
     nsStringKey hashKey(aKey, -1, nsStringKey::NEVER_OWN);
-    nsAutoLock lock(mLock);
+    MutexAutoLock lock(mLock);
 
     if (!_retval) {
         NS_ERROR("nsLDAPService::GetConnection: null pointer ");
         return NS_ERROR_NULL_POINTER;
     }
 
     entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
     if (!entry) {
@@ -495,17 +483,17 @@ NS_IMETHODIMP nsLDAPService::GetConnecti
     return NS_OK;
 }
 
 // void releaseConnection (in wstring aKey);
 NS_IMETHODIMP nsLDAPService::ReleaseConnection(const PRUnichar *aKey)
 {
     nsLDAPServiceEntry *entry;
     nsStringKey hashKey(aKey, -1, nsStringKey::NEVER_OWN);
-    nsAutoLock lock(mLock);
+    MutexAutoLock lock(mLock);
 
     entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
     if (!entry) {
         return NS_ERROR_FAILURE;
     }
 
     if (entry->GetLeases() > 0) {
         entry->SetTimestamp();
@@ -528,17 +516,17 @@ NS_IMETHODIMP nsLDAPService::ReconnectCo
     nsStringKey hashKey(aKey, -1, nsStringKey::NEVER_OWN);
 
     if (!aListener) {
         NS_ERROR("nsLDAPService::ReconnectConnection: null pointer ");
         return NS_ERROR_NULL_POINTER;
     }
 
     {
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
         
         entry = static_cast<nsLDAPServiceEntry *>(mServers->Get(&hashKey));
         if (!entry) {
             return NS_ERROR_FAILURE;
         }
         entry->SetTimestamp();
 
         if (entry->IsRebinding()) {
@@ -563,17 +551,17 @@ NS_IMETHODIMP nsLDAPService::ReconnectCo
     }
 
     rv = EstablishConnection(entry, aListener);
     if (NS_FAILED(rv)) {
         return rv;
     }
 
     {
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
         
         if (!entry->PushListener(static_cast<nsILDAPMessageListener *>
                                             (aListener))) {
             entry->SetRebinding(PR_FALSE);
             return NS_ERROR_FAILURE;
         }
     }
 
@@ -630,17 +618,17 @@ nsLDAPService::OnLDAPMessage(nsILDAPMess
         // server entry in the Service.
         //
         {
             nsCOMPtr<nsILDAPMessageListener> listener;
             nsCOMPtr<nsILDAPMessage> message;
             nsLDAPServiceEntry *entry;
             nsVoidKey connKey(static_cast<nsILDAPConnection *>
                                          (connection));
-            nsAutoLock lock(mLock);
+            MutexAutoLock lock(mLock);
 
             entry = static_cast<nsLDAPServiceEntry *>
                                (mConnections->Get(&connKey));
             if (!entry) {
                 return NS_ERROR_FAILURE;
             }
 
             message = entry->GetMessage();
@@ -653,19 +641,18 @@ nsLDAPService::OnLDAPMessage(nsILDAPMess
             entry->SetRebinding(PR_FALSE);
             entry->SetMessage(aMessage);
 
             // Now process all the pending callbacks/listeners. We
             // have to make sure to unlock before calling a listener,
             // since it's likely to call back into us again.
             //
             while (listener = entry->PopListener()) {
-                lock.unlock();
+                MutexAutoUnlock unlock(mLock);
                 listener->OnLDAPMessage(aMessage);
-                lock.lock();
             }
         }
         break;
 
     default:
         NS_WARNING("nsLDAPService::OnLDAPMessage(): unexpected LDAP message "
                    "received");
 
@@ -768,50 +755,50 @@ nsLDAPService::EstablishConnection(nsLDA
 
     // Try to detect a collision, i.e. someone established a connection
     // just before we did. If so, we drop ours. This code is somewhat
     // similar to what happens in RequestConnection(), i.e. we try to
     // call the listener directly if possible, and if not, push it on
     // the stack of pending requests.
     //
     {
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
 
         conn2 = aEntry->GetConnection();
         message = aEntry->GetMessage();
     }
 
     if (conn2) {
         // Drop the new connection, we can't use it.
         //
         conn = 0;
         if (message) {
             aListener->OnLDAPMessage(message);
             return NS_OK;
         }
 
         {
-            nsAutoLock lock(mLock);
+            MutexAutoLock lock(mLock);
 
             if (!aEntry->PushListener(static_cast<nsILDAPMessageListener *>
                                                  (aListener))) {
                 return NS_ERROR_FAILURE;
             }
         }
 
         return NS_OK;
     }
 
     // We made the connection, lets store it to the server entry,
     // and also update the reverse lookup tables (for finding the
     // server entry related to a particular connection).
     //
     {
         nsVoidKey connKey(static_cast<nsILDAPConnection *>(conn));
-        nsAutoLock lock(mLock);
+        MutexAutoLock lock(mLock);
 
         aEntry->SetConnection(conn);
         mConnections->Put(&connKey, aEntry);
     }
 
     // Setup the bind() operation.
     //
     operation = do_CreateInstance(kLDAPOperationCID, &rv);
--- a/ldap/xpcom/src/nsLDAPService.h
+++ b/ldap/xpcom/src/nsLDAPService.h
@@ -43,17 +43,17 @@
 #include "nsHashtable.h"
 #include "nsILDAPService.h"
 #include "nsILDAPMessage.h"
 #include "nsILDAPMessageListener.h"
 #include "nsCOMPtr.h"
 #include "nsILDAPServer.h"
 #include "nsILDAPConnection.h"
 #include "nsILDAPMessage.h"
-
+#include "mozilla/Mutex.h"
 
 // 6a89ae33-7a90-430d-888c-0dede53a951a 
 //
 #define NS_LDAPSERVICE_CID \
 { \
   0x6a89ae33, 0x7a90, 0x430d, \
   {0x88, 0x8c, 0x0d, 0xed, 0xe5, 0x3a, 0x95, 0x1a} \
 }
@@ -137,13 +137,13 @@ class nsLDAPService : public nsILDAPServ
     // count how many tokens are in this string; for use by
     // createFilter; note that unlike with NextToken, these params
     // are copies, not references.
     //
     PRUint32 CountTokens(nsReadingIterator<char> aIter,
                          nsReadingIterator<char> aIterEnd);
                    
     
-    PRLock *mLock;              // Lock mechanism
+    mozilla::Mutex mLock;       // Lock mechanism
     nsHashtable *mServers;      // Hash table holding server entries
     nsHashtable *mConnections;  // Hash table holding "reverse"
                                 // lookups from connection to server
 };