Skip to content

Commit

Permalink
Use a dedicated event base per libcouchbase instance
Browse files Browse the repository at this point in the history
Previously all of the instances shared a single (the global) event base
instance, so it wouldn't work very well in a multithreaded application.
  • Loading branch information
trondn authored and janl committed Sep 6, 2011
1 parent cccc3c4 commit d453352
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions couchbase.c
Expand Up @@ -106,6 +106,9 @@ static void php_couchbase_instance_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
}
libcouchbase_destroy(php_instance->instance);
}
if (php_instance->evbase) {
event_base_free(php_instance->evbase);
}
efree(php_instance);
}
}
Expand Down Expand Up @@ -166,7 +169,7 @@ long map_error_constant(libcouchbase_error_t error)
case LIBCOUCHBASE_UNKNOWN_COMMAND: return COUCHBASE_UNKNOWN_COMMAND;
case LIBCOUCHBASE_UNKNOWN_HOST: return COUCHBASE_UNKNOWN_HOST;
}
}
}

// functions

Expand All @@ -192,22 +195,30 @@ PHP_FUNCTION(couchbase_create)
}

struct event_base *base;
struct event_base *evbase = event_init();
struct event_base *evbase = event_base_new();
if (evbase == NULL) {
php_printf("Failed to create event base for libcouchbase\n");
RETURN_FALSE;
}

libcouchbase_t instance = libcouchbase_create(host, user,
passwd, bucket, evbase);
if (instance == NULL) {
event_base_free(evbase);
php_printf("Failed to create libcouchbase instance\n");
RETURN_FALSE;
}

if (libcouchbase_connect(instance) != LIBCOUCHBASE_SUCCESS) {
event_base_free(evbase);
php_printf("Failed to connect libcouchbase instance to server\n");
RETURN_FALSE;
}

php_couchbase_instance *php_instance;
php_instance = emalloc(sizeof(php_couchbase_instance));
php_instance->instance = instance;
php_instance->evbase = evbase;

ZEND_REGISTER_RESOURCE(return_value, php_instance, le_couchbase_instance);

Expand Down
1 change: 1 addition & 0 deletions php_couchbase.h
Expand Up @@ -21,6 +21,7 @@ PHP_FUNCTION(couchbase_set_get_callback);

typedef struct _php_couchbase_instance {
libcouchbase_t instance;
void *evbase;
} php_couchbase_instance;

typedef struct _php_couchbase_callbacks {
Expand Down

0 comments on commit d453352

Please sign in to comment.