[Pcsclite-cvs-commit] r6442 - /trunk/PCSC/src/winscard_svc.c

rousseau at users.alioth.debian.org rousseau at users.alioth.debian.org
Fri Aug 24 07:51:15 UTC 2012


Author: rousseau
Date: Fri Aug 24 07:51:14 2012
New Revision: 6442

URL: http://svn.debian.org/wsvn/pcsclite/?sc=1&rev=6442
Log:
CreateContextThread(): Use only one lock/unlock pair

It is quite often useless to lock a resource, perform calculation,
unlock the resource and then act conditionally upon the result of the
calculation, because as soon as shared resource is unlocked the
condition may change and the previous calculation will become obsolete.

Signed-off-by: Dmitry Torokhov <dtor at vmware.com>

Modified:
    trunk/PCSC/src/winscard_svc.c

Modified: trunk/PCSC/src/winscard_svc.c
URL: http://svn.debian.org/wsvn/pcsclite/trunk/PCSC/src/winscard_svc.c?rev=6442&op=diff
==============================================================================
--- trunk/PCSC/src/winscard_svc.c (original)
+++ trunk/PCSC/src/winscard_svc.c Fri Aug 24 07:51:14 2012
@@ -147,15 +147,15 @@
 	int lrv;
 	int listSize;
 	SCONTEXT * newContext = NULL;
+	LONG retval = SCARD_E_NO_MEMORY;
 
 	(void)pthread_mutex_lock(&contextsList_lock);
+
 	listSize = list_size(&contextsList);
-	(void)pthread_mutex_unlock(&contextsList_lock);
-
 	if (listSize >= contextMaxThreadCounter)
 	{
 		Log2(PCSC_LOG_CRITICAL, "Too many context running: %d", listSize);
-		goto error;
+		goto out;
 	}
 
 	/* Create the context for this thread. */
@@ -163,7 +163,7 @@
 	if (NULL == newContext)
 	{
 		Log1(PCSC_LOG_CRITICAL, "Could not allocate new context");
-		goto error;
+		goto out;
 	}
 	memset(newContext, 0, sizeof(*newContext));
 
@@ -174,7 +174,7 @@
 	if (lrv < 0)
 	{
 		Log2(PCSC_LOG_CRITICAL, "list_init failed with return value: %d", lrv);
-		goto error;
+		goto out;
 	}
 
 	/* request to store copies, and provide the metric function */
@@ -192,20 +192,18 @@
 		Log2(PCSC_LOG_CRITICAL,
 			"list_attributes_comparator failed with return value: %d", lrv);
 		list_destroy(&(newContext->cardsList));
-		goto error;
+		goto out;
 	}
 
 	(void)pthread_mutex_init(&newContext->cardsList_lock, NULL);
 
-	(void)pthread_mutex_lock(&contextsList_lock);
 	lrv = list_append(&contextsList, newContext);
-	(void)pthread_mutex_unlock(&contextsList_lock);
 	if (lrv < 0)
 	{
 		Log2(PCSC_LOG_CRITICAL, "list_append failed with return value: %d",
 			lrv);
 		list_destroy(&(newContext->cardsList));
-		goto error;
+		goto out;
 	}
 
 	rv = ThreadCreate(&(newContext->pthThread), THREAD_ATTR_DETACHED,
@@ -215,26 +213,30 @@
 		int lrv2;
 
 		Log2(PCSC_LOG_CRITICAL, "ThreadCreate failed: %s", strerror(rv));
-		(void)pthread_mutex_lock(&contextsList_lock);
 		lrv2 = list_delete(&contextsList, newContext);
-		(void)pthread_mutex_unlock(&contextsList_lock);
 		if (lrv2 < 0)
 			Log2(PCSC_LOG_CRITICAL, "list_delete failed with error %d", lrv2);
 		list_destroy(&(newContext->cardsList));
-		goto error;
+		goto out;
 	}
 
 	/* disable any suicide alarm */
 	if (AutoExit)
 		alarm(0);
 
-	return SCARD_S_SUCCESS;
-
-error:
-	if (newContext)
-		free(newContext);
-	(void)close(*pdwClientID);
-	return SCARD_E_NO_MEMORY;
+	retval = SCARD_S_SUCCESS;
+
+out:
+	(void)pthread_mutex_unlock(&contextsList_lock);
+
+	if (retval != SCARD_S_SUCCESS)
+	{
+		if (newContext)
+			free(newContext);
+		(void)close(*pdwClientID);
+	}
+
+	return retval;
 }
 
 /*




More information about the Pcsclite-cvs-commit mailing list