static void next_event(int new_st, int status, struct state_data *sd) { short wait_event= 0; struct timeval tv, *ptv; int fd; if (status & MYSQL_WAIT_READ) wait_event|= EV_READ; if (status & MYSQL_WAIT_WRITE) wait_event|= EV_WRITE; if (wait_event) fd= mysql_get_socket(&sd->mysql); else fd= -1; if (status & MYSQL_WAIT_TIMEOUT) { tv.tv_sec= mysql_get_timeout_value(&sd->mysql); tv.tv_usec= 0; ptv= &tv; } else ptv= NULL; event_set(&sd->ev_mysql, fd, wait_event, state_machine_handler, sd); event_add(&sd->ev_mysql, ptv); sd->ST= new_st; }
static int wait_for_mysql(MYSQL *mysql, int status) { #ifdef _WIN32 fd_set rs, ws, es; int res; struct timeval tv, *timeout; my_socket s= mysql_get_socket(mysql); FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es); if (status & MYSQL_WAIT_READ) FD_SET(s, &rs); if (status & MYSQL_WAIT_WRITE) FD_SET(s, &ws); if (status & MYSQL_WAIT_EXCEPT) FD_SET(s, &es); if (status & MYSQL_WAIT_TIMEOUT) { tv.tv_sec= mysql_get_timeout_value(mysql); tv.tv_usec= 0; timeout= &tv; } else timeout= NULL; res= select(1, &rs, &ws, &es, timeout); if (res == 0) return MYSQL_WAIT_TIMEOUT; else if (res == SOCKET_ERROR) { /* In a real event framework, we should handle errors and re-try the select. */ return MYSQL_WAIT_TIMEOUT; } else { int status= 0; if (FD_ISSET(s, &rs)) status|= MYSQL_WAIT_READ; if (FD_ISSET(s, &ws)) status|= MYSQL_WAIT_WRITE; if (FD_ISSET(s, &es)) status|= MYSQL_WAIT_EXCEPT; return status; } #else struct pollfd pfd; int timeout; int res= -1; pfd.fd= mysql_get_socket(mysql); pfd.events= (status & MYSQL_WAIT_READ ? POLLIN : 0) | (status & MYSQL_WAIT_WRITE ? POLLOUT : 0) | (status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0); if (status & MYSQL_WAIT_TIMEOUT) timeout= mysql_get_timeout_value_ms(mysql); else timeout= -1; do { res= poll(&pfd, 1, timeout); } while (res == -1 && errno == EINTR); if (res == 0) return MYSQL_WAIT_TIMEOUT; else if (res < 0) { /* In a real event framework, we should handle EINTR and re-try the poll. */ return MYSQL_WAIT_TIMEOUT; } else { int status= 0; if (pfd.revents & POLLIN) status|= MYSQL_WAIT_READ; if (pfd.revents & POLLOUT) status|= MYSQL_WAIT_WRITE; if (pfd.revents & POLLPRI) status|= MYSQL_WAIT_EXCEPT; return status; } #endif }