Skip to content

Commit

Permalink
Ref (#175)
Browse files Browse the repository at this point in the history
* Updates crt native code against new ref-counting APIs
  • Loading branch information
bretambrose authored Sep 8, 2020
1 parent 5d19b1f commit 297d89b
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion aws-common-runtime/aws-c-common
Submodule aws-c-common updated 181 files
32 changes: 20 additions & 12 deletions source/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ PyObject *aws_py_is_alpn_available(PyObject *self, PyObject *args) {
******************************************************************************/

struct event_loop_group_binding {
struct aws_event_loop_group native;
struct aws_event_loop_group *native;

/* Dependencies that must outlive this */
PyObject *shutdown_complete;
Expand Down Expand Up @@ -122,7 +122,7 @@ static void s_elg_capsule_destructor(PyObject *elg_capsule) {

/* Must use async cleanup.
* We could deadlock if we ran the synchronous cleanup from an event-loop thread. */
aws_event_loop_group_clean_up_async(&elg_binding->native, s_elg_native_cleanup_complete, elg_binding);
aws_event_loop_group_release(elg_binding->native);
}

PyObject *aws_py_event_loop_group_new(PyObject *self, PyObject *args) {
Expand All @@ -141,7 +141,13 @@ PyObject *aws_py_event_loop_group_new(PyObject *self, PyObject *args) {
return PyErr_AwsLastError();
}

if (aws_event_loop_group_default_init(&binding->native, allocator, num_threads)) {
struct aws_shutdown_callback_options shutdown_options = {
.shutdown_callback_fn = s_elg_native_cleanup_complete,
.shutdown_callback_user_data = binding,
};

binding->native = aws_event_loop_group_new_default(allocator, num_threads, &shutdown_options);
if (binding->native == NULL) {
PyErr_SetAwsLastError();
goto elg_init_failed;
}
Expand All @@ -158,23 +164,22 @@ PyObject *aws_py_event_loop_group_new(PyObject *self, PyObject *args) {
return capsule;

capsule_new_failed:
aws_event_loop_group_clean_up(&binding->native);
aws_event_loop_group_release(binding->native);
elg_init_failed:
aws_mem_release(allocator, binding);
return NULL;
}

struct aws_event_loop_group *aws_py_get_event_loop_group(PyObject *event_loop_group) {
AWS_PY_RETURN_NATIVE_REF_FROM_BINDING(
event_loop_group, s_capsule_name_elg, "EventLoopGroup", event_loop_group_binding);
AWS_PY_RETURN_NATIVE_FROM_BINDING(event_loop_group, s_capsule_name_elg, "EventLoopGroup", event_loop_group_binding);
}

/*******************************************************************************
* AWS_HOST_RESOLVER
******************************************************************************/

struct host_resolver_binding {
struct aws_host_resolver native;
struct aws_host_resolver *native;

/* Dependencies that must outlive this */
PyObject *event_loop_group;
Expand All @@ -184,7 +189,7 @@ static void s_host_resolver_destructor(PyObject *host_resolver_capsule) {
struct host_resolver_binding *host_resolver =
PyCapsule_GetPointer(host_resolver_capsule, s_capsule_name_host_resolver);
assert(host_resolver);
aws_host_resolver_clean_up(&host_resolver->native);
aws_host_resolver_release(host_resolver->native);
Py_DECREF(host_resolver->event_loop_group);
aws_mem_release(aws_py_get_allocator(), host_resolver);
}
Expand Down Expand Up @@ -218,7 +223,8 @@ PyObject *aws_py_host_resolver_new_default(PyObject *self, PyObject *args) {

/* From hereon, we need to clean up if errors occur */

if (aws_host_resolver_init_default(&host_resolver->native, allocator, max_hosts, elg)) {
host_resolver->native = aws_host_resolver_new_default(allocator, max_hosts, elg, NULL);
if (host_resolver->native == NULL) {
PyErr_SetAwsLastError();
goto resolver_init_failed;
}
Expand All @@ -235,14 +241,14 @@ PyObject *aws_py_host_resolver_new_default(PyObject *self, PyObject *args) {
return capsule;

capsule_new_failed:
aws_host_resolver_clean_up(&host_resolver->native);
aws_host_resolver_release(host_resolver->native);
resolver_init_failed:
aws_mem_release(allocator, host_resolver);
return NULL;
}

struct aws_host_resolver *aws_py_get_host_resolver(PyObject *host_resolver) {
AWS_PY_RETURN_NATIVE_REF_FROM_BINDING(
AWS_PY_RETURN_NATIVE_FROM_BINDING(
host_resolver, s_capsule_name_host_resolver, "HostResolverBase", host_resolver_binding);
}

Expand Down Expand Up @@ -375,7 +381,7 @@ static void s_tls_ctx_destructor(PyObject *tls_ctx_capsule) {
struct aws_tls_ctx *tls_ctx = PyCapsule_GetPointer(tls_ctx_capsule, s_capsule_name_tls_ctx);
assert(tls_ctx);

aws_tls_ctx_destroy(tls_ctx);
aws_tls_ctx_release(tls_ctx);
}

PyObject *aws_py_client_tls_ctx_new(PyObject *self, PyObject *args) {
Expand Down Expand Up @@ -480,6 +486,8 @@ PyObject *aws_py_client_tls_ctx_new(PyObject *self, PyObject *args) {
return capsule;

capsule_new_failure:
aws_tls_ctx_release(tls_ctx);

ctx_options_failure:
aws_tls_ctx_options_clean_up(&ctx_options);
return NULL;
Expand Down
12 changes: 6 additions & 6 deletions source/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
static const char *s_capsule_name_mqtt_client = "aws_mqtt_client";

struct mqtt_client_binding {
struct aws_mqtt_client native;
struct aws_mqtt_client *native;

/* Dependencies that must outlive this */
PyObject *bootstrap;
Expand All @@ -21,7 +21,7 @@ static void s_mqtt_python_client_destructor(PyObject *client_capsule) {
struct mqtt_client_binding *client = PyCapsule_GetPointer(client_capsule, s_capsule_name_mqtt_client);
assert(client);

aws_mqtt_client_clean_up(&client->native);
aws_mqtt_client_release(client->native);
Py_DECREF(client->bootstrap);
Py_DECREF(client->tls_ctx);
aws_mem_release(aws_py_get_allocator(), client);
Expand Down Expand Up @@ -50,8 +50,8 @@ PyObject *aws_py_mqtt_client_new(PyObject *self, PyObject *args) {
}

/* From hereon, we need to clean up if errors occur */

if (aws_mqtt_client_init(&client->native, allocator, bootstrap)) {
client->native = aws_mqtt_client_new(allocator, bootstrap);
if (client->native == NULL) {
PyErr_SetAwsLastError();
goto client_init_failed;
}
Expand All @@ -70,12 +70,12 @@ PyObject *aws_py_mqtt_client_new(PyObject *self, PyObject *args) {
return capsule;

capsule_new_failed:
aws_mqtt_client_clean_up(&client->native);
aws_mqtt_client_release(client->native);
client_init_failed:
aws_mem_release(allocator, client);
return NULL;
}

struct aws_mqtt_client *aws_py_get_mqtt_client(PyObject *mqtt_client) {
AWS_PY_RETURN_NATIVE_REF_FROM_BINDING(mqtt_client, s_capsule_name_mqtt_client, "Client", mqtt_client_binding);
AWS_PY_RETURN_NATIVE_FROM_BINDING(mqtt_client, s_capsule_name_mqtt_client, "Client", mqtt_client_binding);
}
4 changes: 2 additions & 2 deletions source/mqtt_client_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct mqtt_connection_binding {
};

static void s_mqtt_python_connection_finish_destruction(struct mqtt_connection_binding *py_connection) {
aws_mqtt_client_connection_destroy(py_connection->native);
aws_mqtt_client_connection_release(py_connection->native);

Py_DECREF(py_connection->self_proxy);
Py_DECREF(py_connection->client);
Expand Down Expand Up @@ -230,7 +230,7 @@ PyObject *aws_py_mqtt_client_connection_new(PyObject *self, PyObject *args) {
proxy_new_failed:
use_websockets_failed:
set_interruption_failed:
aws_mqtt_client_connection_destroy(py_connection->native);
aws_mqtt_client_connection_release(py_connection->native);
connection_new_failed:
aws_mem_release(allocator, py_connection);
return NULL;
Expand Down

0 comments on commit 297d89b

Please sign in to comment.