Merge branch 'tests-tsan' into 'master'

tests: Fix some data races in tests

See merge request GNOME/glib!453
This commit is contained in:
Philip Withnall 2018-11-13 13:12:38 +00:00
commit e0148985f3
3 changed files with 17 additions and 28 deletions

View File

@ -116,19 +116,22 @@ context_clear (struct context *ctx)
static void
context_quit (struct context *ctx)
{
ctx->quit = TRUE;
g_atomic_int_set (&ctx->quit, TRUE);
g_wakeup_signal (ctx->wakeup);
}
static struct token *
context_pop_token (struct context *ctx)
context_try_pop_token (struct context *ctx)
{
struct token *token;
struct token *token = NULL;
g_mutex_lock (&ctx->lock);
token = ctx->pending_tokens->data;
ctx->pending_tokens = g_slist_delete_link (ctx->pending_tokens,
ctx->pending_tokens);
if (ctx->pending_tokens != NULL)
{
token = ctx->pending_tokens->data;
ctx->pending_tokens = g_slist_delete_link (ctx->pending_tokens,
ctx->pending_tokens);
}
g_mutex_unlock (&ctx->lock);
return token;
@ -188,17 +191,15 @@ static gpointer
thread_func (gpointer data)
{
struct context *ctx = data;
struct token *token;
while (!ctx->quit)
while (!g_atomic_int_get (&ctx->quit))
{
wait_for_signaled (ctx->wakeup);
g_wakeup_acknowledge (ctx->wakeup);
while (ctx->pending_tokens)
while ((token = context_try_pop_token (ctx)) != NULL)
{
struct token *token;
token = context_pop_token (ctx);
g_assert (token->owner == ctx);
dispatch_token (token);
}

View File

@ -153,19 +153,11 @@ my_test_class_init (GTestClass *klass)
klass->test_signal2 = my_test_test_signal2;
}
static inline guint32
quick_rand32 (void)
{
static guint32 accu = 2147483563;
accu = 1664525 * accu + 1013904223;
return accu;
}
static void
test_closure (GClosure *closure)
{
/* try to produce high contention in closure->ref_count */
guint i = 0, n = quick_rand32() % 199;
guint i = 0, n = g_random_int () % 199;
for (i = 0; i < n; i++)
g_closure_ref (closure);
g_closure_sink (closure); /* NOP */

View File

@ -12,8 +12,6 @@
#define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
#define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
static GRand *grand;
typedef struct _GTest GTest;
typedef struct _GTestClass GTestClass;
@ -34,7 +32,7 @@ struct _GTestClass
};
static GType my_test_get_type (void);
static volatile gboolean stopping;
static gboolean stopping;
/* Element signals and args */
enum
@ -87,8 +85,6 @@ my_test_get_type (void)
NULL
};
grand = g_rand_new();
test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
&test_info, 0);
}
@ -221,7 +217,7 @@ my_test_do_signal3 (GTest * test)
static void
my_test_do_prop (GTest * test)
{
test->value = g_rand_int (grand);
test->value = g_random_int ();
g_object_notify (G_OBJECT (test), "test-prop");
}
@ -230,7 +226,7 @@ run_thread (GTest * test)
{
gint i = 1;
while (!stopping) {
while (!g_atomic_int_get (&stopping)) {
if (TESTNUM == 1)
my_test_do_signal1 (test);
if (TESTNUM == 2)
@ -290,7 +286,7 @@ main (int argc, char **argv)
}
g_usleep (5000000);
stopping = TRUE;
g_atomic_int_set (&stopping, TRUE);
g_print ("\nstopping\n");