[Asterisk-Dev] question about looping of code.

Steven Critchfield critch at basesys.com
Sun Nov 9 13:35:45 MST 2003


I know, clumsy subject line. 

While tracking down a problem the other day, I noticed something that
seems unnecessary. (BTW, the problem was in a private patch of mine.)

In channel.c *ast_waitfor_nandfds near the end, there ire 2 loops. It
seems that if the first loop is successful and assigns something to
winner, then nothing would happen in the second loop do to the if
statement. For that matter, it would seem that we continue looping
through the first loop even if we won't do anything after winner is
found. Would it be okay to move the time calculation to before the first
loop and just return instead of bothering to assign to winner? If you do
this, you can remove the winner completely from the function and just
place a return anywhere the assignment is and a null at the end of the
function.

I'll copy the code section below for easy viewing.

        for (x=0;x<n;x++) {
                c[x]->blocking = 0;
                for (y=0;y<AST_MAX_FDS;y++) {
                        if (c[x]->fds[y] > -1) {
                                if ((FD_ISSET(c[x]->fds[y], &rfds) || FD_ISSET(c[x]->fds[y], &efds)) && !winner) {
                                        /* Set exception flag if appropriate */
                                        if (FD_ISSET(c[x]->fds[y], &efds))
                                                c[x]->exception = 1;
                                        c[x]->fdno = y;
                                        winner = c[x];
                                }
                        }
                }
        }
        for (x=0;x<nfds;x++) {
                if ((FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && !winner) {
                        if (outfd)
                                *outfd = fds[x];
                        if (FD_ISSET(fds[x], &efds) && exception)
                                *exception = 1;
                        winner = NULL;
                }
        }
        *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
        return winner;
}

-- 
Steven Critchfield <critch at basesys.com>




More information about the asterisk-dev mailing list