这个还是放在后面解说比较好,因为关系到和一个reader上下文相关的线程终止的问题。
可是目前还没有解说到该线程的启动。
418行, RFUnInitializeReader定义在readerfactory.c
实现如下:
1062 LONG RFUnInitializeReader(PREADER_CONTEXT rContext) 1063 { 1064 Log2(PCSC_LOG_INFO, "Attempting shutdown of %s.", 1065 rContext->lpcReader); 1066 1067 /* Close the port, unbind the functions, and unload the library */ 1068 1069 /* 1070 * If the reader is getting uninitialized then it is being unplugged 1071 * so I can't send a IFDPowerICC call to it 1072 * 1073 * IFDPowerICC( rContext, IFD_POWER_DOWN, Atr, &AtrLen ); 1074 */ 1075 (void)IFDCloseIFD(rContext); 1076 (void)RFUnBindFunctions(rContext); 1077 (void)RFUnloadReader(rContext); 1078 1079 return SCARD_S_SUCCESS; 1080 } |
1062~1080行,好像在哪里有见过?
原来在上面的 RFInitializeReader里做了相反的事情。
RFLoadReader 初始化上下文的 vHandle.
RFBindFunctions 初始化上下的psFunctions.psFunctions_vx(x为1,2,3)
IFDOpenIFD 则是create channel.
RFUnInitializeReader则做相反的事情了。析构。
IFDCloseIFD定义在ifdwrapper.c .
实现如下:
181 LONG IFDCloseIFD(PREADER_CONTEXT rContext) 182 { 183 RESPONSECODE rv = IFD_SUCCESS; 184 int repeat; 185 186 #ifndef PCSCLITE_STATIC_DRIVER 187 RESPONSECODE(*IO_close_channel) (void) = NULL; 188 RESPONSECODE(*IFDH_close_channel) (DWORD) = NULL; 189 190 if (rContext->dwVersion == IFD_HVERSION_1_0) 191 IO_close_channel = rContext->psFunctions.psFunctions_v1.pvfCloseChannel; 192 else 193 IFDH_close_channel = rContext->psFunctions.psFunctions_v2.pvfCloseChannel; 194 #endif 195 196 /* TRY TO LOCK THIS CODE REGION */ 197 repeat = 5; 198 again: 199 rv = SYS_MutexTryLock(rContext->mMutex); 200 if (EBUSY == rv) 201 { 202 Log1(PCSC_LOG_ERROR, "Locking failed"); 203 repeat--; 204 if (repeat) 205 { 206 (void)SYS_USleep(100*1000); /* 100 ms */ 207 goto again; 208 } 209 } 210 211 #ifndef PCSCLITE_STATIC_DRIVER 212 if (rContext->dwVersion == IFD_HVERSION_1_0) 213 214 rv = (*IO_close_channel) (); 215 else 216 rv = (*IFDH_close_channel) (rContext->dwSlot); 217 #else 218 if (rContext->dwVersion == IFD_HVERSION_1_0) 219 rv = IO_Close_Channel(); 220 else 221 rv = IFDHCloseChannel(rContext->dwSlot); 222 #endif 223 224 /* END OF LOCKED REGION */ 225 (void)SYS_MutexUnLock(rContext->mMutex); 226 227 return rv; 228 } |
IFDCloseIFD做了什么?当然,和IFDOpenIFD相反,关闭channel.
值得注意的是,这里采用了 SYS_MutexTryLock,而不是 SYS_MutexLock,并给了5次去获得锁的机会,如果没有机会,那么就不要锁了,直接CloseChannel.
这样会存在一些问题的。
RFUnBindFunctions定义在readerfactory.c
实现如下:
901 LONG RFUnBindFunctions(PREADER_CONTEXT rContext) 902 { 903 /* Zero out everything */ 904 memset(&rContext->psFunctions, 0, sizeof(rContext->psFunctions)); 905 906 return SCARD_S_SUCCESS; 907 } |
去掉RFBindFunctions中获得的函数地址。
RFUnloadReader定义在readerfactory.c
实现如下:
909 LONG RFUnloadReader(PREADER_CONTEXT rContext) 910 { 911 /* Make sure no one else is using this library */ 912 if (*rContext->pdwFeeds == 1) 913 { 914 Log1(PCSC_LOG_INFO, "Unloading reader driver."); 915 (void)DYN_CloseLibrary(&rContext->vHandle); 916 } 917 918 rContext->vHandle = NULL; 919 920 return SCARD_S_SUCCESS; 921 } |
前面提过了, pdwFeeds是驱动参考计数。比如:有两个读卡器使用同一个驱动,那么这个
字段就是2.
912行,要先判断,读卡器上下文是否是使用对应驱动的最后一个读卡器上下文。
918行,最后清除vHandle.
RFUnInitializeReader解说完成。
回到readerfactory.c
的RFRemoveReader.
422 /* Destroy and free the mutex */ 423 if ((NULL == sContext->pdwMutex) || (NULL == sContext->pdwFeeds)) 424 { 425 Log1(PCSC_LOG_ERROR, 426 "Trying to remove an already removed driver"); 427 return SCARD_E_INVALID_VALUE; 428 } 429 430 if (*sContext->pdwMutex == 1) 431 { 432 (void)SYS_MutexDestroy(sContext->mMutex); 433 free(sContext->mMutex); 434 } |
423行,前面解释过了。
430行,表示剩下最后一个共用驱动的读卡器。清除该读卡器对应的上下文,当然也要释放
互斥锁内存。
435 436 *sContext->pdwMutex -= 1; 437 438 if (*sContext->pdwMutex == 0) 439 { 440 free(sContext->pdwMutex); 441 sContext->pdwMutex = NULL; 442 } 443 |
438行,引用计数降为0,释放互斥内存。
444 *sContext->pdwFeeds -= 1; 445 446 /* Added by Dave to free the pdwFeeds variable */ 447 448 if (*sContext->pdwFeeds == 0) 449 { 450 free(sContext->pdwFeeds); 451 sContext->pdwFeeds = NULL; 452 } |
444~452行,采用同样的方法,处理pdwFeeds.
453 454 sContext->lpcDevice[0] = 0; 455 sContext->dwVersion = 0; 456 sContext->dwPort = 0; 457 sContext->mMutex = NULL; 458 sContext->dwBlockStatus = 0; 459 sContext->dwContexts = 0; 460 sContext->dwSlot = 0; 461 sContext->dwLockId = 0; 462 sContext->LockCount = 0; 463 sContext->vHandle = NULL; 464 sContext->dwIdentity = 0; 465 sContext->readerState = NULL; 466 467 for (i = 0; i < PCSCLITE_MAX_READER_CONTEXT_CHANNELS; i++) 468 sContext->psHandles[i].hCard = 0; 469 470 dwNumReadersContexts -= 1; |
453~470行,清除剩下的字段。
471 472 /* signal an event to clients */ 473 StatSynchronize(NULL); 474 } 475 476 return SCARD_S_SUCCESS; 477 } |
471行,通过pipe发出信号,通知客户端。上面讲过了。
RFRemoveReader结束。
有RFAddReader,就有RFRemoveReader.有开始就有结束。