/*
 * call-seq:
 *    conn.async_exec(sql [, params, result_format ] ) -> PGresult
 *    conn.async_exec(sql [, params, result_format ] ) {|pg_result| block }
 *
 * This function has the same behavior as +PGconn#exec+,
 * except that it's implemented using asynchronous command 
 * processing and ruby's +rb_thread_select+ in order to 
 * allow other threads to process while waiting for the
 * server to complete the request.
 */
static VALUE
pgconn_async_exec(int argc, VALUE *argv, VALUE self)
{
        VALUE rb_pgresult = Qnil;

        /* remove any remaining results from the queue */
        pgconn_block( 0, NULL, self ); /* wait for input (without blocking) before reading the last result */
        pgconn_get_last_result( self );

        pgconn_send_query( argc, argv, self );
        pgconn_block( 0, NULL, self );
        rb_pgresult = pgconn_get_last_result( self );

        if ( rb_block_given_p() ) {
                return rb_ensure( rb_yield, rb_pgresult, pgresult_clear, rb_pgresult );
        }
        return rb_pgresult;
}