1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2011 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Alexander Larsson <alexl@redhat.com>
27 #include "gresource.h"
28 #include <gvdb/gvdb-reader.h>
30 #include <gio/gmemoryinputstream.h>
31 #include <gio/gzlibdecompressor.h>
32 #include <gio/gconverterinputstream.h>
41 static void register_lazy_static_resources ();
43 G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref)
47 * @short_description: Resource framework
50 * Applications and libraries often contain binary or textual data that is really part of the
51 * application, rather than user data. For instance #GtkBuilder .ui files, splashscreen images,
52 * GMenu markup xml, CSS files, icons, etc. These are often shipped as files in <filename>$datadir/appname</filename>, or
53 * manually included as literal strings in the code.
55 * The #GResource API and the <link linkend="glib-compile-resources">glib-compile-resources</link> program
56 * provide a convenient and efficient alternative to this which has some nice properties. You
57 * maintain the files as normal files, so its easy to edit them, but during the build the files
58 * are combined into a binary bundle that is linked into the executable. This means that loading
59 * the resource files are efficient (as they are already in memory, shared with other instances) and
60 * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
61 * also makes it easier to create relocatable applications.
63 * Resource files can also be marked as compressed. Such files will be included in the resource bundle
64 * in a compressed form, but will be automatically uncompressed when the resource is used. This
65 * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
67 * Resource files can also be marked to be preprocessed, by setting the value of the
68 * <literal>preprocess</literal> attribute to a comma-separated list of preprocessing options.
69 * The only options currently supported are:
71 * <literal>xml-stripblanks</literal> which will use <command>xmllint</command> to strip
72 * ignorable whitespace from the xml file. For this to work, the <envar>XMLLINT</envar>
73 * environment variable must be set to the full path to the xmllint executable, or xmllint
74 * must be in the PATH; otherwise the preprocessing step is skipped.
76 * <literal>to-pixdata</literal> which will use <command>gdk-pixbuf-pixdata</command> to convert
77 * images to the GdkPixdata format, which allows you to create pixbufs directly using the data inside
78 * the resource file, rather than an (uncompressed) copy if it. For this, the gdk-pixbuf-pixdata
79 * program must be in the PATH, or the <envar>GDK_PIXBUF_PIXDATA</envar> environment variable must be
80 * set to the full path to the gdk-pixbuf-pixdata executable; otherwise the resource compiler will
83 * Resource bundles are created by the <link linkend="glib-compile-resources">glib-compile-resources</link> program
84 * which takes an xml file that describes the bundle, and a set of files that the xml references. These
85 * are combined into a binary resource bundle.
87 * <example id="resource-example"><title>Example resource description</title>
88 * <programlisting><![CDATA[
89 * <?xml version="1.0" encoding="UTF-8"?>
91 * <gresource prefix="/org/gtk/Example">
92 * <file>data/splashscreen.png</file>
93 * <file compressed="true">dialog.ui</file>
94 * <file preprocess="xml-stripblanks">menumarkup.xml</file>
97 * ]]></programlisting></example>
99 * This will create a resource bundle with the following files:
100 * <programlisting><![CDATA[
101 * /org/gtk/Example/data/splashscreen.png
102 * /org/gtk/Example/dialog.ui
103 * /org/gtk/Example/menumarkup.xml
104 * ]]></programlisting>
106 * Note that all resources in the process share the same namespace, so use java-style
107 * path prefixes (like in the above example) to avoid conflicts.
109 * You can then use <link linkend="glib-compile-resources">glib-compile-resources</link> to compile the xml to a
110 * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
111 * --generate-header arguments to create a source file and header to link directly into your application.
113 * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
114 * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
115 * to the data. You can also use uris like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
118 * There are two forms of the generated source, the default version uses the compiler support for constructor
119 * and destructor functions (where available) to automatically create and register the #GResource on startup
120 * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead
121 * created. This requires an explicit initialization call in your application/library, but it works on all platforms,
122 * even on the minor ones where this is not available. (Constructor support is available for at least Win32, MacOS and Linux.)
124 * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
125 * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
126 * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
127 * is for your own resources, and resource data is often used once, during parsing, and then released.
133 * g_resource_error_quark:
135 * Gets the #GResource Error Quark.
137 * Return value: a #GQuark
142 g_resource_error_quark (void)
144 return g_quark_from_static_string ("g-resource-error-quark");
149 * @resource: A #GResource
151 * Atomically increments the reference count of @array by one. This
152 * function is MT-safe and may be called from any thread.
154 * Returns: The passed in #GResource
159 g_resource_ref (GResource *resource)
161 g_atomic_int_inc (&resource->ref_count);
167 * @resource: A #GResource
169 * Atomically decrements the reference count of @resource by one. If the
170 * reference count drops to 0, all memory allocated by the array is
171 * released. This function is MT-safe and may be called from any
177 g_resource_unref (GResource *resource)
179 if (g_atomic_int_dec_and_test (&resource->ref_count))
181 gvdb_table_unref (resource->table);
187 * g_resource_new_from_table:
188 * @table: (transfer full): a GvdbTable
190 * Returns: (transfer full): a new #GResource for @table
193 g_resource_new_from_table (GvdbTable *table)
197 resource = g_new (GResource, 1);
198 resource->ref_count = 1;
199 resource->table = table;
205 * g_resource_new_from_data:
207 * @error: return location for a #GError, or %NULL
209 * Creates a GResource from a reference to the binary resource bundle.
210 * This will keep a reference to @data while the resource lives, so
211 * the data should not be modified or freed.
213 * If you want to use this resource in the global resource namespace you need
214 * to register it with g_resources_register().
216 * Return value: (transfer full): a new #GResource, or %NULL on error
221 g_resource_new_from_data (GBytes *data,
226 table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL),
227 g_bytes_get_size (data),
230 (GvdbRefFunc)g_bytes_ref,
231 (GDestroyNotify)g_bytes_unref,
237 return g_resource_new_from_table (table);
242 * @filename: (type filename): the path of a filename to load, in the GLib filename encoding
243 * @error: return location for a #GError, or %NULL
245 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
246 * you to query it for data.
248 * If you want to use this resource in the global resource namespace you need
249 * to register it with g_resources_register().
251 * Return value: (transfer full): a new #GResource, or %NULL on error
256 g_resource_load (const gchar *filename,
261 table = gvdb_table_new (filename, FALSE, error);
265 return g_resource_new_from_table (table);
269 gboolean do_lookup (GResource *resource,
271 GResourceLookupFlags lookup_flags,
278 char *free_path = NULL;
280 gboolean res = FALSE;
283 path_len = strlen (path);
284 if (path[path_len-1] == '/')
286 path = free_path = g_strdup (path);
287 free_path[path_len-1] = 0;
290 value = gvdb_table_get_raw_value (resource->table, path);
294 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
295 _("The resource at '%s' does not exist"),
300 guint32 _size, _flags;
303 g_variant_get (value, "(uu@ay)",
308 _size = GUINT32_FROM_LE (_size);
309 _flags = GUINT32_FROM_LE (_flags);
316 *data = g_variant_get_data (array);
319 /* Don't report trailing newline that non-compressed files has */
320 if (_flags & G_RESOURCE_FLAGS_COMPRESSED)
321 *data_size = g_variant_get_size (array);
323 *data_size = g_variant_get_size (array) - 1;
325 g_variant_unref (array);
326 g_variant_unref (value);
336 * g_resource_open_stream:
337 * @resource: A #GResource
338 * @path: A pathname inside the resource
339 * @lookup_flags: A #GResourceLookupFlags
340 * @error: return location for a #GError, or %NULL
342 * Looks for a file at the specified @path in the resource and
343 * returns a #GInputStream that lets you read the data.
345 * @lookup_flags controls the behaviour of the lookup.
347 * Returns: (transfer full): #GInputStream or %NULL on error.
348 * Free the returned object with g_object_unref()
353 g_resource_open_stream (GResource *resource,
355 GResourceLookupFlags lookup_flags,
361 GInputStream *stream, *stream2;
363 if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
366 stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
367 g_object_set_data_full (G_OBJECT (stream), "g-resource",
368 g_resource_ref (resource),
369 (GDestroyNotify)g_resource_unref);
371 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
373 GZlibDecompressor *decompressor =
374 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
376 stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
377 g_object_unref (decompressor);
378 g_object_unref (stream);
386 * g_resource_lookup_data:
387 * @resource: A #GResource
388 * @path: A pathname inside the resource
389 * @lookup_flags: A #GResourceLookupFlags
390 * @error: return location for a #GError, or %NULL
392 * Looks for a file at the specified @path in the resource and
393 * returns a #GBytes that lets you directly access the data in
396 * The data is always followed by a zero byte, so you
397 * can safely use the data as a C string. However, that byte
398 * is not included in the size of the GBytes.
400 * For uncompressed resource files this is a pointer directly into
401 * the resource bundle, which is typically in some readonly data section
402 * in the program binary. For compressed files we allocate memory on
403 * the heap and automatically uncompress the data.
405 * @lookup_flags controls the behaviour of the lookup.
407 * Returns: (transfer full): #GBytes or %NULL on error.
408 * Free the returned object with g_bytes_unref()
413 g_resource_lookup_data (GResource *resource,
415 GResourceLookupFlags lookup_flags,
423 if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
426 if (flags & G_RESOURCE_FLAGS_COMPRESSED)
428 char *uncompressed, *d;
430 GConverterResult res;
431 gsize d_size, s_size;
432 gsize bytes_read, bytes_written;
435 GZlibDecompressor *decompressor =
436 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
438 uncompressed = g_malloc (size + 1);
447 res = g_converter_convert (G_CONVERTER (decompressor),
450 G_CONVERTER_INPUT_AT_END,
454 if (res == G_CONVERTER_ERROR)
456 g_free (uncompressed);
457 g_object_unref (decompressor);
459 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL,
460 _("The resource at '%s' failed to decompress"),
466 s_size -= bytes_read;
468 d_size -= bytes_written;
470 while (res != G_CONVERTER_FINISHED);
472 uncompressed[size] = 0; /* Zero terminate */
474 g_object_unref (decompressor);
476 return g_bytes_new_take (uncompressed, size);
479 return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
483 * g_resource_get_info:
484 * @resource: A #GResource
485 * @path: A pathname inside the resource
486 * @lookup_flags: A #GResourceLookupFlags
487 * @size: (out) (allow-none): a location to place the length of the contents of the file,
488 * or %NULL if the length is not needed
489 * @flags: (out) (allow-none): a location to place the flags about the file,
490 * or %NULL if the length is not needed
491 * @error: return location for a #GError, or %NULL
493 * Looks for a file at the specified @path in the resource and
494 * if found returns information about it.
496 * @lookup_flags controls the behaviour of the lookup.
498 * Returns: %TRUE if the file was found. %FALSE if there were errors
503 g_resource_get_info (GResource *resource,
505 GResourceLookupFlags lookup_flags,
510 return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
514 * g_resource_enumerate_children:
515 * @resource: A #GResource
516 * @path: A pathname inside the resource
517 * @lookup_flags: A #GResourceLookupFlags
518 * @error: return location for a #GError, or %NULL
520 * Returns all the names of children at the specified @path in the resource.
521 * The return result is a %NULL terminated list of strings which should
522 * be released with g_strfreev().
524 * @lookup_flags controls the behaviour of the lookup.
526 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
531 g_resource_enumerate_children (GResource *resource,
533 GResourceLookupFlags lookup_flags,
538 char *path_with_slash;
542 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
543 _("The resource at '%s' does not exist"),
548 path_len = strlen (path);
549 if (path[path_len-1] != '/')
550 path_with_slash = g_strconcat (path, "/", NULL);
552 path_with_slash = g_strdup (path);
554 children = gvdb_table_list (resource->table, path_with_slash);
555 g_free (path_with_slash);
557 if (children == NULL)
559 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
560 _("The resource at '%s' does not exist"),
568 static GRWLock resources_lock;
569 static GList *registered_resources;
571 /* This is updated atomically, so we can append to it and check for NULL outside the
572 lock, but all other accesses are done under the write lock */
573 static GStaticResource *lazy_register_resources;
576 g_resources_register_unlocked (GResource *resource)
578 registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource));
582 g_resources_unregister_unlocked (GResource *resource)
584 if (g_list_find (registered_resources, resource) == NULL)
586 g_warning ("Tried to remove not registred resource");
590 registered_resources = g_list_remove (registered_resources, resource);
591 g_resource_unref (resource);
596 * g_resources_register:
597 * @resource: A #GResource
599 * Registers the resource with the process-global set of resources.
600 * Once a resource is registered the files in it can be accessed
601 * with the global resource lookup functions like g_resources_lookup_data().
606 g_resources_register (GResource *resource)
608 g_rw_lock_writer_lock (&resources_lock);
609 g_resources_register_unlocked (resource);
610 g_rw_lock_writer_unlock (&resources_lock);
614 * g_resources_unregister:
615 * @resource: A #GResource
617 * Unregisters the resource from the process-global set of resources.
622 g_resources_unregister (GResource *resource)
624 g_rw_lock_writer_lock (&resources_lock);
625 g_resources_unregister_unlocked (resource);
626 g_rw_lock_writer_unlock (&resources_lock);
630 * g_resources_open_stream:
631 * @path: A pathname inside the resource
632 * @lookup_flags: A #GResourceLookupFlags
633 * @error: return location for a #GError, or %NULL
635 * Looks for a file at the specified @path in the set of
636 * globally registred resources and returns a #GInputStream
637 * that lets you read the data.
639 * @lookup_flags controls the behaviour of the lookup.
641 * Returns: (transfer full): #GInputStream or %NULL on error.
642 * Free the returned object with g_object_unref()
647 g_resources_open_stream (const gchar *path,
648 GResourceLookupFlags lookup_flags,
651 GInputStream *res = NULL;
653 GInputStream *stream;
655 register_lazy_static_resources ();
657 g_rw_lock_reader_lock (&resources_lock);
659 for (l = registered_resources; l != NULL; l = l->next)
661 GResource *r = l->data;
662 GError *my_error = NULL;
664 stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
665 if (stream == NULL &&
666 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
668 g_clear_error (&my_error);
673 g_propagate_error (error, my_error);
680 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
681 _("The resource at '%s' does not exist"),
684 g_rw_lock_reader_unlock (&resources_lock);
690 * g_resources_lookup_data:
691 * @path: A pathname inside the resource
692 * @lookup_flags: A #GResourceLookupFlags
693 * @error: return location for a #GError, or %NULL
695 * Looks for a file at the specified @path in the set of
696 * globally registred resources and returns a #GBytes that
697 * lets you directly access the data in memory.
699 * The data is always followed by a zero byte, so you
700 * can safely use the data as a C string. However, that byte
701 * is not included in the size of the GBytes.
703 * For uncompressed resource files this is a pointer directly into
704 * the resource bundle, which is typically in some readonly data section
705 * in the program binary. For compressed files we allocate memory on
706 * the heap and automatically uncompress the data.
708 * @lookup_flags controls the behaviour of the lookup.
710 * Returns: (transfer full): #GBytes or %NULL on error.
711 * Free the returned object with g_bytes_unref()
716 g_resources_lookup_data (const gchar *path,
717 GResourceLookupFlags lookup_flags,
724 register_lazy_static_resources ();
726 g_rw_lock_reader_lock (&resources_lock);
728 for (l = registered_resources; l != NULL; l = l->next)
730 GResource *r = l->data;
731 GError *my_error = NULL;
733 data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
735 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
737 g_clear_error (&my_error);
742 g_propagate_error (error, my_error);
749 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
750 _("The resource at '%s' does not exist"),
753 g_rw_lock_reader_unlock (&resources_lock);
759 * g_resources_enumerate_children:
760 * @path: A pathname inside the resource
761 * @lookup_flags: A #GResourceLookupFlags
762 * @error: return location for a #GError, or %NULL
764 * Returns all the names of children at the specified @path in the set of
765 * globally registred resources.
766 * The return result is a %NULL terminated list of strings which should
767 * be released with g_strfreev().
769 * @lookup_flags controls the behaviour of the lookup.
771 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
776 g_resources_enumerate_children (const gchar *path,
777 GResourceLookupFlags lookup_flags,
780 GHashTable *hash = NULL;
785 register_lazy_static_resources ();
787 g_rw_lock_reader_lock (&resources_lock);
789 for (l = registered_resources; l != NULL; l = l->next)
791 GResource *r = l->data;
793 children = g_resource_enumerate_children (r, path, 0, NULL);
795 if (children != NULL)
798 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
800 for (i = 0; children[i] != NULL; i++)
801 g_hash_table_insert (hash, children[i], children[i]);
806 g_rw_lock_reader_unlock (&resources_lock);
810 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
811 _("The resource at '%s' does not exist"),
820 n_children = g_hash_table_size (hash);
821 children = g_new (char *, n_children + 1);
824 g_hash_table_iter_init (&iter, hash);
825 while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
826 children[i++] = g_strdup (key);
827 children[i++] = NULL;
829 g_hash_table_destroy (hash);
836 * g_resources_get_info:
837 * @path: A pathname inside the resource
838 * @lookup_flags: A #GResourceLookupFlags
839 * @size: (out) (allow-none): a location to place the length of the contents of the file,
840 * or %NULL if the length is not needed
841 * @flags: (out) (allow-none): a location to place the flags about the file,
842 * or %NULL if the length is not needed
843 * @error: return location for a #GError, or %NULL
845 * Looks for a file at the specified @path in the set of
846 * globally registred resources and if found returns information about it.
848 * @lookup_flags controls the behaviour of the lookup.
850 * Returns: %TRUE if the file was found. %FALSE if there were errors
855 g_resources_get_info (const gchar *path,
856 GResourceLookupFlags lookup_flags,
861 gboolean res = FALSE;
865 register_lazy_static_resources ();
867 g_rw_lock_reader_lock (&resources_lock);
869 for (l = registered_resources; l != NULL; l = l->next)
871 GResource *r = l->data;
872 GError *my_error = NULL;
874 r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
876 g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
878 g_clear_error (&my_error);
883 g_propagate_error (error, my_error);
890 g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
891 _("The resource at '%s' does not exist"),
894 g_rw_lock_reader_unlock (&resources_lock);
899 /* This code is to handle registration of resources very early, from a constructor.
900 * At that point we'd like to do minimal work, to avoid ordering issues. For instance,
901 * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable
902 * before the first call to g_malloc.
904 * So, what we do at construction time is that we just register a static structure on
905 * a list of resources that need to be initialized, and then later, when doing any lookups
906 * in the global list of registered resources, or when getting a reference to the
907 * lazily initialized resource we lazily create and register all the GResources on
910 * To avoid having to use locks in the constructor, and having to grab the writer lock
911 * when checking the lazy registering list we update lazy_register_resources in
912 * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all
914 * * check if there are any resources to lazily initialize
915 * * Add a static resource to the lazy init list
916 * Do use the full writer lock for protection.
920 register_lazy_static_resources_unlocked (void)
922 GStaticResource *list;
925 list = lazy_register_resources;
926 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, list, NULL));
930 GBytes *bytes = g_bytes_new_static (list->data, list->data_len);
931 GResource *resource = g_resource_new_from_data (bytes, NULL);
934 g_resources_register_unlocked (resource);
935 g_atomic_pointer_set (&list->resource, resource);
937 g_bytes_unref (bytes);
944 register_lazy_static_resources (void)
946 if (g_atomic_pointer_get (&lazy_register_resources) == NULL)
949 g_rw_lock_writer_lock (&resources_lock);
950 register_lazy_static_resources_unlocked ();
951 g_rw_lock_writer_unlock (&resources_lock);
955 * g_static_resource_init:
956 * @static_resource: pointer to a static #GStaticResource
958 * Initializes a GResource from static data using a
961 * This is normally used by code generated by
962 * <link linkend="glib-compile-resources">glib-compile-resources</link>
963 * and is not typically used by other code.
968 g_static_resource_init (GStaticResource *static_resource)
974 next = lazy_register_resources;
975 static_resource->next = next;
977 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource));
981 * g_static_resource_fini:
982 * @static_resource: pointer to a static #GStaticResource
984 * Finalized a GResource initialized by g_static_resource_init().
986 * This is normally used by code generated by
987 * <link linkend="glib-compile-resources">glib-compile-resources</link>
988 * and is not typically used by other code.
993 g_static_resource_fini (GStaticResource *static_resource)
997 g_rw_lock_writer_lock (&resources_lock);
999 register_lazy_static_resources_unlocked ();
1001 resource = g_atomic_pointer_get (&static_resource->resource);
1004 g_atomic_pointer_set (&static_resource->resource, NULL);
1005 g_resources_unregister_unlocked (resource);
1006 g_resource_unref (resource);
1009 g_rw_lock_writer_unlock (&resources_lock);
1013 * g_static_resource_get_resource:
1014 * @static_resource: pointer to a static #GStaticResource
1016 * Gets the GResource that was registred by a call to g_static_resource_init().
1018 * This is normally used by code generated by
1019 * <link linkend="glib-compile-resources">glib-compile-resources</link>
1020 * and is not typically used by other code.
1022 * Return value: (transfer none): a #GResource
1027 g_static_resource_get_resource (GStaticResource *static_resource)
1029 register_lazy_static_resources ();
1031 return g_atomic_pointer_get (&static_resource->resource);