From 894ec8c4aacc6b712b468bc86f5aa82e22e10453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Thu, 8 May 2014 15:24:33 -0400 Subject: [PATCH] channels/smartcard: cleanup --- channels/smartcard/client/smartcard_main.c | 67 +++++++--------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/channels/smartcard/client/smartcard_main.c b/channels/smartcard/client/smartcard_main.c index 1ce610982..fe01b9377 100644 --- a/channels/smartcard/client/smartcard_main.c +++ b/channels/smartcard/client/smartcard_main.c @@ -35,8 +35,6 @@ #include "smartcard_main.h" -#define DEBUG_SMARTCARD_INIT 1 - static void smartcard_free(DEVICE* device) { SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) device; @@ -63,7 +61,6 @@ static void smartcard_free(DEVICE* device) static void smartcard_init(DEVICE* device) { - IRP* irp; int index; int keyCount; ULONG_PTR* pKeys; @@ -79,15 +76,9 @@ static void smartcard_init(DEVICE* device) */ /** - * Step 1: Call SCardCancel on existing contexts, unblocking all outstanding IRPs. + * Call SCardCancel on existing contexts, unblocking all outstanding IRPs. */ -#ifdef DEBUG_SMARTCARD_INIT - printf("[1] rgSCardContextList: %d rgOutstandingMessages: %d\n", - ListDictionary_Count(smartcard->rgSCardContextList), - ListDictionary_Count(smartcard->rgOutstandingMessages)); -#endif - if (ListDictionary_Count(smartcard->rgSCardContextList) > 0) { pKeys = NULL; @@ -107,39 +98,9 @@ static void smartcard_init(DEVICE* device) } /** - * Step 2: Wait for all outstanding IRPs to complete. + * Call SCardReleaseContext on remaining contexts and remove them from rgSCardContextList. */ -#ifdef DEBUG_SMARTCARD_INIT - printf("[2] rgSCardContextList: %d rgOutstandingMessages: %d\n", - ListDictionary_Count(smartcard->rgSCardContextList), - ListDictionary_Count(smartcard->rgOutstandingMessages)); -#endif - - if (ListDictionary_Count(smartcard->rgOutstandingMessages) > 0) - { - pKeys = NULL; - keyCount = ListDictionary_GetKeys(smartcard->rgOutstandingMessages, &pKeys); - - for (index = 0; index < keyCount; index++) - { - irp = (IRP*) ListDictionary_GetItemValue(smartcard->rgOutstandingMessages, (void*) pKeys[index]); - ListDictionary_Remove(smartcard->rgOutstandingMessages, (void*) pKeys[index]); - } - - free(pKeys); - } - - /** - * Step 3: Call SCardReleaseContext on remaining contexts and remove them from rgSCardContextList. - */ - -#ifdef DEBUG_SMARTCARD_INIT - printf("[3] rgSCardContextList: %d rgOutstandingMessages: %d\n", - ListDictionary_Count(smartcard->rgSCardContextList), - ListDictionary_Count(smartcard->rgOutstandingMessages)); -#endif - if (ListDictionary_Count(smartcard->rgSCardContextList) > 0) { pKeys = NULL; @@ -158,12 +119,6 @@ static void smartcard_init(DEVICE* device) free(pKeys); } - -#ifdef DEBUG_SMARTCARD_INIT - printf("[4] rgSCardContextList: %d rgOutstandingMessages: %d\n", - ListDictionary_Count(smartcard->rgSCardContextList), - ListDictionary_Count(smartcard->rgOutstandingMessages)); -#endif } void smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp) @@ -329,7 +284,25 @@ static void* smartcard_thread_func(void* arg) break; if (message.id == WMQ_QUIT) + { + while (WaitForSingleObject(Queue_Event(smartcard->CompletedIrpQueue), 0) == WAIT_OBJECT_0) + { + irp = (IRP*) Queue_Dequeue(smartcard->CompletedIrpQueue); + + if (irp) + { + if (irp->thread) + { + WaitForSingleObject(irp->thread, INFINITE); + CloseHandle(irp->thread); + } + + smartcard_complete_irp(smartcard, irp); + } + } + break; + } irp = (IRP*) message.wParam;