Don't continuously toggle fd blocking mode

This commit is contained in:
Jeremy Lakeman 2016-03-29 16:35:48 +10:30
parent 256fdb11c9
commit c04d015bc6
7 changed files with 6 additions and 18 deletions

View File

@ -62,7 +62,6 @@ static void process_command(char *line, struct cli_schema *cli_commands){
static void read_lines(struct sched_ent *alarm){
struct command_state *state=(struct command_state *)alarm;
set_nonblock(alarm->poll.fd);
ssize_t bytes = read(alarm->poll.fd, state->line_buff + state->line_pos, sizeof(state->line_buff) - state->line_pos);
if (bytes<=0){
// EOF?
@ -70,7 +69,6 @@ static void read_lines(struct sched_ent *alarm){
alarm->poll.fd=-1;
return;
}
set_block(alarm->poll.fd);
size_t i = state->line_pos;
size_t processed=0;
state->line_pos+=bytes;

View File

@ -252,6 +252,7 @@ int _watch(struct __sourceloc __whence, struct sched_ent *alarm)
DEBUGF(io, "Adding watch %s, #%d for %s", alloca_alarm_name(alarm), alarm->poll.fd, alloca_poll_events(alarm->poll.events));
if (fdcount>=MAX_WATCHED_FDS)
return WHY("Too many file handles to watch");
set_nonblock(alarm->poll.fd);
fd_callbacks[fdcount]=alarm;
alarm->poll.revents = 0;
alarm->_poll_index=fdcount;
@ -425,18 +426,11 @@ int fd_poll2(time_ms_t (*waiting)(time_ms_t, time_ms_t, time_ms_t), void (*wokeu
for(i=fdcount -1;i>=0;i--){
if (fd_callbacks[i] && fd_callbacks[i]->poll.fd == fds[i].fd && fds[i].revents) {
errno=0;
int fd = fds[i].fd;
set_nonblock(fd);
// Work around OSX behaviour that doesn't set POLLERR on
// devices that have been deconfigured, e.g., a USB serial adapter
// that has been removed.
if (errno == ENXIO) fds[i].revents|=POLLERR;
call_alarm(fd_callbacks[i], fds[i].revents);
// The alarm may have closed and unwatched the descriptor, make sure this descriptor still matches
if (i<fdcount && fds[i].fd == fd){
if (set_block(fds[i].fd))
FATALF("Alarm %p %s has a bad descriptor that wasn't closed!", fd_callbacks[i], alloca_alarm_name(fd_callbacks[i]));
}
}
}
// time may have passed while processing IO, or processing IO could trigger a new overdue alarm

View File

@ -243,6 +243,7 @@ void httpd_server_poll(struct sched_ent *alarm)
if (errno && errno != EAGAIN)
WARN_perror("accept");
} else {
set_nonblock(sock);
++http_request_uuid_counter;
strbuf_sprintf(&log_context, "httpd/%u", http_request_uuid_counter);
struct sockaddr_in *peerip=NULL;

View File

@ -216,9 +216,7 @@ int overlay_mdp_send(int mdp_sockfd, overlay_mdp_frame *mdp, int flags, int time
if (make_local_sockaddr(&addr, "mdp.socket") == -1)
return WHY("Failed to make socket address");
// Send to that socket
set_nonblock(mdp_sockfd);
ssize_t result = sendto(mdp_sockfd, mdp, (size_t)len, 0, &addr.addr, addr.addrlen);
set_block(mdp_sockfd);
if ((size_t)result != (size_t)len) {
if (result == -1)
WHYF_perror("sendto(fd=%d,len=%zu,addr=%s)", mdp_sockfd, (size_t)len, alloca_socket_address(&addr));
@ -322,10 +320,8 @@ int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, mdp_port_t port, in
recvaddr.addrlen = sizeof recvaddr.store;
ssize_t len;
mdp->packetTypeAndFlags = 0;
set_nonblock(mdp_sockfd);
len = recv_message(mdp_sockfd, &recvaddr, ttl, (unsigned char *)mdp, sizeof(overlay_mdp_frame));
set_block(mdp_sockfd);
if (len <= 0)
return -1; // no packet received

View File

@ -662,10 +662,7 @@ int monitor_tell_clients(char *msg, int msglen, int mask)
for(i=monitor_socket_count -1;i>=0;i--) {
if (monitor_sockets[i].flags & mask) {
// DEBUG("Writing AUDIOPACKET to client");
if ( set_nonblock(monitor_sockets[i].alarm.poll.fd) == -1
|| write_all_nonblock(monitor_sockets[i].alarm.poll.fd, msg, msglen) == -1
|| set_block(monitor_sockets[i].alarm.poll.fd) == -1
) {
if ( write_all_nonblock(monitor_sockets[i].alarm.poll.fd, msg, msglen) == -1) {
INFOF("Tearing down monitor client #%d", i);
monitor_close(&monitor_sockets[i]);
}

View File

@ -215,6 +215,8 @@ overlay_bind_socket(const struct socket_address *addr){
if (socket_bind(fd, addr))
goto error;
set_nonblock(fd);
return fd;
@ -1070,7 +1072,6 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o
return -1;
}
}
set_block(interface->alarm.poll.fd);
ob_free(buffer);
return 0;
}

View File

@ -396,6 +396,7 @@ static int app_vomp_console(const struct cli_parsed *parsed, struct cli_context
monitor_client_writeline(monitor_alarm.poll.fd, "monitor vomp %d\n",
VOMP_CODEC_TEXT);
set_nonblock(STDIN_FILENO);
set_nonblock(monitor_alarm.poll.fd);
watch(&monitor_alarm);