summaryrefslogtreecommitdiff
path: root/gusb/gusb-context.c
diff options
context:
space:
mode:
authorDebarshi Ray <debarshir@src.gnome.org>2011-09-07 11:00:27 +0300
committerDebarshi Ray <debarshir@src.gnome.org>2011-09-08 22:59:42 +0300
commit5fb6ef3d1570b58d1f0446001a7544ecdecd8a75 (patch)
treec2c06997f47c751da768f0be8860394c957f221a /gusb/gusb-context.c
parent5e987ff6c3c0cb8e110dc8cf1d07674adc8e2ec7 (diff)
downloadgusb-5fb6ef3d1570b58d1f0446001a7544ecdecd8a75.tar.gz
Ensure that there is only one GUsbSource for each GUsbContext
As a result, g_usb_source_{new,destroy} have been moved to a private header, and are called from the get_source and finalize methods of GUsbContext. Note that the GUsbSource is lazy allocated and only created when g_usb_context_get_source is called for the first time for a particular GUsbContext.
Diffstat (limited to 'gusb/gusb-context.c')
-rw-r--r--gusb/gusb-context.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c
index 2abd5b4..cccd8ff 100644
--- a/gusb/gusb-context.c
+++ b/gusb/gusb-context.c
@@ -33,6 +33,7 @@
#include "gusb-util.h"
#include "gusb-context.h"
#include "gusb-context-private.h"
+#include "gusb-source-private.h"
static void g_usb_context_finalize (GObject *object);
@@ -51,6 +52,8 @@ enum {
**/
struct _GUsbContextPrivate
{
+ GStaticMutex source_mutex;
+ GUsbSource *source;
libusb_context *context;
int debug_level;
};
@@ -179,7 +182,11 @@ g_usb_context_class_init (GUsbContextClass *klass)
static void
g_usb_context_init (GUsbContext *context)
{
- context->priv = G_USB_CONTEXT_GET_PRIVATE (context);
+ GUsbContextPrivate *priv;
+
+ priv = context->priv = G_USB_CONTEXT_GET_PRIVATE (context);
+ g_static_mutex_init (&priv->source_mutex);
+ priv->source = NULL;
}
/**
@@ -191,6 +198,10 @@ g_usb_context_finalize (GObject *object)
GUsbContext *context = G_USB_CONTEXT (object);
GUsbContextPrivate *priv = context->priv;
+ g_static_mutex_free (&priv->source_mutex);
+ if (priv->source != NULL)
+ _g_usb_source_destroy (priv->source);
+
libusb_exit (priv->context);
G_OBJECT_CLASS (g_usb_context_parent_class)->finalize (object);
@@ -229,6 +240,34 @@ _g_usb_context_get_context (GUsbContext *context)
}
/**
+ * g_usb_context_get_source:
+ * @context: a #GUsbContext
+ * @main_ctx: a #GMainContext, or %NULL
+ *
+ * Returns a source for this context. The first call actually creates
+ * the source and the result is returned in all future calls.
+ *
+ * Return value: (transfer none): the #GUsbSource.
+ *
+ * Since: 0.1.0
+ **/
+GUsbSource *
+g_usb_context_get_source (GUsbContext *context,
+ GMainContext *main_ctx)
+{
+ GUsbContextPrivate *priv = context->priv;
+
+ if (priv->source == NULL) {
+ g_static_mutex_lock (&priv->source_mutex);
+ if (priv->source == NULL)
+ priv->source = _g_usb_source_new (main_ctx, context);
+ g_static_mutex_unlock (&priv->source_mutex);
+ }
+
+ return priv->source;
+}
+
+/**
* g_usb_context_set_debug:
* @context: a #GUsbContext
* @flags: a GLogLevelFlags such as %G_LOG_LEVEL_ERROR | %G_LOG_LEVEL_INFO, or 0