Skip to content

Commit

Permalink
Detect available features.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 25, 2023
1 parent 53e7a83 commit 61269c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 29 additions & 2 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
#include <ruby/encoding.h>
#include <ruby/io.h>
#include <ruby/thread.h>

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <fcntl.h>

#include <trilogy.h>

#include "trilogy-ruby.h"

#define TRILOGY_RB_TIMEOUT 1

#if defined(HAVE_RB_IO_WAIT) && defined(HAVE_RB_GC_MARK_MOVABLE)
#define TRILOGY_RB_IO_WAIT
#endif

#ifdef TRILOGY_RB_IO_WAIT
#include <ruby/fiber/scheduler.h>
#endif

VALUE Trilogy_CastError;
static VALUE Trilogy_BaseConnectionError, Trilogy_ProtocolError, Trilogy_SSLError, Trilogy_QueryError,
Trilogy_ConnectionClosedError, Trilogy_TimeoutError, Trilogy_Result;
Expand All @@ -26,7 +36,9 @@ static ID id_socket, id_host, id_port, id_username, id_password, id_found_rows,

struct trilogy_ctx {
trilogy_conn_t conn;
#ifdef TRILOGY_RB_IO_WAIT
VALUE io;
#endif

char server_version[TRILOGY_SERVER_VERSION_SIZE + 1];
unsigned int query_flags;
Expand All @@ -53,6 +65,7 @@ static size_t trilogy_ctx_memsize(const void *ptr) {
return memsize;
}

#ifdef TRILOGY_RB_IO_WAIT
static void trilogy_ctx_mark(void *ptr)
{
struct trilogy_ctx *ctx = ptr;
Expand All @@ -68,12 +81,15 @@ static void trilogy_ctx_compact(void *ptr)
if (RTEST(ctx->io))
ctx->io = rb_gc_location(ctx->io);
}
#endif

const rb_data_type_t trilogy_data_type = {
.wrap_struct_name = "trilogy",
.function = {
#ifdef TRILOGY_RB_IO_WAIT
.dmark = trilogy_ctx_mark,
.dcompact = trilogy_ctx_compact,
#endif
.dfree = trilogy_ctx_free,
.dsize = trilogy_ctx_memsize,
},
Expand Down Expand Up @@ -157,7 +173,10 @@ static VALUE allocate_trilogy(VALUE klass)

VALUE obj = TypedData_Make_Struct(klass, struct trilogy_ctx, &trilogy_data_type, ctx);

#ifdef TRILOGY_RB_IO_WAIT
ctx->io = Qnil;
#endif

ctx->query_flags = TRILOGY_FLAGS_DEFAULT;

if (trilogy_init(&ctx->conn) < 0) {
Expand Down Expand Up @@ -234,8 +253,11 @@ static int _cb_ruby_wait(trilogy_sock_t *sock, trilogy_wait_t wait)
}

int fd = trilogy_sock_fd(sock);

#ifdef TRILOGY_RB_IO_WAIT
if (ctx->io == Qnil) {
ctx->io = rb_io_fdopen(fd, 0, "trilogy_socket");
ctx->io = rb_io_fdopen(fd, O_RDWR, NULL);
rb_io_
}

VALUE result = rb_io_wait(ctx->io, RB_INT2NUM(wait_flag), rb_fiber_scheduler_make_timeout(timeout));
Expand All @@ -244,6 +266,11 @@ static int _cb_ruby_wait(trilogy_sock_t *sock, trilogy_wait_t wait)
} else {
return TRILOGY_SYSERR;
}
#else
if (rb_wait_for_single_fd(fd, wait_flag, timeout) <= 0)
return TRILOGY_SYSERR;
return 0;
#endif
}

struct nogvl_sock_args {
Expand Down Expand Up @@ -990,7 +1017,7 @@ static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx

static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }

void Init_cext()
void Init_cext(void)
{
VALUE Trilogy = rb_const_get(rb_cObject, rb_intern("Trilogy"));
rb_define_alloc_func(Trilogy, allocate_trilogy);
Expand Down
3 changes: 3 additions & 0 deletions contrib/ruby/ext/trilogy-ruby/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
have_library("ssl", "SSL_new")
have_func("rb_interned_str", "ruby.h")

have_func("rb_gc_mark_movable", "ruby.h")
have_func("rb_io_wait", "ruby.h")

create_makefile "trilogy/cext"

0 comments on commit 61269c1

Please sign in to comment.