Temporarily revert poll changes

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2757 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2004-04-24 23:11:28 +00:00
parent 470f226d9d
commit b9604cde6c
3 changed files with 101 additions and 138 deletions

View File

@@ -13,7 +13,6 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/poll.h>
#include <asterisk/logger.h> #include <asterisk/logger.h>
#include <asterisk/options.h> #include <asterisk/options.h>
#include <asterisk/cli.h> #include <asterisk/cli.h>
@@ -230,24 +229,26 @@ static void *netconsole(void *vconsole)
char hostname[256]; char hostname[256];
char tmp[512]; char tmp[512];
int res; int res;
struct pollfd fds[2]; int max;
fd_set rfds;
if (gethostname(hostname, sizeof(hostname))) if (gethostname(hostname, sizeof(hostname)))
strncpy(hostname, "<Unknown>", sizeof(hostname)-1); strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
snprintf(tmp, sizeof(tmp), "%s/%d/%s\n", hostname, ast_mainpid, ASTERISK_VERSION); snprintf(tmp, sizeof(tmp), "%s/%d/%s\n", hostname, ast_mainpid, ASTERISK_VERSION);
fdprint(con->fd, tmp); fdprint(con->fd, tmp);
for(;;) { for(;;) {
fds[0].fd = con->fd; FD_ZERO(&rfds);
fds[0].events = POLLIN; FD_SET(con->fd, &rfds);
fds[1].fd = con->p[0]; FD_SET(con->p[0], &rfds);
fds[1].events = POLLIN; max = con->fd;
if (con->p[0] > max)
res = poll(fds, 2, -1); max = con->p[0];
res = ast_select(max + 1, &rfds, NULL, NULL, NULL);
if (res < 0) { if (res < 0) {
ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno)); ast_log(LOG_WARNING, "select returned < 0: %s\n", strerror(errno));
continue; continue;
} }
if (fds[0].revents) { if (FD_ISSET(con->fd, &rfds)) {
res = read(con->fd, tmp, sizeof(tmp)); res = read(con->fd, tmp, sizeof(tmp));
if (res < 1) { if (res < 1) {
break; break;
@@ -255,7 +256,7 @@ static void *netconsole(void *vconsole)
tmp[res] = 0; tmp[res] = 0;
ast_cli_command(con->fd, tmp); ast_cli_command(con->fd, tmp);
} }
if (fds[1].revents) { if (FD_ISSET(con->p[0], &rfds)) {
res = read(con->p[0], tmp, sizeof(tmp)); res = read(con->p[0], tmp, sizeof(tmp));
if (res < 1) { if (res < 1) {
ast_log(LOG_ERROR, "read returned %d\n", res); ast_log(LOG_ERROR, "read returned %d\n", res);
@@ -279,23 +280,23 @@ static void *netconsole(void *vconsole)
static void *listener(void *unused) static void *listener(void *unused)
{ {
struct sockaddr_un sun; struct sockaddr_un sun;
fd_set fds;
int s; int s;
int len; int len;
int x; int x;
int flags; int flags;
struct pollfd fds[1];
pthread_attr_t attr; pthread_attr_t attr;
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for(;;) { for(;;) {
if (ast_socket < 0) if (ast_socket < 0)
return NULL; return NULL;
fds[0].fd = ast_socket; FD_ZERO(&fds);
fds[0].events= POLLIN; FD_SET(ast_socket, &fds);
s = poll(fds, 1, -1); s = ast_select(ast_socket + 1, &fds, NULL, NULL, NULL);
if (s < 0) { if (s < 0) {
if (errno != EINTR) if (errno != EINTR)
ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno)); ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
continue; continue;
} }
len = sizeof(sun); len = sizeof(sun);
@@ -396,7 +397,7 @@ static int ast_tryconnect(void)
static void urg_handler(int num) static void urg_handler(int num)
{ {
/* Called by soft_hangup to interrupt the poll, read, or other /* Called by soft_hangup to interrupt the select, read, or other
system call. We don't actually need to do anything though. */ system call. We don't actually need to do anything though. */
/* Cannot EVER ast_log from within a signal handler */ /* Cannot EVER ast_log from within a signal handler */
if (option_debug) if (option_debug)
@@ -627,7 +628,7 @@ static void console_verboser(const char *s, int pos, int replace, int complete)
fputs(s + pos,stdout); fputs(s + pos,stdout);
fflush(stdout); fflush(stdout);
if (complete) if (complete)
/* Wake up a poll()ing console */ /* Wake up a select()ing console */
if (option_console && consolethread != AST_PTHREADT_NULL) if (option_console && consolethread != AST_PTHREADT_NULL)
pthread_kill(consolethread, SIGURG); pthread_kill(consolethread, SIGURG);
} }
@@ -820,38 +821,38 @@ static struct ast_cli_entry astbang = { { "!", NULL }, handle_bang, "Execute a s
static int ast_el_read_char(EditLine *el, char *cp) static int ast_el_read_char(EditLine *el, char *cp)
{ {
int num_read=0; int num_read=0;
int lastpos=0; int lastpos=0;
struct pollfd fds[2]; fd_set rfds;
int res; int res;
int max; int max;
char buf[512]; char buf[512];
for (;;) { for (;;) {
max = 1; FD_ZERO(&rfds);
fds[0].fd = ast_consock; FD_SET(ast_consock, &rfds);
fds[0].events = POLLIN; max = ast_consock;
if (!option_exec) { if (!option_exec) {
fds[1].fd = STDIN_FILENO; FD_SET(STDIN_FILENO, &rfds);
fds[1].events = POLLIN; if (STDIN_FILENO > max)
max++; max = STDIN_FILENO;
} }
res = poll(fds, max, -1); res = ast_select(max+1, &rfds, NULL, NULL, NULL);
if (res < 0) { if (res < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
ast_log(LOG_ERROR, "poll failed: %s\n", strerror(errno)); ast_log(LOG_ERROR, "select failed: %s\n", strerror(errno));
break; break;
} }
if (!option_exec && fds[1].revents) { if (FD_ISSET(STDIN_FILENO, &rfds)) {
num_read = read(STDIN_FILENO, cp, 1); num_read = read(STDIN_FILENO, cp, 1);
if (num_read < 1) { if (num_read < 1) {
break; break;
} else } else
return (num_read); return (num_read);
} }
if (fds[0].revents) { if (FD_ISSET(ast_consock, &rfds)) {
res = read(ast_consock, buf, sizeof(buf) - 1); res = read(ast_consock, buf, sizeof(buf) - 1);
/* if the remote side disappears exit */ /* if the remote side disappears exit */
if (res < 1) { if (res < 1) {
@@ -1671,7 +1672,7 @@ int main(int argc, char *argv[])
} else { } else {
/* Do nothing */ /* Do nothing */
poll(NULL,0, -1); ast_select(0,NULL,NULL,NULL,NULL);
} }
return 0; return 0;
} }

156
channel.c
View File

@@ -20,7 +20,6 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> /* For PI */ #include <math.h> /* For PI */
#include <sys/poll.h>
#include <asterisk/pbx.h> #include <asterisk/pbx.h>
#include <asterisk/frame.h> #include <asterisk/frame.h>
#include <asterisk/sched.h> #include <asterisk/sched.h>
@@ -774,40 +773,32 @@ int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen,
return 0; return 0;
} }
static inline int ast_fdisset(struct pollfd *pfds, int fd, int max)
{
int x;
for (x=0;x<max;x++)
if (pfds[x].fd == fd)
return pfds[x].revents;
return 0;
}
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception) int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
{ {
/* Wait for x amount of time on a file descriptor to have input. */ /* Wait for x amount of time on a file descriptor to have input. */
struct timeval start, now; struct timeval tv;
fd_set rfds, efds;
int res; int res;
int x, y; int x, max=-1;
int winner = -1; int winner = -1;
struct pollfd *pfds;
pfds = alloca(sizeof(struct pollfd) * n); tv.tv_sec = *ms / 1000;
if (!pfds) { tv.tv_usec = (*ms % 1000) * 1000;
ast_log(LOG_WARNING, "alloca failed! bad things will happen.\n"); FD_ZERO(&rfds);
return -1; FD_ZERO(&efds);
}
if (*ms > 0)
gettimeofday(&start, NULL);
y = 0;
for (x=0;x<n;x++) { for (x=0;x<n;x++) {
if (fds[x] > -1) { if (fds[x] > -1) {
pfds[y].fd = fds[x]; FD_SET(fds[x], &rfds);
pfds[y].events = POLLIN | POLLPRI; FD_SET(fds[x], &efds);
y++; if (fds[x] > max)
max = fds[x];
} }
} }
res = poll(pfds, y, *ms); if (*ms >= 0)
res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
else
res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
if (res < 0) { if (res < 0) {
/* Simulate a timeout if we were interrupted */ /* Simulate a timeout if we were interrupted */
if (errno != EINTR) if (errno != EINTR)
@@ -816,29 +807,15 @@ int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
*ms = 0; *ms = 0;
return -1; return -1;
} }
for (x=0;x<n;x++) { for (x=0;x<n;x++) {
if (fds[x] > -1) { if ((fds[x] > -1) && (FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && (winner < 0)) {
if ((res = ast_fdisset(pfds, fds[x], y))) { if (exception)
winner = fds[x]; *exception = FD_ISSET(fds[x], &efds);
if (exception) { winner = fds[x];
if (res & POLLPRI)
*exception = -1;
else
*exception = 0;
}
}
} }
} }
if (*ms > 0) { *ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
long passed;
gettimeofday(&now, NULL);
passed = (now.tv_sec - start.tv_sec) * 1000;
passed += (now.tv_usec - start.tv_usec) / 1000;
if (passed <= *ms)
*ms -= passed;
else
*ms = 0;
}
return winner; return winner;
} }
@@ -846,21 +823,13 @@ struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds,
int *exception, int *outfd, int *ms) int *exception, int *outfd, int *ms)
{ {
/* Wait for x amount of time on a file descriptor to have input. */ /* Wait for x amount of time on a file descriptor to have input. */
struct timeval start, end; struct timeval tv;
struct pollfd *pfds; fd_set rfds, efds;
int res; int res;
long rms; int x, y, max=-1;
int x, y, max;
time_t now = 0; time_t now = 0;
long whentohangup = 0, havewhen = 0, diff; long whentohangup = 0, havewhen = 0, diff;
struct ast_channel *winner = NULL; struct ast_channel *winner = NULL;
pfds = alloca(sizeof(struct pollfd) * (n * AST_MAX_FDS + nfds));
if (!pfds) {
ast_log(LOG_WARNING, "alloca failed! bad things will happen.\n");
*outfd = -1;
return NULL;
}
if (outfd) if (outfd)
*outfd = -99999; *outfd = -99999;
if (exception) if (exception)
@@ -888,35 +857,41 @@ struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds,
} }
ast_mutex_unlock(&c[x]->lock); ast_mutex_unlock(&c[x]->lock);
} }
rms = *ms; tv.tv_sec = *ms / 1000;
tv.tv_usec = (*ms % 1000) * 1000;
if (havewhen) { if (havewhen) {
if ((*ms < 0) || (whentohangup * 1000 < *ms)) { if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
rms = whentohangup * 1000; tv.tv_sec = whentohangup;
tv.tv_usec = 0;
} }
} }
max = 0; FD_ZERO(&rfds);
FD_ZERO(&efds);
for (x=0;x<n;x++) { for (x=0;x<n;x++) {
for (y=0;y<AST_MAX_FDS;y++) { for (y=0;y<AST_MAX_FDS;y++) {
if (c[x]->fds[y] > -1) { if (c[x]->fds[y] > -1) {
pfds[max].fd = c[x]->fds[y]; FD_SET(c[x]->fds[y], &rfds);
pfds[max].events = POLLIN | POLLPRI; FD_SET(c[x]->fds[y], &efds);
max++; if (c[x]->fds[y] > max)
max = c[x]->fds[y];
} }
} }
CHECK_BLOCKING(c[x]); CHECK_BLOCKING(c[x]);
} }
for (x=0;x<nfds; x++) { for (x=0;x<nfds; x++) {
if (fds[x] > -1) { FD_SET(fds[x], &rfds);
pfds[max].fd = fds[x]; FD_SET(fds[x], &efds);
pfds[max].events = POLLIN | POLLPRI; if (fds[x] > max)
max++; max = fds[x];
}
} }
if (*ms > 0) if ((*ms >= 0) || (havewhen))
gettimeofday(&start, NULL); res = ast_select(max + 1, &rfds, NULL, &efds, &tv);
res = poll(pfds, max, rms); else
res = ast_select(max + 1, &rfds, NULL, &efds, NULL);
if (res < 0) { if (res < 0) {
for (x=0;x<n;x++) for (x=0;x<n;x++)
c[x]->blocking = 0; c[x]->blocking = 0;
@@ -943,41 +918,26 @@ struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds,
} }
for (y=0;y<AST_MAX_FDS;y++) { for (y=0;y<AST_MAX_FDS;y++) {
if (c[x]->fds[y] > -1) { if (c[x]->fds[y] > -1) {
if ((res = ast_fdisset(pfds, c[x]->fds[y], max))) { if ((FD_ISSET(c[x]->fds[y], &rfds) || FD_ISSET(c[x]->fds[y], &efds)) && !winner) {
if (res & POLLPRI) /* Set exception flag if appropriate */
if (FD_ISSET(c[x]->fds[y], &efds))
c[x]->exception = 1; c[x]->exception = 1;
else c[x]->fdno = y;
c[x]->exception = 0;
winner = c[x]; winner = c[x];
} }
} }
} }
} }
for (x=0;x<nfds;x++) { for (x=0;x<nfds;x++) {
if (fds[x] > -1) { if ((FD_ISSET(fds[x], &rfds) || FD_ISSET(fds[x], &efds)) && !winner) {
if ((res = ast_fdisset(pfds, fds[x], max))) { if (outfd)
if (outfd) *outfd = fds[x];
*outfd = fds[x]; if (FD_ISSET(fds[x], &efds) && exception)
if (exception) { *exception = 1;
if (res & POLLPRI) winner = NULL;
*exception = -1; }
else
*exception = 1;
}
winner = NULL;
}
}
}
if (*ms > 0) {
long diff;
gettimeofday(&end, NULL);
diff = (end.tv_sec - start.tv_sec) * 1000;
diff += (end.tv_usec - start.tv_usec) * 1000;
if (diff < *ms)
*ms -= diff;
else
*ms = 0;
} }
*ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
return winner; return winner;
} }

View File

@@ -25,7 +25,6 @@
#include <signal.h> #include <signal.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <sys/poll.h>
#include <asterisk/channel.h> #include <asterisk/channel.h>
#include <asterisk/file.h> #include <asterisk/file.h>
#include <asterisk/manager.h> #include <asterisk/manager.h>
@@ -69,7 +68,8 @@ int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
/* Try to write string, but wait no more than ms milliseconds /* Try to write string, but wait no more than ms milliseconds
before timing out */ before timing out */
int res=0; int res=0;
struct pollfd fds[1]; struct timeval tv;
fd_set fds;
while(len) { while(len) {
res = write(fd, s, len); res = write(fd, s, len);
if ((res < 0) && (errno != EAGAIN)) { if ((res < 0) && (errno != EAGAIN)) {
@@ -78,10 +78,12 @@ int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
if (res < 0) res = 0; if (res < 0) res = 0;
len -= res; len -= res;
s += res; s += res;
fds[0].fd = fd; tv.tv_sec = timeoutms / 1000;
fds[0].events = POLLOUT; tv.tv_usec = timeoutms % 1000;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Wait until writable again */ /* Wait until writable again */
res = poll(fds, 1, timeoutms); res = select(fd + 1, NULL, &fds, NULL, &tv);
if (res < 1) if (res < 1)
return -1; return -1;
} }
@@ -713,7 +715,7 @@ static int get_input(struct mansession *s, char *output)
/* output must have at least sizeof(s->inbuf) space */ /* output must have at least sizeof(s->inbuf) space */
int res; int res;
int x; int x;
struct pollfd fds[1]; fd_set fds;
for (x=1;x<s->inlen;x++) { for (x=1;x<s->inlen;x++) {
if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) { if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
/* Copy output data up to and including \r\n */ /* Copy output data up to and including \r\n */
@@ -730,9 +732,9 @@ static int get_input(struct mansession *s, char *output)
ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf); ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf);
s->inlen = 0; s->inlen = 0;
} }
fds[0].fd = s->fd; FD_ZERO(&fds);
fds[0].events = POLLIN; FD_SET(s->fd, &fds);
res = poll(fds, 1, -1); res = ast_select(s->fd + 1, &fds, NULL, NULL, NULL);
if (res < 0) { if (res < 0) {
ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno)); ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
} else if (res > 0) { } else if (res > 0) {