g_once_init*() (Re: Performance implications of GRegex structure)



On Sat, 17 Mar 2007, Owen Taylor wrote:

On Sat, 2007-03-17 at 16:14 +0100, Marco Barisione wrote:

Owen: what should do exactly G_STATIC_REGEX_INIT?

I was imagining:

struct _GStaticRegex {
   GOnce once;
   GStaticRegex *regex;
   const gchar *pattern;
   GRegexCompileFlags flags;
}

#define G_STATIC_REGEX_INIT(pattern, flags) { \
   G_ONCE_INIT,                              \
   NULL,                                     \
   pattern,                                  \
   flags                                     \
}

hm, i'd like to point out that there really should not be
extra macro magic around for handling cross-thread regexps.
with GOnce, GLib provides means for thread-safe one time
initialization already, and if the use of that is too cumbersome,
we're currently working on a more convenient alternative here:
	http://bugzilla.gnome.org/show_bug.cgi?id=65041
with that, you should be able to write:
  static GRegexp *pattern = NULL;
  static gsize initialized = 0;
  if (g_once_init_enter (&initialized))
    {
      pattern = g_regexp_new (...);
      g_once_init_leave (&initialized, 1);
    }
providing a g_once_*() convenience API that can be used everywhere
is a much better way than to re-invent G_STATIC_*() magic for every
component that's potentially used across multiple threads.

						- Owen

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]