centralize closing and freeing logic for connections

This commit is contained in:
Brian Lopez 2010-10-17 16:34:49 -07:00
parent ba4f3612b6
commit 0a7e7ee475
1 changed files with 37 additions and 39 deletions

View File

@ -104,8 +104,10 @@ static VALUE nogvl_connect(void *ptr) {
return client ? Qtrue : Qfalse;
}
static void rb_mysql_client_free(void * ptr) {
mysql_client_wrapper *wrapper = (mysql_client_wrapper *)ptr;
static VALUE nogvl_close(void *ptr) {
mysql_client_wrapper *wrapper = ptr;
if (!wrapper->closed) {
wrapper->closed = 1;
/*
* we'll send a QUIT message to the server, but that message is more of a
@ -119,39 +121,35 @@ static void rb_mysql_client_free(void * ptr) {
* if the socket is dead we have no chance of blocking,
* so ignore any potential fcntl errors since they don't matter
*/
#ifndef _WIN32
#ifndef _WIN32
int flags = fcntl(fd, F_GETFL);
if (flags > 0 && !(flags & O_NONBLOCK))
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#else
#else
u_long iMode = 1;
ioctlsocket(fd, FIONBIO, &iMode);
#endif
#endif
}
/* It's safe to call mysql_close() on an already closed connection. */
if (!wrapper->closed) {
mysql_close(wrapper->client);
if (!wrapper->freed) {
free(wrapper->client);
}
}
xfree(ptr);
}
static VALUE nogvl_close(void * ptr) {
mysql_client_wrapper *wrapper = ptr;
if (!wrapper->closed) {
wrapper->closed = 1;
mysql_close(wrapper->client);
wrapper->client->net.fd = -1;
}
if (!wrapper->freed) {
wrapper->freed = 1;
free(wrapper->client);
}
}
return Qnil;
}
static void rb_mysql_client_free(void * ptr) {
mysql_client_wrapper *wrapper = (mysql_client_wrapper *)ptr;
nogvl_close(wrapper);
xfree(ptr);
}
static VALUE allocate(VALUE klass) {
VALUE obj;
mysql_client_wrapper * wrapper;