[glib/wip/gcleanup: 2/7] grand: restructure a bit
- From: Ryan Lortie <ryanl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/gcleanup: 2/7] grand: restructure a bit
- Date: Mon, 25 Mar 2013 01:58:26 +0000 (UTC)
commit 4ee319737a9f969a59613b7b9c81b22b4aa897bd
Author: Ryan Lortie <desrt desrt ca>
Date: Sun Mar 24 21:25:26 2013 -0400
grand: restructure a bit
Instead of having lots of 'if NULL then allocate' code segments for the
global GRand instance, move it to a single getter function that everyone
calls.
glib/grand.c | 38 +++++++++++++++++---------------------
1 files changed, 17 insertions(+), 21 deletions(-)
---
diff --git a/glib/grand.c b/glib/grand.c
index 1485c2e..6bd7f18 100644
--- a/glib/grand.c
+++ b/glib/grand.c
@@ -110,7 +110,6 @@
**/
G_LOCK_DEFINE_STATIC (global_random);
-static GRand* global_random = NULL;
/* Period parameters */
#define N 624
@@ -584,6 +583,18 @@ g_rand_double_range (GRand* rand, gdouble begin, gdouble end)
return r * end - (r - 1) * begin;
}
+static GRand *
+get_global_random (void)
+{
+ static GRand *global_random;
+
+ /* called while locked */
+ if (!global_random)
+ global_random = g_rand_new ();
+
+ return global_random;
+}
+
/**
* g_random_boolean:
*
@@ -604,10 +615,7 @@ g_random_int (void)
{
guint32 result;
G_LOCK (global_random);
- if (!global_random)
- global_random = g_rand_new ();
-
- result = g_rand_int (global_random);
+ result = g_rand_int (get_global_random ());
G_UNLOCK (global_random);
return result;
}
@@ -627,10 +635,7 @@ g_random_int_range (gint32 begin, gint32 end)
{
gint32 result;
G_LOCK (global_random);
- if (!global_random)
- global_random = g_rand_new ();
-
- result = g_rand_int_range (global_random, begin, end);
+ result = g_rand_int_range (get_global_random (), begin, end);
G_UNLOCK (global_random);
return result;
}
@@ -647,10 +652,7 @@ g_random_double (void)
{
double result;
G_LOCK (global_random);
- if (!global_random)
- global_random = g_rand_new ();
-
- result = g_rand_double (global_random);
+ result = g_rand_double (get_global_random ());
G_UNLOCK (global_random);
return result;
}
@@ -669,10 +671,7 @@ g_random_double_range (gdouble begin, gdouble end)
{
double result;
G_LOCK (global_random);
- if (!global_random)
- global_random = g_rand_new ();
-
- result = g_rand_double_range (global_random, begin, end);
+ result = g_rand_double_range (get_global_random (), begin, end);
G_UNLOCK (global_random);
return result;
}
@@ -688,9 +687,6 @@ void
g_random_set_seed (guint32 seed)
{
G_LOCK (global_random);
- if (!global_random)
- global_random = g_rand_new_with_seed (seed);
- else
- g_rand_set_seed (global_random, seed);
+ g_rand_set_seed (get_global_random (), seed);
G_UNLOCK (global_random);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]