[glib-networking/wip/pwithnall/dtls: 8/24] tlsinputstream: Use thread-safe GWeakRef
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib-networking/wip/pwithnall/dtls: 8/24] tlsinputstream: Use thread-safe GWeakRef
- Date: Wed, 7 Oct 2015 13:23:52 +0000 (UTC)
commit dfe002c38f514fdb79dd01aec04c5255fc6fd6ab
Author: Olivier CrĂȘte <olivier crete collabora com>
Date: Sun Aug 31 12:59:13 2014 -0400
tlsinputstream: Use thread-safe GWeakRef
https://bugzilla.gnome.org/show_bug.cgi?id=735754
tls/gnutls/gtlsinputstream-gnutls.c | 70 +++++++++++++++++++++++-----------
1 files changed, 47 insertions(+), 23 deletions(-)
---
diff --git a/tls/gnutls/gtlsinputstream-gnutls.c b/tls/gnutls/gtlsinputstream-gnutls.c
index 38292ed..c6789ea 100644
--- a/tls/gnutls/gtlsinputstream-gnutls.c
+++ b/tls/gnutls/gtlsinputstream-gnutls.c
@@ -31,7 +31,7 @@ G_DEFINE_TYPE_WITH_CODE (GTlsInputStreamGnutls, g_tls_input_stream_gnutls, G_TYP
struct _GTlsInputStreamGnutlsPrivate
{
- GTlsConnectionGnutls *conn;
+ GWeakRef weak_conn;
};
static void
@@ -39,16 +39,21 @@ g_tls_input_stream_gnutls_dispose (GObject *object)
{
GTlsInputStreamGnutls *stream = G_TLS_INPUT_STREAM_GNUTLS (object);
- if (stream->priv->conn)
- {
- g_object_remove_weak_pointer (G_OBJECT (stream->priv->conn),
- (gpointer *)&stream->priv->conn);
- stream->priv->conn = NULL;
- }
+ g_weak_ref_set (&stream->priv->weak_conn, NULL);
G_OBJECT_CLASS (g_tls_input_stream_gnutls_parent_class)->dispose (object);
}
+static void
+g_tls_input_stream_gnutls_finalize (GObject *object)
+{
+ GTlsInputStreamGnutls *stream = G_TLS_INPUT_STREAM_GNUTLS (object);
+
+ g_weak_ref_clear (&stream->priv->weak_conn);
+
+ G_OBJECT_CLASS (g_tls_input_stream_gnutls_parent_class)->finalize (object);
+}
+
static gssize
g_tls_input_stream_gnutls_read (GInputStream *stream,
void *buffer,
@@ -57,22 +62,33 @@ g_tls_input_stream_gnutls_read (GInputStream *stream,
GError **error)
{
GTlsInputStreamGnutls *tls_stream = G_TLS_INPUT_STREAM_GNUTLS (stream);
+ GTlsConnectionGnutls *conn;
+ gssize ret;
- g_return_val_if_fail (tls_stream->priv->conn != NULL, -1);
+ conn = g_weak_ref_get (&tls_stream->priv->weak_conn);
+ g_return_val_if_fail (conn != NULL, -1);
- return g_tls_connection_gnutls_read (tls_stream->priv->conn,
- buffer, count, TRUE,
- cancellable, error);
+ ret = g_tls_connection_gnutls_read (conn,
+ buffer, count, TRUE,
+ cancellable, error);
+ g_object_unref (conn);
+ return ret;
}
static gboolean
g_tls_input_stream_gnutls_pollable_is_readable (GPollableInputStream *pollable)
{
GTlsInputStreamGnutls *tls_stream = G_TLS_INPUT_STREAM_GNUTLS (pollable);
+ GTlsConnectionGnutls *conn;
+ gboolean ret;
- g_return_val_if_fail (tls_stream->priv->conn != NULL, FALSE);
+ conn = g_weak_ref_get (&tls_stream->priv->weak_conn);
+ g_return_val_if_fail (conn != NULL, FALSE);
- return g_tls_connection_gnutls_check (tls_stream->priv->conn, G_IO_IN);
+ ret = g_tls_connection_gnutls_check (conn, G_IO_IN);
+
+ g_object_unref (conn);
+ return ret;
}
static GSource *
@@ -80,12 +96,15 @@ g_tls_input_stream_gnutls_pollable_create_source (GPollableInputStream *pollable
GCancellable *cancellable)
{
GTlsInputStreamGnutls *tls_stream = G_TLS_INPUT_STREAM_GNUTLS (pollable);
+ GTlsConnectionGnutls *conn;
+ GSource *ret;
- g_return_val_if_fail (tls_stream->priv->conn != NULL, NULL);
+ conn = g_weak_ref_get (&tls_stream->priv->weak_conn);
+ g_return_val_if_fail (conn != NULL, NULL);
- return g_tls_connection_gnutls_create_source (tls_stream->priv->conn,
- G_IO_IN,
- cancellable);
+ ret = g_tls_connection_gnutls_create_source (conn, G_IO_IN, cancellable);
+ g_object_unref (conn);
+ return ret;
}
static gssize
@@ -95,10 +114,16 @@ g_tls_input_stream_gnutls_pollable_read_nonblocking (GPollableInputStream *poll
GError **error)
{
GTlsInputStreamGnutls *tls_stream = G_TLS_INPUT_STREAM_GNUTLS (pollable);
+ GTlsConnectionGnutls *conn;
+ gssize ret;
+
+ conn = g_weak_ref_get (&tls_stream->priv->weak_conn);
+ g_return_val_if_fail (conn != NULL, -1);
+
+ ret = g_tls_connection_gnutls_read (conn, buffer, size, FALSE, NULL, error);
- return g_tls_connection_gnutls_read (tls_stream->priv->conn,
- buffer, size, FALSE,
- NULL, error);
+ g_object_unref (conn);
+ return ret;
}
static void
@@ -110,6 +135,7 @@ g_tls_input_stream_gnutls_class_init (GTlsInputStreamGnutlsClass *klass)
g_type_class_add_private (klass, sizeof (GTlsInputStreamGnutlsPrivate));
gobject_class->dispose = g_tls_input_stream_gnutls_dispose;
+ gobject_class->finalize = g_tls_input_stream_gnutls_finalize;
input_stream_class->read_fn = g_tls_input_stream_gnutls_read;
}
@@ -134,9 +160,7 @@ g_tls_input_stream_gnutls_new (GTlsConnectionGnutls *conn)
GTlsInputStreamGnutls *tls_stream;
tls_stream = g_object_new (G_TYPE_TLS_INPUT_STREAM_GNUTLS, NULL);
- tls_stream->priv->conn = conn;
- g_object_add_weak_pointer (G_OBJECT (conn),
- (gpointer *)&tls_stream->priv->conn);
+ g_weak_ref_init (&tls_stream->priv->weak_conn, conn);
return G_INPUT_STREAM (tls_stream);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]