Skip to content
Tcl Source Code Artifact [54007f3edb]
Login

Artifact [54007f3edb]

Artifact 54007f3edb94591c35523f7d100c4efbf96e0701:

Ticket change [54007f3edb] - New ticket [40eb6131cfc9a277|40eb6131cf] <i>Use poll() instead of select() to wait on file descriptors</i>. by anonymous on 2015-10-14 15:05:57.
D 2015-10-14T15:05:57.776
J assignee nobody
J closer nobody
J cmimetype text/plain
J comment By\susing\spoll()\sinstead\sof\sselect()\sit\sis\spossible\sto\swait\son\sa\ssignificantly\slarger\snumber\sof\sfile\sdescriptors.\r\n\r\ndiff\s-Nuar\stcl8.6.4.orig/unix/tclUnixChan.c\stcl8.6.4/unix/tclUnixChan.c\r\n---\stcl8.6.4.orig/unix/tclUnixChan.c\t2015-02-26\s11:57:28.000000000\s-0500\r\n+++\stcl8.6.4/unix/tclUnixChan.c\t2015-06-16\s15:24:33.688754594\s-0400\r\n@@\s-48,6\s+48,8\s@@\r\n\s\r\n\s#endif\t/*\sHAVE_TERMIOS_H\s*/\r\n\s\r\n+#include\s<poll.h>\r\n+\r\n\s/*\r\n\s\s*\sHelper\smacros\sto\smake\sparts\sof\sthis\sfile\sclearer.\sThe\smacros\sdo\sexactly\r\n\s\s*\swhat\sthey\ssay\son\sthe\stin.\s:-)\sThey\salso\sonly\sever\srefer\sto\stheir\sarguments\r\n@@\s-1731,22\s+1733,9\s@@\r\n\s\t\t\t\t\s*\sforever.\s*/\r\n\s{\r\n\s\s\s\s\sTcl_Time\sabortTime\s=\s{0,\s0},\snow;\s/*\ssilence\sgcc\s4\swarning\s*/\r\n-\s\s\s\sstruct\stimeval\sblockTime,\s*timeoutPtr;\r\n\s\s\s\s\sint\snumFound,\sresult\s=\s0;\r\n-\s\s\s\sfd_set\sreadableMask;\r\n-\s\s\s\sfd_set\swritableMask;\r\n-\s\s\s\sfd_set\sexceptionMask;\r\n-\r\n-#ifndef\s_DARWIN_C_SOURCE\r\n-\s\s\s\s/*\r\n-\s\s\s\s\s*\sSanity\scheck\sfd.\r\n-\s\s\s\s\s*/\r\n-\r\n-\s\s\s\sif\s(fd\s>=\sFD_SETSIZE)\s{\r\n-\tTcl_Panic("TclUnixWaitForFile\scan't\shandle\sfile\sid\s%d",\sfd);\r\n-\t/*\smust\snever\sget\shere,\sor\sselect\smasks\soverrun\swill\soccur\sbelow\s*/\r\n-\s\s\s\s}\r\n-#endif\r\n+\s\s\s\sint\smsec\s=\s0;\r\n+\s\s\s\sstruct\spollfd\spollfd;\r\n\s\r\n\s\s\s\s\s/*\r\n\s\s\s\s\s\s*\sIf\sthere\sis\sa\snon-zero\sfinite\stimeout,\scompute\sthe\stime\swhen\swe\sgive\r\n@@\s-1761,70\s+1750,55\s@@\r\n\s\t\s\s\s\sabortTime.usec\s-=\s1000000;\r\n\s\t\s\s\s\sabortTime.sec\s+=\s1;\r\n\s\t}\r\n-\ttimeoutPtr\s=\s&blockTime;\r\n\s\s\s\s\s}\selse\sif\s(timeout\s==\s0)\s{\r\n-\ttimeoutPtr\s=\s&blockTime;\r\n-\tblockTime.tv_sec\s=\s0;\r\n-\tblockTime.tv_usec\s=\s0;\r\n+\tmsec\s=\s0;\r\n\s\s\s\s\s}\selse\s{\r\n-\ttimeoutPtr\s=\sNULL;\r\n+\tmsec\s=\s-1;\r\n\s\s\s\s\s}\r\n\s\r\n\s\s\s\s\s/*\r\n-\s\s\s\s\s*\sInitialize\sthe\sselect\smasks.\r\n-\s\s\s\s\s*/\r\n-\r\n-\s\s\s\sFD_ZERO(&readableMask);\r\n-\s\s\s\sFD_ZERO(&writableMask);\r\n-\s\s\s\sFD_ZERO(&exceptionMask);\r\n-\r\n-\s\s\s\s/*\r\n\s\s\s\s\s\s*\sLoop\sin\sa\smini-event\sloop\sof\sour\sown,\swaiting\sfor\seither\sthe\sfile\sto\r\n\s\s\s\s\s\s*\sbecome\sready\sor\sa\stimeout\sto\soccur.\r\n\s\s\s\s\s\s*/\r\n\s\r\n\s\s\s\s\swhile\s(1)\s{\r\n\s\tif\s(timeout\s>\s0)\s{\r\n-\t\s\s\s\sblockTime.tv_sec\s=\sabortTime.sec\s-\snow.sec;\r\n-\t\s\s\s\sblockTime.tv_usec\s=\sabortTime.usec\s-\snow.usec;\r\n-\t\s\s\s\sif\s(blockTime.tv_usec\s<\s0)\s{\r\n-\t\tblockTime.tv_sec\s-=\s1;\r\n-\t\tblockTime.tv_usec\s+=\s1000000;\r\n-\t\s\s\s\s}\r\n-\t\s\s\s\sif\s(blockTime.tv_sec\s<\s0)\s{\r\n-\t\tblockTime.tv_sec\s=\s0;\r\n-\t\tblockTime.tv_usec\s=\s0;\r\n+\s\s\s\s\s\s\s\s\s\s\s\smsec\s\s=\s(abortTime.sec\s-\snow.sec)*1000;\r\n+\s\s\s\s\s\s\s\s\s\s\s\smsec\s+=\s(abortTime.usec\s-\snow.usec)/1000;\r\n+\t\s\s\s\sif\s(msec\s<\s0)\s{\r\n+\t\tmsec\s=\s0;\r\n\s\t\s\s\s\s}\r\n\s\t}\r\n\s\r\n\s\t/*\r\n-\t\s*\sSetup\sthe\sselect\smasks\sfor\sthe\sfd.\r\n+\t\s*\sSetup\spoll\sfor\sthe\sfd.\r\n\s\t\s*/\r\n\s\r\n+\s\s\s\s\s\s\s\spollfd.fd\s\s\s\s\s=\sfd;\r\n+\s\s\s\s\s\s\s\spollfd.events\s=\s0;\r\n\s\tif\s(mask\s&\sTCL_READABLE)\s{\r\n-\t\s\s\s\sFD_SET(fd,\s&readableMask);\r\n+\t\s\s\s\s\spollfd.events\s|=\sPOLLIN;\r\n\s\t}\r\n\s\tif\s(mask\s&\sTCL_WRITABLE)\s{\r\n-\t\s\s\s\sFD_SET(fd,\s&writableMask);\r\n+\t\s\s\s\s\spollfd.events\s|=\sPOLLOUT;\r\n\s\t}\r\n\s\tif\s(mask\s&\sTCL_EXCEPTION)\s{\r\n-\t\s\s\s\sFD_SET(fd,\s&exceptionMask);\r\n+\t\s\s\s\s\spollfd.events\s|=\sPOLLPRI;\r\n\s\t}\r\n\s\r\n\s\t/*\r\n\s\t\s*\sWait\sfor\sthe\sevent\sor\sa\stimeout.\r\n\s\t\s*/\r\n\s\r\n-\tnumFound\s=\sselect(fd\s+\s1,\s&readableMask,\s&writableMask,\r\n-\t\t&exceptionMask,\stimeoutPtr);\r\n+\tnumFound\s=\spoll(&pollfd,\s1,\smsec);\r\n\s\tif\s(numFound\s==\s1)\s{\r\n-\t\s\s\s\sif\s(FD_ISSET(fd,\s&readableMask))\s{\r\n+\t\s\s\s\sif\s(pollfd.revents\s&\sPOLLIN)\s{\r\n\s\t\tSET_BITS(result,\sTCL_READABLE);\r\n\s\t\s\s\s\s}\r\n-\t\s\s\s\sif\s(FD_ISSET(fd,\s&writableMask))\s{\r\n+\t\s\s\s\sif\s(pollfd.revents\s&\sPOLLOUT)\s{\r\n\s\t\tSET_BITS(result,\sTCL_WRITABLE);\r\n\s\t\s\s\s\s}\r\n-\t\s\s\s\sif\s(FD_ISSET(fd,\s&exceptionMask))\s{\r\n+\t\s\s\s\sif\s(pollfd.revents\s&\sPOLLPRI)\s{\r\n\s\t\tSET_BITS(result,\sTCL_EXCEPTION);\r\n\s\t\s\s\s\s}\r\n\s\t\s\s\s\sresult\s&=\smask;\r\ndiff\s-Nuar\stcl8.6.4.orig/unix/tclUnixNotfy.c\stcl8.6.4/unix/tclUnixNotfy.c\r\n---\stcl8.6.4.orig/unix/tclUnixNotfy.c\t2015-02-26\s11:57:28.000000000\s-0500\r\n+++\stcl8.6.4/unix/tclUnixNotfy.c\t2015-06-18\s16:12:58.789284455\s-0400\r\n@@\s-14,6\s+14,7\s@@\r\n\s#include\s"tclInt.h"\r\n\s#ifndef\sHAVE_COREFOUNDATION\t/*\sDarwin/Mac\sOS\sX\sCoreFoundation\snotifier\sis\r\n\s\t\t\t\t\s*\sin\stclMacOSXNotify.c\s*/\r\n+#include\s<poll.h>\r\n\s#include\s<signal.h>\r\n\s\r\n\s/*\r\n@@\s-49,16\s+50,8\s@@\r\n\s\t\t\t\t\s*\sthe\sevent\sis\squeued).\s*/\r\n\s}\sFileHandlerEvent;\r\n\s\r\n-/*\r\n-\s*\sThe\sfollowing\sstructure\scontains\sa\sset\sof\sselect()\smasks\sto\strack\sreadable,\r\n-\s*\swritable,\sand\sexception\sconditions.\r\n-\s*/\r\n-\r\n-typedef\sstruct\sSelectMasks\s{\r\n-\s\s\s\sfd_set\sreadable;\r\n-\s\s\s\sfd_set\swritable;\r\n-\s\s\s\sfd_set\sexception;\r\n-}\sSelectMasks;\r\n+#define\sPOLLFD_SIZE\t4096\r\n+#define\sNELEM(a)\t(sizeof\s(a)/sizeof\s*(a))\r\n\s\r\n\s/*\r\n\s\s*\sThe\sfollowing\sstatic\sstructure\scontains\sthe\sstate\sinformation\sfor\sthe\r\n@@\s-69,17\s+62,10\s@@\r\n\stypedef\sstruct\sThreadSpecificData\s{\r\n\s\s\s\s\sFileHandler\s*firstFileHandlerPtr;\r\n\s\t\t\t\t/*\sPointer\sto\shead\sof\sfile\shandler\slist.\s*/\r\n-\s\s\s\sSelectMasks\scheckMasks;\t/*\sThis\sstructure\sis\sused\sto\sbuild\sup\sthe\r\n-\t\t\t\t\s*\smasks\sto\sbe\sused\sin\sthe\snext\scall\sto\r\n-\t\t\t\t\s*\sselect.\sBits\sare\sset\sin\sresponse\sto\scalls\r\n-\t\t\t\t\s*\sto\sTcl_CreateFileHandler.\s*/\r\n-\s\s\s\sSelectMasks\sreadyMasks;\t/*\sThis\sarray\sreflects\sthe\sreadable/writable\r\n-\t\t\t\t\s*\sconditions\sthat\swere\sfound\sto\sexist\sby\sthe\r\n-\t\t\t\t\s*\slast\scall\sto\sselect.\s*/\r\n-\s\s\s\sint\snumFdBits;\t\t/*\sNumber\sof\svalid\sbits\sin\scheckMasks\s(one\r\n-\t\t\t\t\s*\smore\sthan\shighest\sfd\sfor\swhich\r\n-\t\t\t\t\s*\sTcl_WatchFile\shas\sbeen\scalled).\s*/\r\n+\s\s\s\sint\t\t\s\sfds_size;\r\n+\s\s\s\sstruct\spollfd\sfds[POLLFD_SIZE];\r\n\s#ifdef\sTCL_THREADS\r\n+\s\s\s\sint\t\t\s\sfds_offset;\t/*\soffset\sin\snotifier\sfds\sarray\s*/\r\n\s\s\s\s\sint\sonList;\t\t\t/*\sTrue\sif\sit\sis\sin\sthis\slist\s*/\r\n\s\s\s\s\sunsigned\sint\spollState;\t/*\spollState\sis\sused\sto\simplement\sa\spolling\r\n\s\t\t\t\t\s*\shandshake\sbetween\seach\sthread\sand\sthe\r\n@@\s-107,6\s+93,39\s@@\r\n\s#endif\s/*\sTCL_THREADS\s*/\r\n\s}\sThreadSpecificData;\r\n\s\r\n+static\sinline\sint\r\n+fds_index(\r\n+\s\s\s\sThreadSpecificData\s*tsdPtr,\r\n+\s\s\s\sint\sfd)\r\n+{\r\n+\s\s\s\sint\smin\s=\s0;\r\n+\s\s\s\sint\smax\s=\stsdPtr->fds_size\s-\s1;\r\n+\s\s\s\sint\sii;\r\n+\r\n+\s\s\s\swhile\s(max\s>=\smin)\s{\r\n+\tii\s=\s(max\s+\smin)/2;\r\n+\tif\s(tsdPtr->fds[ii].fd\s==\sfd)\s{\r\n+\t\s\s\s\sreturn\sii;\r\n+\t}\r\n+\tif\s(fd\s>\stsdPtr->fds[ii].fd)\s{\r\n+\t\s\s\s\smin\s=\sii\s+\s1;\r\n+\t}\selse\s{\r\n+\t\s\s\s\smax\s=\sii\s-\s1;\r\n+\t}\r\n+\s\s\s\s}\r\n+\r\n+\s\s\s\sreturn\s-1;\r\n+}\r\n+\r\n+static\sinline\sstruct\spollfd\s*\r\n+fds_pointer(\r\n+\s\s\s\sThreadSpecificData\s*tsdPtr,\r\n+\s\s\s\sint\sfd)\r\n+{\r\n+\s\s\s\sint\sii\s=\sfds_index(tsdPtr,\sfd);\r\n+\s\s\s\sreturn\sii\s!=\s-1\s?\s&tsdPtr->fds[ii]\s:\sNULL;\r\n+}\r\n+\r\n\sstatic\sTcl_ThreadDataKey\sdataKey;\r\n\s\r\n\s#ifdef\sTCL_THREADS\r\n@@\s-565,6\s+584,8\s@@\r\n\s\s\s\s\s}\selse\s{\r\n\s\tThreadSpecificData\s*tsdPtr\s=\sTCL_TSD_INIT(&dataKey);\r\n\s\tFileHandler\s*filePtr;\r\n+\tint\sii;\r\n+\tstruct\spollfd\s*fdsp;\r\n\s\r\n\s\tfor\s(filePtr\s=\stsdPtr->firstFileHandlerPtr;\sfilePtr\s!=\sNULL;\r\n\s\t\tfilePtr\s=\sfilePtr->nextPtr)\s{\r\n@@\s-587,24\s+608,33\s@@\r\n\s\t\s*\sUpdate\sthe\scheck\smasks\sfor\sthis\sfile.\r\n\s\t\s*/\r\n\s\r\n-\tif\s(mask\s&\sTCL_READABLE)\s{\r\n-\t\s\s\s\sFD_SET(fd,\s&tsdPtr->checkMasks.readable);\r\n-\t}\selse\s{\r\n-\t\s\s\s\sFD_CLR(fd,\s&tsdPtr->checkMasks.readable);\r\n-\t}\r\n-\tif\s(mask\s&\sTCL_WRITABLE)\s{\r\n-\t\s\s\s\sFD_SET(fd,\s&tsdPtr->checkMasks.writable);\r\n-\t}\selse\s{\r\n-\t\s\s\s\sFD_CLR(fd,\s&tsdPtr->checkMasks.writable);\r\n-\t}\r\n-\tif\s(mask\s&\sTCL_EXCEPTION)\s{\r\n-\t\s\s\s\sFD_SET(fd,\s&tsdPtr->checkMasks.exception);\r\n-\t}\selse\s{\r\n-\t\s\s\s\sFD_CLR(fd,\s&tsdPtr->checkMasks.exception);\r\n-\t}\r\n-\tif\s(tsdPtr->numFdBits\s<=\sfd)\s{\r\n-\t\s\s\s\stsdPtr->numFdBits\s=\sfd+1;\r\n-\t}\r\n+\tfdsp\s=\sfds_pointer(tsdPtr,\sfd);\r\n+\tif\s(fdsp\s==\sNULL\s&&\stsdPtr->fds_size\s<\sNELEM(tsdPtr->fds))\s{\r\n+\t\s\s\s\sfor\s(ii\s=\s0;\sii\s<\stsdPtr->fds_size;\sii++)\s{\r\n+\t\tif\s(tsdPtr->fds[ii].fd\s>\sfd)\s{\r\n+\t\t\s\s\s\sbreak;\r\n+\t\t}\r\n+\t\s\s\s\s}\r\n+\t\s\s\s\sfdsp\s=\s&tsdPtr->fds[ii];\r\n+\t\s\s\s\smemmove(&tsdPtr->fds[ii+1],\s&tsdPtr->fds[ii],\r\n+\t\t\s\s\s\ssizeof\stsdPtr->fds[ii]\s*\s(tsdPtr->fds_size\s-\sii));\r\n+\t\s\s\s\stsdPtr->fds_size++;\r\n+\t\s\s\s\sfdsp->fd\s=\sfd;\r\n+\t}\r\n+\tif\s(fdsp\s==\sNULL)\s{\r\n+\s\s\s\s\s\s\s\s\s\s\s\sTcl_Panic("%s:\sout\sof\sfile\sdescriptor\sspace",\s__FUNCTION__);\r\n+\s\s\s\s\s\s\s\s}\r\n+\r\n+\s\s\s\s\s\s\s\sfdsp->events\s=\s0;\r\n+\s\s\s\s\s\s\s\sif\s(mask\s&\sTCL_READABLE)\s{\r\n+\s\s\s\s\s\s\s\s\s\s\s\sfdsp->events\s|=\sPOLLIN;\r\n+\s\s\s\s\s\s\s\s}\r\n+\s\s\s\s\s\s\s\sif\s(mask\s&\sTCL_WRITABLE)\s{\r\n+\s\s\s\s\s\s\s\s\s\s\s\sfdsp->events\s|=\sPOLLOUT;\r\n+\s\s\s\s\s\s\s\s}\r\n+\s\s\s\s\s\s\s\sif\s(mask\s&\sTCL_EXCEPTION)\s{\r\n+\s\s\s\s\s\s\s\s\s\s\s\sfdsp->events\s|=\sPOLLPRI;\r\n+\s\s\s\s\s\s\s\s}\r\n\s\s\s\s\s}\r\n\s}\r\n\s\f\r\n@@\s-634,7\s+664,7\s@@\r\n\s\treturn;\r\n\s\s\s\s\s}\selse\s{\r\n\s\tFileHandler\s*filePtr,\s*prevPtr;\r\n-\tint\si;\r\n+\tint\sii;\r\n\s\tThreadSpecificData\s*tsdPtr\s=\sTCL_TSD_INIT(&dataKey);\r\n\s\r\n\s\t/*\r\n@@\s-655,32\s+685,11\s@@\r\n\s\t\s*\sUpdate\sthe\scheck\smasks\sfor\sthis\sfile.\r\n\s\t\s*/\r\n\s\r\n-\tif\s(filePtr->mask\s&\sTCL_READABLE)\s{\r\n-\t\s\s\s\sFD_CLR(fd,\s&tsdPtr->checkMasks.readable);\r\n-\t}\r\n-\tif\s(filePtr->mask\s&\sTCL_WRITABLE)\s{\r\n-\t\s\s\s\sFD_CLR(fd,\s&tsdPtr->checkMasks.writable);\r\n-\t}\r\n-\tif\s(filePtr->mask\s&\sTCL_EXCEPTION)\s{\r\n-\t\s\s\s\sFD_CLR(fd,\s&tsdPtr->checkMasks.exception);\r\n-\t}\r\n-\r\n-\t/*\r\n-\t\s*\sFind\scurrent\smax\sfd.\r\n-\t\s*/\r\n-\r\n-\tif\s(fd+1\s==\stsdPtr->numFdBits)\s{\r\n-\t\s\s\s\sint\snumFdBits\s=\s0;\r\n-\r\n-\t\s\s\s\sfor\s(i\s=\sfd-1;\si\s>=\s0;\si--)\s{\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.readable)\r\n-\t\t\t||\sFD_ISSET(i,\s&tsdPtr->checkMasks.writable)\r\n-\t\t\t||\sFD_ISSET(i,\s&tsdPtr->checkMasks.exception))\s{\r\n-\t\t\s\s\s\snumFdBits\s=\si+1;\r\n-\t\t\s\s\s\sbreak;\r\n-\t\t}\r\n-\t\s\s\s\s}\r\n-\t\s\s\s\stsdPtr->numFdBits\s=\snumFdBits;\r\n+\tii\s=\sfds_index(tsdPtr,\sfd);\r\n+\tif\s(ii\s!=\s-1)\s{\r\n+\t\s\s\s\stsdPtr->fds_size--;\r\n+\t\s\s\s\smemmove(&tsdPtr->fds[ii],\s&tsdPtr->fds[ii+1],\r\n+\t\t\s\s\s\ssizeof\stsdPtr->fds[ii]\s*\s(tsdPtr->fds_size\s-\sii));\r\n\s\t}\r\n\s\r\n\s\t/*\r\n@@\s-820,7\s+829,7\s@@\r\n\s\treturn\stclNotifierHooks.waitForEventProc(timePtr);\r\n\s\s\s\s\s}\selse\s{\r\n\s\tFileHandler\s*filePtr;\r\n-\tint\smask;\r\n+\tint\smask,\sii;\r\n\s\tTcl_Time\svTime;\r\n\s#ifdef\sTCL_THREADS\r\n\s\tint\swaitForFiles;\r\n@@\s-828,13\s+837,6\s@@\r\n\s\tMSG\smsg;\r\n\s#\s\s\sendif\s/*\s__CYGWIN__\s*/\r\n\s#else\r\n-\t/*\r\n-\t\s*\sImpl.\snotes:\stimeout\s&\stimeoutPtr\sare\sused\sif,\sand\sonly\sif\sthreads\r\n-\t\s*\sare\snot\senabled.\sThey\sare\sthe\sarguments\sfor\sthe\sregular\sselect()\r\n-\t\s*\sused\swhen\sthe\score\sis\snot\sthread-enabled.\r\n-\t\s*/\r\n-\r\n-\tstruct\stimeval\stimeout,\s*timeoutPtr;\r\n\s\tint\snumFound;\r\n\s#endif\s/*\sTCL_THREADS\s*/\r\n\s\tThreadSpecificData\s*tsdPtr\s=\sTCL_TSD_INIT(&dataKey);\r\n@@\s-858,10\s+860,7\s@@\r\n\s\t\ttimePtr\s=\s&vTime;\r\n\s\t\s\s\s\s}\r\n\s#ifndef\sTCL_THREADS\r\n-\t\s\s\s\stimeout.tv_sec\s=\stimePtr->sec;\r\n-\t\s\s\s\stimeout.tv_usec\s=\stimePtr->usec;\r\n-\t\s\s\s\stimeoutPtr\s=\s&timeout;\r\n-\t}\selse\sif\s(tsdPtr->numFdBits\s==\s0)\s{\r\n+\t}\selse\sif\s(tsdPtr->fds_size\s==\s0)\s{\r\n\s\t\s\s\s\s/*\r\n\s\t\s\s\s\s\s*\sIf\sthere\sare\sno\sthreads,\sno\stimeout,\sand\sno\sfds\sregistered,\r\n\s\t\s\s\s\s\s*\sthen\sthere\sare\sno\sevents\spossible\sand\swe\smust\savoid\sdeadlock.\r\n@@\s-871,8\s+870,6\s@@\r\n\s\t\s\s\s\s\s*/\r\n\s\r\n\s\t\s\s\s\sreturn\s-1;\r\n-\t}\selse\s{\r\n-\t\s\s\s\stimeoutPtr\s=\sNULL;\r\n\s#endif\s/*\s!TCL_THREADS\s*/\r\n\s\t}\r\n\s\r\n@@\s-932,10\s+929,16\s@@\r\n\s\t\s\s\s\stsdPtr->pollState\s=\sPOLL_WANT;\r\n\s\t\s\s\s\stimePtr\s=\sNULL;\r\n\s\t}\selse\s{\r\n-\t\s\s\s\swaitForFiles\s=\s(tsdPtr->numFdBits\s>\s0);\r\n+\t\s\s\s\swaitForFiles\s=\s(tsdPtr->fds_size\s>\s0);\r\n\s\t\s\s\s\stsdPtr->pollState\s=\s0;\r\n\s\t}\r\n\s\r\n+\t/*\sinitialize\sevents\s*/\r\n+\ttsdPtr->fds_offset\s=\s-1;\r\n+\tfor\s(ii\s=\s0;\sii\s<\stsdPtr->fds_size;\sii++)\s{\r\n+\t\s\s\s\stsdPtr->fds[ii].revents\s=\s0;\r\n+\t}\r\n+\r\n\s\tif\s(waitForFiles)\s{\r\n\s\t\s\s\s\s/*\r\n\s\t\s\s\s\s\s*\sAdd\sthe\sThreadSpecificData\sstructure\sof\sthis\sthread\sto\sthe\slist\r\n@@\s-957,10\s+960,6\s@@\r\n\s\t\s\s\s\s}\r\n\s\t}\r\n\s\r\n-\tFD_ZERO(&tsdPtr->readyMasks.readable);\r\n-\tFD_ZERO(&tsdPtr->readyMasks.writable);\r\n-\tFD_ZERO(&tsdPtr->readyMasks.exception);\r\n-\r\n\s\tif\s(!tsdPtr->eventReady)\s{\r\n\s#ifdef\s__CYGWIN__\r\n\s\t\s\s\s\sif\s(!PeekMessageW(&msg,\sNULL,\s0,\s0,\s0))\s{\r\n@@\s-1025,10\s+1024,8\s@@\r\n\s\t}\r\n\s\r\n\s#else\r\n-\ttsdPtr->readyMasks\s=\stsdPtr->checkMasks;\r\n-\tnumFound\s=\sselect(tsdPtr->numFdBits,\s&tsdPtr->readyMasks.readable,\r\n-\t\t&tsdPtr->readyMasks.writable,\s&tsdPtr->readyMasks.exception,\r\n-\t\ttimeoutPtr);\r\n+\tnumFound\s=\spoll(tsdPtr->fds,\stsdPtr->fds_size,\stimePtr\s==\sNULL\r\n+\t\t\t?\s-1\s:\s(timePtr->sec*1000)+(timePtr->usec/1000));\r\n\s\r\n\s\t/*\r\n\s\t\s*\sSome\ssystems\sdon't\sclear\sthe\smasks\safter\san\serror,\sso\swe\shave\sto\sdo\r\n@@\s-1036,9\s+1033,9\s@@\r\n\s\t\s*/\r\n\s\r\n\s\tif\s(numFound\s==\s-1)\s{\r\n-\t\s\s\s\sFD_ZERO(&tsdPtr->readyMasks.readable);\r\n-\t\s\s\s\sFD_ZERO(&tsdPtr->readyMasks.writable);\r\n-\t\s\s\s\sFD_ZERO(&tsdPtr->readyMasks.exception);\r\n+\t\s\s\s\sfor\s(ii\s=\s0;\sii\s<\stsdPtr->fds_size;\sii++)\s{\r\n+\t\ttsdPtr->fds[ii].revents\s=\s0;\r\n+\t\s\s\s\s}\r\n\s\t}\r\n\s#endif\s/*\sTCL_THREADS\s*/\r\n\s\r\n@@\s-1048,14\s+1045,34\s@@\r\n\s\r\n\s\tfor\s(filePtr\s=\stsdPtr->firstFileHandlerPtr;\s(filePtr\s!=\sNULL);\r\n\s\t\tfilePtr\s=\sfilePtr->nextPtr)\s{\r\n+\t\s\s\s\sstruct\spollfd\s*fdsp\s=\sfds_pointer(tsdPtr,\sfilePtr->fd);\r\n+\t\s\s\s\sshort\srevents;\r\n+\r\n+\t\s\s\s\sif\s(fdsp\s==\sNULL)\s{\r\n+\t\tTcl_Panic("%s:\sfd=%d\snot\sfound",\s__FUNCTION__,\sfilePtr->fd);\r\n+\t\tcontinue;\r\n+\t\s\s\s\s}\r\n+\r\n+\t\s\s\s\sif\s(fdsp->revents\s&\s(POLLNVAL\s|\sPOLLHUP\s|\sPOLLERR))\s{\r\n+\t\t/*\ssomething\sis\swrong\s*/\r\n+\t\tif\s(fdsp->revents\s&\sPOLLNVAL)\s{\r\n+\t\t\s\s\s\s/*\sfile\sdescriptor\sis\sno\slonger\svalid,\sskip\sit\s*/\r\n+\t\t\s\s\s\scontinue;\r\n+\t\t}\r\n+\r\n+\t\t/*\semulate\sthe\sread\sor\swrite\sso\sthey\sget\sthe\serror\scondition\s*/\r\n+\t\tfdsp->revents\s|=\sfdsp->events;\r\n+\t\s\s\s\s}\r\n+\r\n\s\t\s\s\s\smask\s=\s0;\r\n-\t\s\s\s\sif\s(FD_ISSET(filePtr->fd,\s&tsdPtr->readyMasks.readable))\s{\r\n+\t\s\s\s\srevents\s=\sfdsp->revents\s&\sfdsp->events;\r\n+\t\s\s\s\sif\s(revents\s&\sPOLLIN)\s{\r\n\s\t\tmask\s|=\sTCL_READABLE;\r\n\s\t\s\s\s\s}\r\n-\t\s\s\s\sif\s(FD_ISSET(filePtr->fd,\s&tsdPtr->readyMasks.writable))\s{\r\n+\t\s\s\s\sif\s(revents\s&\sPOLLOUT)\s{\r\n\s\t\tmask\s|=\sTCL_WRITABLE;\r\n\s\t\s\s\s\s}\r\n-\t\s\s\s\sif\s(FD_ISSET(filePtr->fd,\s&tsdPtr->readyMasks.exception))\s{\r\n+\t\s\s\s\sif\s(revents\s&\sPOLLPRI)\s{\r\n\s\t\tmask\s|=\sTCL_EXCEPTION;\r\n\s\t\s\s\s\s}\r\n\s\r\n@@\s-1115,13\s+1132,12\s@@\r\n\s\s\s\s\sClientData\sclientData)\t/*\sNot\sused.\s*/\r\n\s{\r\n\s\s\s\s\sThreadSpecificData\s*tsdPtr;\r\n-\s\s\s\sfd_set\sreadableMask;\r\n-\s\s\s\sfd_set\swritableMask;\r\n-\s\s\s\sfd_set\sexceptionMask;\r\n+\s\s\s\sstruct\spollfd\s*\sfdsp;\r\n+\s\s\s\sint\sfds_size,\sfds_num;\r\n\s\s\s\s\sint\sfds[2];\r\n-\s\s\s\sint\si,\snumFdBits\s=\s0,\sreceivePipe;\r\n+\s\s\s\sint\sii,\sreceivePipe;\r\n\s\s\s\s\slong\sfound;\r\n-\s\s\s\sstruct\stimeval\spoll\s=\s{0.,\s0.},\s*timePtr;\r\n+\s\s\s\sint\stimeout;\r\n\s\s\s\s\schar\sbuf[2];\r\n\s\r\n\s\s\s\s\sif\s(pipe(fds)\s!=\s0)\s{\r\n@@\s-1162,35\s+1178,43\s@@\r\n\s\s\s\s\sTcl_MutexUnlock(&notifierMutex);\r\n\s\r\n\s\s\s\s\s/*\r\n+\s\s\s\s\s*\sallocate\senough\sspace\sin\sthe\spoll\smask\sfor\sthe\sreceive\spipe\r\n+\s\s\s\s\s*/\r\n+\r\n+\s\s\s\sfds_size\s=\s128;\r\n+\s\s\s\sfdsp\s\s\s\s\s=\sckalloc((sizeof\s*fdsp)\s*\sfds_size);\r\n+\r\n+\s\s\s\s/*\r\n\s\s\s\s\s\s*\sLook\sfor\sfile\sevents\sand\sreport\sthem\sto\sinterested\sthreads.\r\n\s\s\s\s\s\s*/\r\n\s\r\n\s\s\s\s\swhile\s(1)\s{\r\n-\tFD_ZERO(&readableMask);\r\n-\tFD_ZERO(&writableMask);\r\n-\tFD_ZERO(&exceptionMask);\r\n+\t/*\r\n+\t\s*\sSet\sup\sthe\spoll\smask\sto\sinclude\sthe\sreceive\spipe.\r\n+\t\s*/\r\n+\r\n+\tfdsp[0].fd\s\s\s\s\s=\sreceivePipe;\r\n+\tfdsp[0].events\s=\sPOLLIN;\r\n+\tfds_num\s=\s1;\r\n\s\r\n\s\t/*\r\n\s\t\s*\sCompute\sthe\slogical\sOR\sof\sthe\sselect\smasks\sfrom\sall\sthe\swaiting\r\n\s\t\s*\snotifiers.\r\n\s\t\s*/\r\n\s\r\n+\ttimeout\s=\s-1;\r\n+\r\n\s\tTcl_MutexLock(&notifierMutex);\r\n-\ttimePtr\s=\sNULL;\r\n\s\tfor\s(tsdPtr\s=\swaitingListPtr;\stsdPtr;\stsdPtr\s=\stsdPtr->nextPtr)\s{\r\n-\t\s\s\s\sfor\s(i\s=\stsdPtr->numFdBits-1;\si\s>=\s0;\s--i)\s{\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.readable))\s{\r\n-\t\t\s\s\s\sFD_SET(i,\s&readableMask);\r\n-\t\t}\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.writable))\s{\r\n-\t\t\s\s\s\sFD_SET(i,\s&writableMask);\r\n-\t\t}\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.exception))\s{\r\n-\t\t\s\s\s\sFD_SET(i,\s&exceptionMask);\r\n-\t\t}\r\n-\t\s\s\s\s}\r\n-\t\s\s\s\sif\s(tsdPtr->numFdBits\s>\snumFdBits)\s{\r\n-\t\tnumFdBits\s=\stsdPtr->numFdBits;\r\n+\t\s\s\s\sif\s((fds_num\s+\stsdPtr->fds_size)\s>\sfds_size)\s{\r\n+\t\tfds_size\s=\sfds_num\s+\stsdPtr->fds_size;\r\n+\t\tfdsp\t\s=\sckrealloc(fdsp,\s(sizeof\s*fdsp)\s*\sfds_size);\r\n+\t\s\s\s\s}\r\n+\t\s\s\s\stsdPtr->fds_offset\s=\sfds_num;\r\n+\t\s\s\s\sfor\s(ii\s=\s0;\sii\s<\stsdPtr->fds_size;\sii++)\s{\r\n+\t\tfdsp[fds_num].fd\s\s\s\s\s=\stsdPtr->fds[ii].fd;\r\n+\t\tfdsp[fds_num].events\s=\stsdPtr->fds[ii].events;\r\n+\t\tfds_num++;\r\n\s\t\s\s\s\s}\r\n\s\t\s\s\s\sif\s(tsdPtr->pollState\s&\sPOLL_WANT)\s{\r\n\s\t\t/*\r\n@@\s-1199,22\s+1223,12\s@@\r\n\s\t\t\s*/\r\n\s\r\n\s\t\ttsdPtr->pollState\s|=\sPOLL_DONE;\r\n-\t\ttimePtr\s=\s&poll;\r\n+\t\ttimeout\s=\s0;\r\n\s\t\s\s\s\s}\r\n\s\t}\r\n\s\tTcl_MutexUnlock(&notifierMutex);\r\n\s\r\n-\t/*\r\n-\t\s*\sSet\sup\sthe\sselect\smask\sto\sinclude\sthe\sreceive\spipe.\r\n-\t\s*/\r\n-\r\n-\tif\s(receivePipe\s>=\snumFdBits)\s{\r\n-\t\s\s\s\snumFdBits\s=\sreceivePipe\s+\s1;\r\n-\t}\r\n-\tFD_SET(receivePipe,\s&readableMask);\r\n-\r\n-\tif\s(select(numFdBits,\s&readableMask,\s&writableMask,\s&exceptionMask,\r\n-\t\ttimePtr)\s==\s-1)\s{\r\n+\tif\s(poll(fdsp,\sfds_num,\stimeout)\s==\s-1)\s{\r\n\s\t\s\s\s\s/*\r\n\s\t\s\s\s\s\s*\sTry\sagain\simmediately\son\san\serror.\r\n\s\t\s\s\s\s\s*/\r\n@@\s-1228,22\s+1242,14\s@@\r\n\s\r\n\s\tTcl_MutexLock(&notifierMutex);\r\n\s\tfor\s(tsdPtr\s=\swaitingListPtr;\stsdPtr;\stsdPtr\s=\stsdPtr->nextPtr)\s{\r\n-\t\s\s\s\sfound\s=\s0;\r\n+\t\s\s\s\sif\s(tsdPtr->fds_offset\s<\s0)\s{\r\n+\t\tcontinue;\r\n+\t\s\s\s\s}\r\n\s\r\n-\t\s\s\s\sfor\s(i\s=\stsdPtr->numFdBits-1;\si\s>=\s0;\s--i)\s{\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.readable)\r\n-\t\t\t&&\sFD_ISSET(i,\s&readableMask))\s{\r\n-\t\t\s\s\s\sFD_SET(i,\s&tsdPtr->readyMasks.readable);\r\n-\t\t\s\s\s\sfound\s=\s1;\r\n-\t\t}\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.writable)\r\n-\t\t\t&&\sFD_ISSET(i,\s&writableMask))\s{\r\n-\t\t\s\s\s\sFD_SET(i,\s&tsdPtr->readyMasks.writable);\r\n-\t\t\s\s\s\sfound\s=\s1;\r\n-\t\t}\r\n-\t\tif\s(FD_ISSET(i,\s&tsdPtr->checkMasks.exception)\r\n-\t\t\t&&\sFD_ISSET(i,\s&exceptionMask))\s{\r\n-\t\t\s\s\s\sFD_SET(i,\s&tsdPtr->readyMasks.exception);\r\n+\t\s\s\s\sfound\s=\s0;\r\n+\t\s\s\s\sfor\s(ii\s=\s0;\sii\s<\stsdPtr->fds_size;\sii++)\s{\r\n+\t\ttsdPtr->fds[ii].revents\s=\sfdsp[tsdPtr->fds_offset\s+\sii].revents;\r\n+\t\tif\s(tsdPtr->fds[ii].revents\s&\stsdPtr->fds[ii].events)\s{\r\n\s\t\t\s\s\s\sfound\s=\s1;\r\n\s\t\t}\r\n\s\t\s\s\s\s}\r\n@@\s-1285,10\s+1291,10\s@@\r\n\s\t\s*\savoid\sa\srace\scondition\swe\sonly\sread\sone\sat\sa\stime.\r\n\s\t\s*/\r\n\s\r\n-\tif\s(FD_ISSET(receivePipe,\s&readableMask))\s{\r\n-\t\s\s\s\si\s=\sread(receivePipe,\sbuf,\s1);\r\n+\tif\s(fdsp[0].revents\s&\sPOLLIN)\s{\r\n+\t\s\s\s\sii\s=\sread(receivePipe,\sbuf,\s1);\r\n\s\r\n-\t\s\s\s\sif\s((i\s==\s0)\s||\s((i\s==\s1)\s&&\s(buf[0]\s==\s'q')))\s{\r\n+\t\s\s\s\sif\s((ii\s==\s0)\s||\s((ii\s==\s1)\s&&\s(buf[0]\s==\s'q')))\s{\r\n\s\t\t/*\r\n\s\t\t\s*\sSomeone\sclosed\sthe\swrite\send\sof\sthe\spipe\sor\ssent\sus\sa\sQuit\r\n\s\t\t\s*\smessage\s[Bug:\s4139]\sand\sthen\sclosed\sthe\swrite\send\sof\sthe\r\n@@\s-1310,6\s+1316,7\s@@\r\n\s\s\s\s\striggerPipe\s=\s-1;\r\n\s\s\s\s\sTcl_ConditionNotify(&notifierCV);\r\n\s\s\s\s\sTcl_MutexUnlock(&notifierMutex);\r\n+\s\s\s\sckfree(fdsp);\r\n\s\r\n\s\s\s\s\sTclpThreadExit(0);\r\n\s}
J foundin 8.6.4
J is_private 0
J login anonymous
J priority 5\sMedium
J private_contact 9a6c641caad491522f67f69ee7852e9faed9007e
J resolution None
J severity Important
J status Open
J submitter anonymous
J subsystem 20.\s[interp]
J title Use\spoll()\sinstead\sof\sselect()\sto\swait\son\sfile\sdescriptors
J type Patch
K 40eb6131cfc9a277c57d39ed8f61ba09f5133674
U anonymous
Z 4b5c2392e70e34d71ecdd776a6a65a25