static void
ls_arg_free(void *p) {
    ls_arg *args = p;
    if (args) {
        rb_fd_term(&args->in);
        rb_fd_term(&args->out);
        xfree(args);
    }
}
Esempio n. 2
0
static void
rb_thread_wait_fd_rw(int fd, int read)
{
    int result = 0;

    while (result <= 0) {
	rb_fdset_t *set = ALLOC(rb_fdset_t);
	rb_fd_init(set);
	FD_SET(fd, set);

	if (read) {
	    result = do_select(fd + 1, rb_fd_ptr(set), 0, 0, 0);
	}
	else {
	    result = do_select(fd + 1, 0, rb_fd_ptr(set), 0, 0);
	}

	rb_fd_term(set);

	if (result < 0) {
	    rb_sys_fail(0);
	}
    }
}
Esempio n. 3
0
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;
  rb_fd_init(&rset);
  rb_fd_set(socket_fd, &rset);

  while (1) {
    retval = rb_thread_fd_select(socket_fd + 1, &rset, NULL, NULL, NULL);

    if (retval < 0) {
      rb_fd_term(&rset);
      rb_sys_fail(0);
    }

    if (retval == 0) {
      continue;
    }

    if (PQconsumeInput(db) == 0) {
      rb_fd_term(&rset);
      rb_raise(eDO_ConnectionError, "%s", PQerrorMessage(db));
    }

    if (PQisBusy(db) == 0) {
      break;
    }
  }

  rb_fd_term(&rset);
  data_objects_debug(connection, query, &start);
  return PQgetResult(db);
}