Пример #1
0
static void
store_snap_cache_update (GsPlugin *plugin, GPtrArray *snaps)
{
	GsPluginData *priv = gs_plugin_get_data (plugin);
	g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->store_snaps_lock);
	guint i;

	for (i = 0; i < snaps->len; i++) {
		SnapdSnap *snap = snaps->pdata[i];
		g_hash_table_insert (priv->store_snaps, g_strdup (snapd_snap_get_name (snap)), g_object_ref (snap));
	}
}
Пример #2
0
static SnapdSnap *
store_snap_cache_lookup (GsPlugin *plugin, const gchar *name)
{
	GsPluginData *priv = gs_plugin_get_data (plugin);
	g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->store_snaps_lock);
	SnapdSnap *snap;

	snap = g_hash_table_lookup (priv->store_snaps, name);
	if (snap == NULL)
		return NULL;

	return g_object_ref (snap);
}
Пример #3
0
static void
test_g_mutex_locker (void)
{
  GMutex mutex;

  g_mutex_init (&mutex);

  if (TRUE)
    {
      g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex);
      
      g_assert (val != NULL);
    }
}
Пример #4
0
/**
 * as_ref_string_unref:
 * @rstr: a #AsRefString
 *
 * Removes a reference to the string.
 *
 * Returns: the same %AsRefString, or %NULL if the refcount dropped to zero
 *
 * Since: 0.6.6
 */
AsRefString *
as_ref_string_unref (AsRefString *rstr)
{
	AsRefStringHeader *hdr;

	g_return_val_if_fail (rstr != NULL, NULL);

	hdr = AS_REFPTR_TO_HEADER (rstr);
	if (hdr->refcnt < 0)
		return rstr;
	if (g_atomic_int_dec_and_test (&hdr->refcnt)) {
		g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&as_ref_string_mutex);
		g_hash_table_remove (as_ref_string_get_hash_safe (), rstr);
		return NULL;
	}
	return rstr;
}
Пример #5
0
/**
 * as_ref_string_new_copy_with_length:
 * @str: a string
 * @len: length of @str, not including the NUL byte
 *
 * Returns a deep copied refcounted string. The returned string can be modified
 * without affecting other refcounted versions.
 *
 * Returns: a %AsRefString
 *
 * Since: 0.6.6
 */
AsRefString *
as_ref_string_new_copy_with_length (const gchar *str, gsize len)
{
	AsRefStringHeader *hdr;
	AsRefString *rstr_new;
	g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&as_ref_string_mutex);

	/* create object */
	hdr = g_malloc (len + sizeof (AsRefStringHeader) + 1);
	hdr->refcnt = 1;
	rstr_new = AS_REFPTR_FROM_HEADER (hdr);
	memcpy (rstr_new, str, len);
	rstr_new[len] = '\0';

	/* add */
	g_hash_table_add (as_ref_string_get_hash_safe (), rstr_new);

	/* return to data, not the header */
	return rstr_new;
}
Пример #6
0
/**
 * as_ref_string_new_with_length:
 * @str: a string
 * @len: length of @str, not including the NUL byte
 *
 * Returns a immutable refcounted string. The returned string cannot be modified
 * without affecting other refcounted versions.
 *
 * Returns: a %AsRefString
 *
 * Since: 0.6.6
 */
AsRefString *
as_ref_string_new_with_length (const gchar *str, gsize len)
{
	AsRefStringHeader *hdr;
	g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&as_ref_string_mutex);

	g_return_val_if_fail (str != NULL, NULL);

	/* already in hash */
	if (g_hash_table_contains (as_ref_string_get_hash_safe (), str)) {
		hdr = AS_REFPTR_TO_HEADER (str);
		if (hdr->refcnt < 0)
			return (AsRefString *) str;
		g_atomic_int_inc (&hdr->refcnt);
		return (AsRefString *) str;
	}

	g_clear_pointer (&locker, g_mutex_locker_free);
	return as_ref_string_new_copy_with_length (str, len);
}
Пример #7
0
static void
test_g_mutex_locker (void)
{
  GMutex mutex;
  GThread *thread;

  g_mutex_init (&mutex);

  if (TRUE)
    {
      g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex);
      
      g_assert_nonnull (val);

      /* Verify that the mutex is actually locked */
      thread = g_thread_new ("mutex locked", mutex_locked_thread, &mutex);
      g_thread_join (thread);
    }

    /* Verify that the mutex is unlocked again */
    thread = g_thread_new ("mutex unlocked", mutex_unlocked_thread, &mutex);
    g_thread_join (thread);
}
Пример #8
0
/**
 * as_ref_string_debug:
 * @flags: some #AsRefStringDebugFlags, e.g. %AS_REF_STRING_DEBUG_DUPES
 *
 * This function outputs some debugging information to a string.
 *
 * Returns: a string describing the current state of the dedupe hash.
 *
 * Since: 0.6.6
 */
gchar *
as_ref_string_debug (AsRefStringDebugFlags flags)
{
	GHashTable *hash = NULL;
	GString *tmp = g_string_new (NULL);
	g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&as_ref_string_mutex);

	/* overview */
	hash = as_ref_string_get_hash_safe ();
	g_string_append_printf (tmp, "Size of hash table: %u\n",
				g_hash_table_size (hash));

	/* success: deduped */
	if (flags & AS_REF_STRING_DEBUG_DEDUPED) {
		GList *l;
		g_autoptr(GList) keys = g_hash_table_get_keys (hash);

		/* split up sections */
		if (tmp->len > 0)
			g_string_append (tmp, "\n\n");

		/* sort by refcount number */
		keys = g_list_sort (keys, as_ref_string_sort_by_refcnt_cb);
		g_string_append (tmp, "Deduplicated strings:\n");
		for (l = keys; l != NULL; l = l->next) {
			const gchar *str = l->data;
			AsRefStringHeader *hdr = AS_REFPTR_TO_HEADER (str);
			if (hdr->refcnt <= 1)
				continue;
			g_string_append_printf (tmp, "%i\t%s\n", hdr->refcnt, str);
		}
	}

	/* failed: duplicate */
	if (flags & AS_REF_STRING_DEBUG_DUPES) {
		GList *l;
		GList *l2;
		g_autoptr(GHashTable) dupes = g_hash_table_new (g_direct_hash, g_direct_equal);
		g_autoptr(GList) keys = g_hash_table_get_keys (hash);

		/* split up sections */
		if (tmp->len > 0)
			g_string_append (tmp, "\n\n");

		g_string_append (tmp, "Duplicated strings:\n");
		for (l = keys; l != NULL; l = l->next) {
			const gchar *str = l->data;
			AsRefStringHeader *hdr = AS_REFPTR_TO_HEADER (str);
			guint dupe_cnt = 0;

			if (g_hash_table_contains (dupes, hdr))
				continue;
			g_hash_table_add (dupes, (gpointer) hdr);

			for (l2 = l; l2 != NULL; l2 = l2->next) {
				const gchar *str2 = l2->data;
				AsRefStringHeader *hdr2 = AS_REFPTR_TO_HEADER (str2);
				if (g_hash_table_contains (dupes, hdr2))
					continue;
				if (l == l2)
					continue;
				if (g_strcmp0 (str, str2) != 0)
					continue;
				g_hash_table_add (dupes, (gpointer) hdr2);
				dupe_cnt += 1;
			}
			if (dupe_cnt > 0) {
				g_string_append_printf (tmp, "%u\t%s\n",
							dupe_cnt, str);
			}
		}
	}
	return g_string_free (tmp, FALSE);
}