2 /* Unit tests for GOnce and friends
3 * Copyright (C) 2011 Red Hat, Inc
4 * Author: Matthias Clasen
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
10 * This work 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.
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
27 do_once (gpointer data
)
33 return GINT_TO_POINTER (i
);
39 GOnce once
= G_ONCE_INIT
;
42 g_assert (once
.status
== G_ONCE_STATUS_NOTCALLED
);
44 res
= g_once (&once
, do_once
, NULL
);
45 g_assert_cmpint (GPOINTER_TO_INT (res
), ==, 1);
47 g_assert (once
.status
== G_ONCE_STATUS_READY
);
49 res
= g_once (&once
, do_once
, NULL
);
50 g_assert_cmpint (GPOINTER_TO_INT (res
), ==, 1);
56 static gsize init
= 0;
58 if (g_once_init_enter (&init
))
61 g_once_init_leave (&init
, 1);
64 g_assert_cmpint (init
, ==, 1);
65 if (g_once_init_enter (&init
))
67 g_assert_not_reached ();
68 g_once_init_leave (&init
, 2);
70 g_assert_cmpint (init
, ==, 1);
80 static gsize init
= 0;
82 if (g_once_init_enter (&init
))
86 g_once_init_leave (&init
, 1);
91 thread_func (gpointer data
)
102 GThread
*threads
[THREADS
];
106 for (i
= 0; i
< THREADS
; i
++)
107 threads
[i
] = g_thread_new ("once3", thread_func
, NULL
);
109 for (i
= 0; i
< THREADS
; i
++)
110 g_thread_join (threads
[i
]);
112 g_assert_cmpint (shared
, ==, 42);
118 static const gchar
*val
;
120 if (g_once_init_enter (&val
))
121 g_once_init_leave (&val
, "foo");
123 g_assert_cmpstr (val
, ==, "foo");
127 main (int argc
, char *argv
[])
129 g_test_init (&argc
, &argv
, NULL
);
131 g_test_add_func ("/thread/once1", test_once1
);
132 g_test_add_func ("/thread/once2", test_once2
);
133 g_test_add_func ("/thread/once3", test_once3
);
134 g_test_add_func ("/thread/once4", test_once4
);
136 return g_test_run ();