void Init_grpc_c() { if (!grpc_rb_load_core()) { rb_raise(rb_eLoadError, "Couldn't find or load gRPC's dynamic C core"); return; } rb_global_variable(&bg_thread_init_rb_mu); bg_thread_init_rb_mu = rb_mutex_new(); grpc_rb_mGRPC = rb_define_module("GRPC"); grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core"); grpc_rb_sNewServerRpc = rb_struct_define( "NewServerRpc", "method", "host", "deadline", "metadata", "call", NULL); grpc_rb_sStatus = rb_struct_define("Status", "code", "details", "metadata", NULL); sym_code = ID2SYM(rb_intern("code")); sym_details = ID2SYM(rb_intern("details")); sym_metadata = ID2SYM(rb_intern("metadata")); Init_grpc_channel(); Init_grpc_call(); Init_grpc_call_credentials(); Init_grpc_channel_credentials(); Init_grpc_server(); Init_grpc_server_credentials(); Init_grpc_status_codes(); Init_grpc_time_consts(); Init_grpc_compression_options(); }
VALUE renet_connection_initialize(VALUE self, VALUE host, VALUE port, VALUE channels, VALUE download, VALUE upload) { rb_funcall(mENet, rb_intern("initialize"), 0); Connection* connection; /* Now that we're releasing the GIL while waiting for enet_host_service we need a lock to prevent potential segfaults if another thread tries to do an operation on same connection */ VALUE lock = rb_mutex_new(); rb_iv_set(self, "@lock", lock); rb_mutex_lock(lock); Data_Get_Struct(self, Connection, connection); Check_Type(host, T_STRING); if (enet_address_set_host(connection->address, StringValuePtr(host)) != 0) { rb_raise(rb_eStandardError, "Cannot set address"); } connection->address->port = NUM2UINT(port); connection->channels = NUM2UINT(channels); connection->online = 0; connection->host = enet_host_create(NULL, 1, connection->channels, NUM2UINT(download), NUM2UINT(upload)); if (connection->host == NULL) { rb_raise(rb_eStandardError, "Cannot create host"); } rb_iv_set(self, "@total_sent_data", INT2FIX(0)); rb_iv_set(self, "@total_received_data", INT2FIX(0)); rb_iv_set(self, "@total_sent_packets", INT2FIX(0)); rb_iv_set(self, "@total_received_packets", INT2FIX(0)); rb_mutex_unlock(lock); return self; }
VALUE renet_server_initialize(VALUE self, VALUE port, VALUE n_peers, VALUE channels, VALUE download, VALUE upload) { rb_funcall(mENet, rb_intern("initialize"), 0); Server* server; Data_Get_Struct(self, Server, server); VALUE lock = rb_mutex_new(); rb_iv_set(self, "@lock", lock); rb_mutex_lock(lock); server->address->host = ENET_HOST_ANY; server->address->port = NUM2UINT(port); server->channels = NUM2UINT(channels); server->host = enet_host_create(server->address, NUM2UINT(n_peers), server->channels, NUM2UINT(download), NUM2UINT(upload)); if (server->host == NULL) { rb_raise(rb_eStandardError, "Cannot create server"); } rb_iv_set(self, "@total_sent_data", INT2FIX(0)); rb_iv_set(self, "@total_received_data", INT2FIX(0)); rb_iv_set(self, "@total_sent_packets", INT2FIX(0)); rb_iv_set(self, "@total_received_packets", INT2FIX(0)); rb_mutex_unlock(lock); return self; }
static VALUE xthread_monitor_alloc(VALUE klass) { VALUE volatile obj; xthread_monitor_t *mon; obj = TypedData_Make_Struct(klass, xthread_monitor_t, &xthread_monitor_data_type, mon); mon->owner = Qnil; mon->count = 0; mon->mutex = rb_mutex_new(); return obj; }
/* :nodoc: * * Initialize a new instance of Sedna. Undocumented, because Sedna.connect should * be used instead. */ static VALUE cSedna_initialize(VALUE self, VALUE options) { VALUE host_k, db_k, user_k, pw_k, host_v, db_v, user_v, pw_v; char *host, *db, *user, *pw; // Ensure the argument is a Hash. Check_Type(options, T_HASH); // Store the symbols of the valid hash keys. host_k = ID2SYM(rb_intern("host")); db_k = ID2SYM(rb_intern("database")); user_k = ID2SYM(rb_intern("username")); pw_k = ID2SYM(rb_intern("password")); // Get the connection details or set them to the default values if not given. if(NIL_P(host_v = rb_hash_aref(options, host_k))) host = strdup(DEFAULT_HOST); else host = StringValuePtr(host_v); if(NIL_P(db_v = rb_hash_aref(options, db_k ))) db = strdup(DEFAULT_DB); else db = StringValuePtr(db_v); if(NIL_P(user_v = rb_hash_aref(options, user_k))) user = strdup(DEFAULT_USER); else user = StringValuePtr(user_v); if(NIL_P(pw_v = rb_hash_aref(options, pw_k ))) pw = strdup(DEFAULT_PW); else pw = StringValuePtr(pw_v); // Save all connection details to instance variables. rb_iv_set(self, IV_HOST, rb_str_new2(host)); rb_iv_set(self, IV_DB, rb_str_new2(db)); rb_iv_set(self, IV_USER, rb_str_new2(user)); rb_iv_set(self, IV_PW, rb_str_new2(pw)); #ifdef NON_BLOCKING // Create a mutex if this build supports non-blocking queries. rb_iv_set(self, IV_MUTEX, rb_mutex_new()); #endif // Connect to the database. SCA c = { sedna_struct(self), host, db, user, pw }; sedna_connect(self, &c); // Initialize @autocommit to true. rb_iv_set(self, IV_AUTOCOMMIT, Qtrue); return self; }