PGresult * do_postgres_cCommand_execute_async(VALUE self, VALUE connection, PGconn *db, VALUE query) { PGresult *response; char* str = StringValuePtr(query); while ((response = PQgetResult(db))) { PQclear(response); } struct timeval start; int retval; gettimeofday(&start, NULL); retval = PQsendQuery(db, str); if (!retval) { if (PQstatus(db) != CONNECTION_OK) { PQreset(db); if (PQstatus(db) == CONNECTION_OK) { retval = PQsendQuery(db, str); } else { do_postgres_full_connect(connection, db); retval = PQsendQuery(db, str); } } if (!retval) { rb_raise(eDO_ConnectionError, "%s", PQerrorMessage(db)); } } int socket_fd = PQsocket(db); rb_fdset_t rset; while (1) { rb_fd_init(&rset); rb_fd_set(socket_fd, &rset); retval = rb_thread_fd_select(socket_fd + 1, &rset, NULL, NULL, NULL); if (retval < 0) { rb_sys_fail(0); } if (retval == 0) { continue; } if (PQconsumeInput(db) == 0) { rb_raise(eDO_ConnectionError, "%s", PQerrorMessage(db)); } if (PQisBusy(db) == 0) { break; } } data_objects_debug(connection, query, &start); return PQgetResult(db); }
static VALUE dnssd_service_process(VALUE self) { DNSServiceRef *client; int dnssd_fd, result; rb_fdset_t read; struct timeval timeout; get(cDNSSDService, self, DNSServiceRef, client); if (client == NULL) { /* looks like this thread has already been stopped */ return Qnil; } dnssd_fd = DNSServiceRefSockFD(*client); if (-1 == dnssd_fd) rb_raise(eDNSSDError, "unable to get DNSSD FD for result processing"); timeout.tv_sec = 0; timeout.tv_usec = 10000; rb_fd_init(&read); retry: rb_fd_zero(&read); rb_fd_set(dnssd_fd, &read); result = rb_thread_fd_select(dnssd_fd + 1, &read, NULL, NULL, &timeout); if (result == -1) rb_sys_fail("select"); if (rb_ivar_get(self, dnssd_iv_continue) == Qfalse) return Qnil; /* timeout */ if (result == 0) goto retry; DNSServiceErrorType e = DNSServiceProcessResult(*client); dnssd_check_error_code(e); return self; }
static VALUE loop_run_select(VALUE argp) { ls_arg *args = (ls_arg*) argp; rb_mt_loop *loop = args->loop; rb_fdset_t *in = NULL, *out = NULL; struct timeval timeout; struct timeval *timeoutp = NULL; int result, max = 0; hrtime_t now, next_time; next_time = timers_minimum(&loop->timers); if (next_time) { now = gethrtime(); if (next_time <= now) { timeout.tv_sec = 0; timeout.tv_usec = 0; } else { hrtime_t hrto = (next_time - now) / 1000; timeout.tv_sec = (long)(hrto / 1000000); timeout.tv_usec = (long)(hrto % 1000000); } timeoutp = &timeout; } if (loop->events.count) { uint32_t i; rb_fd_init(&args->in); rb_fd_init(&args->out); for(i = 0; i < loop->events.count; i++) { rb_mt_socket_list *list = &loop->events.sockets[i]; if (list->flags & LCB_READ_EVENT) { in = &args->in; rb_fd_set(list->socket, in); } if (list->flags & LCB_WRITE_EVENT) { out = &args->out; rb_fd_set(list->socket, out); } } max = events_max_fd(&loop->events) + 1; } result = rb_thread_fd_select(max, in, out, NULL, timeoutp); if (result < 0) { rb_sys_fail("rb_thread_fd_select"); } /* fix current time so that socket callbacks will not cause timers timeouts */ if (next_time) { now = gethrtime(); } if (result > 0) { uint32_t i; for(i = 0; i < loop->events.count && result; i++) { rb_mt_socket_list *list = loop->events.sockets + i; rb_mt_event *sock = list->first; short flags = 0; if (in && rb_fd_isset(list->socket, in)) { flags |= LCB_READ_EVENT; result--; } if (out && rb_fd_isset(list->socket, out)) { flags |= LCB_WRITE_EVENT; result--; } if (flags) { loop_enque_events(&loop->callbacks, sock, flags); } } callbacks_run(&loop->callbacks); } if (next_time) { timers_run(&loop->timers, now); } if (loop->events.count == 0 && loop->timers.count == 0) { loop->run = 0; } return Qnil; }
static VALUE fcgi_s_accept(VALUE self) { int status; FCGX_Request *req; rb_fdset_t readfds; req = ALLOC(FCGX_Request); status = FCGX_InitRequest(req,0,0); if (status != 0) { rb_raise(eFCGIError, "FCGX_Init() failed"); return Qnil; } rb_fd_init(&readfds); rb_fd_set(req->listen_sock, &readfds); if (rb_thread_fd_select(readfds.maxfd, &readfds, NULL, NULL, NULL) < 1) { return Qnil; } status = FCGX_Accept_r(req); if (status >= 0) { fcgi_data *data; fcgi_stream_data *stream_data; char **env; VALUE obj,key, value; char *pkey,*pvalue; int flags, fd; /* Unset NONBLOCKING */ fd = ((FCGX_Request*) req)->ipcFd; flags = fcntl(fd, F_GETFL); if (flags & O_NONBLOCK) { fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); } obj = Data_Make_Struct(self, fcgi_data, fcgi_mark, fcgi_free_req, data); data->req = req; data->in = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data); stream_data->stream = req->in; stream_data->req = obj; data->out = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data); stream_data->stream = req->out; stream_data->req = obj; data->err = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data); stream_data->stream = req->err; stream_data->req = obj; data->env = rb_hash_new(); env = req->envp; for (; *env; env++) { int size = 0; pkey = *env; pvalue = pkey; while( *(pvalue++) != '=') size++; key = rb_str_new(pkey, size); value = rb_str_new2(pvalue); OBJ_TAINT(key); OBJ_TAINT(value); rb_hash_aset(data->env, key, value); } return obj; } else { FCGX_Free(req, 1); free(req); return Qnil; } }