Update contrib.
1 #undef G_DISABLE_ASSERT
8 #include <glib_global.h>
9 #include "mrt2_glib2_test.h"
16 GStaticRecMutex test_g_static_rec_mutex_mutex;
17 static guint test_g_static_rec_mutex_int = 0;
18 static gboolean test_g_static_rec_mutex_thread_ready;
21 test_g_static_rec_mutex_thread (gpointer data)
23 g_assert (GPOINTER_TO_INT (data) == 42);
24 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)
26 test_g_static_rec_mutex_thread_ready = TRUE;
28 //Testing g_static_rec_mutex_lock_full
29 g_static_rec_mutex_lock_full (&test_g_static_rec_mutex_mutex, 10);
31 g_assert (test_g_static_rec_mutex_int == 42);
32 test_g_static_rec_mutex_thread_ready = FALSE;
34 //Testing g_static_rec_mutex_unlock_full for lock_full
35 g_static_rec_mutex_unlock_full (&test_g_static_rec_mutex_mutex);
37 g_thread_exit (GINT_TO_POINTER (43));
39 g_assert_not_reached ();
44 test_g_static_rec_mutex (void)
48 //Test for g_static_rec_mutex_init
49 g_static_rec_mutex_init(&test_g_static_rec_mutex_mutex);
51 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
52 test_g_static_rec_mutex_thread_ready = FALSE;
53 thread = g_thread_create (test_g_static_rec_mutex_thread,
54 GINT_TO_POINTER (42), TRUE, NULL);
55 /* This busy wait is only for testing purposes and not an example of
57 while (!test_g_static_rec_mutex_thread_ready)
58 g_usleep (G_USEC_PER_SEC / 5);
60 g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
61 test_g_static_rec_mutex_int = 41;
62 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
63 test_g_static_rec_mutex_int = 42;
64 g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
66 /* This busy wait is only for testing purposes and not an example of
68 while (test_g_static_rec_mutex_thread_ready)
69 g_usleep (G_USEC_PER_SEC / 5);
71 g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
72 test_g_static_rec_mutex_int = 0;
74 //Testing g_static_rec_mutex_unlock_full with normal single lock
75 g_static_rec_mutex_unlock_full (&test_g_static_rec_mutex_mutex);
77 g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
78 g_static_rec_mutex_free (&test_g_static_rec_mutex_mutex);
84 /* -1 = writing; >0 = # of readers */
85 static gint test_g_static_rw_lock_state = 0;
86 G_LOCK_DEFINE (test_g_static_rw_lock_state);
88 static gboolean test_g_static_rw_lock_run = TRUE;
89 GStaticRWLock test_g_static_rw_lock_lock;// = G_STATIC_RW_LOCK_INIT;
92 test_g_static_rw_lock_thread (gpointer data)
94 while (test_g_static_rw_lock_run)
96 if (g_random_double() > .2) /* I'm a reader */
99 if (g_random_double() > .2) /* I'll block */
100 g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
101 else /* I'll only try */
102 if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
104 G_LOCK (test_g_static_rw_lock_state);
105 g_assert (test_g_static_rw_lock_state >= 0);
106 test_g_static_rw_lock_state++;
107 G_UNLOCK (test_g_static_rw_lock_state);
109 g_usleep (g_random_int_range (20,1000));
111 G_LOCK (test_g_static_rw_lock_state);
112 test_g_static_rw_lock_state--;
113 G_UNLOCK (test_g_static_rw_lock_state);
115 g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
117 else /* I'm a writer */
120 if (g_random_double() > .2) /* I'll block */
121 g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
122 else /* I'll only try */
123 if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
125 G_LOCK (test_g_static_rw_lock_state);
126 g_assert (test_g_static_rw_lock_state == 0);
127 test_g_static_rw_lock_state = -1;
128 G_UNLOCK (test_g_static_rw_lock_state);
130 g_usleep (g_random_int_range (20,1000));
132 G_LOCK (test_g_static_rw_lock_state);
133 test_g_static_rw_lock_state = 0;
134 G_UNLOCK (test_g_static_rw_lock_state);
136 g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
143 test_g_static_rw_lock ()
145 GThread *threads[THREADS];
148 g_static_rw_lock_init(&test_g_static_rw_lock_lock);
150 for (i = 0; i < THREADS; i++)
152 threads[i] = g_thread_create (test_g_static_rw_lock_thread,
155 g_usleep (G_USEC_PER_SEC * 5);
156 test_g_static_rw_lock_run = FALSE;
157 for (i = 0; i < THREADS; i++)
159 g_thread_join (threads[i]);
161 g_assert (test_g_static_rw_lock_state == 0);
162 g_static_rw_lock_free(&test_g_static_rw_lock_lock);
165 void test_g_thread_error_quark()
167 GQuark c = g_thread_error_quark();
172 static void test_g_thread_set_priority (void)
175 thread = g_thread_self();
176 g_thread_set_priority (thread, G_THREAD_PRIORITY_HIGH);
177 g_assert((thread->priority) == G_THREAD_PRIORITY_HIGH);
181 /* run all the tests */
183 run_all_thread_tests()
185 test_g_static_rec_mutex ();
186 test_g_static_rw_lock ();
187 test_g_thread_error_quark();
188 test_g_thread_set_priority();
197 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
198 g_set_print_handler(mrtPrintHandler);
202 /* Only run the test, if threads are enabled and a default thread
203 implementation is available */
204 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
205 g_thread_init (NULL);
206 run_all_thread_tests ();
208 /* Now we rerun all tests, but this time we fool the system into
209 * thinking, that the available thread system is not native, but
212 g_thread_use_default_impl = FALSE;
213 run_all_thread_tests ();
218 testResultXml("tthread");
219 #endif /* EMULATOR */