Important Update: (Massive CPU usage improvement)
- Bug fix: Select timeout not constant, reduces to zero - Only scan FDs if Select event occured
This commit is contained in:
@@ -160,13 +160,14 @@ bool CSelect::Test()
|
||||
TSelectHandle ** HandlePtr = NULL;
|
||||
int TestFD = 0;
|
||||
int Events = 0;
|
||||
timeval STimeout = Timeout;
|
||||
|
||||
// Set Test sets
|
||||
ReadFDS = ReadTestFDS;
|
||||
WriteFDS = WriteTestFDS;
|
||||
|
||||
// Perform select
|
||||
Events = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &Timeout );
|
||||
Events = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &STimeout );
|
||||
if (Events < 0)
|
||||
{
|
||||
LogMessage( dlHigh, "Select: Select operation failed" );
|
||||
@@ -174,47 +175,49 @@ bool CSelect::Test()
|
||||
}
|
||||
|
||||
// Check all descriptors for events
|
||||
Handle = FirstHandle;
|
||||
while (Handle)
|
||||
{
|
||||
// Check if to remove from list
|
||||
if (!Handle->Read && !Handle->Write)
|
||||
if (Events) {
|
||||
Handle = FirstHandle;
|
||||
while (Handle)
|
||||
{
|
||||
// Update Maximum Test FD
|
||||
if (Handle->FD == MaxFD-1) {
|
||||
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
|
||||
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
|
||||
break;
|
||||
// Check if to remove from list
|
||||
if (!Handle->Read && !Handle->Write)
|
||||
{
|
||||
// Update Maximum Test FD
|
||||
if (Handle->FD == MaxFD-1) {
|
||||
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
|
||||
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
MaxFD = TestFD+1;
|
||||
}
|
||||
MaxFD = TestFD+1;
|
||||
|
||||
// Remove from list
|
||||
HandlePtr = &FirstHandle;
|
||||
while (*HandlePtr && (*HandlePtr != Handle))
|
||||
HandlePtr = &((*HandlePtr)->Next);
|
||||
*HandlePtr = (*HandlePtr)->Next;
|
||||
|
||||
// Destroy and go to next
|
||||
free( Handle );
|
||||
Handle = *HandlePtr;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check read Event
|
||||
if (FD_ISSET( Handle->FD, &ReadFDS ) && Handle->Function) {
|
||||
Handle->Function->Read( Handle->FD );
|
||||
}
|
||||
|
||||
// Remove from list
|
||||
HandlePtr = &FirstHandle;
|
||||
while (*HandlePtr && (*HandlePtr != Handle))
|
||||
HandlePtr = &((*HandlePtr)->Next);
|
||||
*HandlePtr = (*HandlePtr)->Next;
|
||||
|
||||
// Destroy and go to next
|
||||
free( Handle );
|
||||
Handle = *HandlePtr;
|
||||
continue;
|
||||
// Check Write Event
|
||||
if (FD_ISSET( Handle->FD, &WriteFDS ) && Handle->Function) {
|
||||
Handle->Function->Write( Handle->FD );
|
||||
}
|
||||
}
|
||||
// Next
|
||||
Handle = Handle->Next;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check read Event
|
||||
if (FD_ISSET( Handle->FD, &ReadFDS ) && Handle->Function) {
|
||||
Handle->Function->Read( Handle->FD );
|
||||
}
|
||||
|
||||
// Check Write Event
|
||||
if (FD_ISSET( Handle->FD, &WriteFDS ) && Handle->Function) {
|
||||
Handle->Function->Write( Handle->FD );
|
||||
}
|
||||
}
|
||||
// Next
|
||||
Handle = Handle->Next;
|
||||
}
|
||||
|
||||
// return success
|
||||
|
||||
Reference in New Issue
Block a user