Completed the thread support in GLib. Thread creation, prioritizing
[platform/upstream/glib.git] / tests / thread-test.c
1 #include <glib.h>
2
3 /* GMutex */
4
5 static GMutex* test_g_mutex_mutex = NULL;
6 static guint test_g_mutex_int = 0;
7
8 static void
9 test_g_mutex_thread (gpointer data)
10 {
11   g_assert (GPOINTER_TO_INT (data) == 42);
12   g_assert (g_mutex_trylock (test_g_mutex_mutex) == FALSE);
13   g_mutex_lock (test_g_mutex_mutex);
14   g_assert (test_g_mutex_int == 42);
15   g_mutex_unlock (test_g_mutex_mutex);
16 }
17
18 static void
19 test_g_mutex (void)
20 {
21   GThread *thread;
22   test_g_mutex_mutex = g_mutex_new ();
23
24   g_assert (g_mutex_trylock (test_g_mutex_mutex));
25   thread = g_thread_create (test_g_mutex_thread, 
26                             GINT_TO_POINTER (42),
27                             0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
28   g_usleep (G_MICROSEC);
29   test_g_mutex_int = 42;
30   g_mutex_unlock (test_g_mutex_mutex);
31   g_thread_join (thread);
32   g_mutex_free (test_g_mutex_mutex);
33 }
34
35 /* GStaticRecMutex */
36
37 static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
38 static guint test_g_static_rec_mutex_int = 0;
39
40 static void
41 test_g_static_rec_mutex_thread (gpointer data)
42 {
43   g_assert (GPOINTER_TO_INT (data) == 42);
44   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) 
45             == FALSE);
46   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
47   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
48   g_assert (test_g_static_rec_mutex_int == 42);
49   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
50   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
51 }
52
53 static void
54 test_g_static_rec_mutex (void)
55 {
56   GThread *thread;
57
58   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
59   thread = g_thread_create (test_g_static_rec_mutex_thread, 
60                             GINT_TO_POINTER (42),
61                             0, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL);
62   g_usleep (G_MICROSEC);
63   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
64   g_usleep (G_MICROSEC);
65   test_g_static_rec_mutex_int = 41;
66   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
67   test_g_static_rec_mutex_int = 42;  
68   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
69   g_usleep (G_MICROSEC);
70   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
71   test_g_static_rec_mutex_int = 0;  
72   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
73   g_thread_join (thread);
74 }
75
76 /* GStaticPrivate */
77
78 #define THREADS 10
79
80 static GStaticPrivate test_g_static_private_private = G_STATIC_PRIVATE_INIT;
81 static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
82 static guint test_g_static_private_counter = 0;
83
84 static gpointer
85 test_g_static_private_constructor (void)
86 {
87   g_static_mutex_lock (&test_g_static_private_mutex);
88   test_g_static_private_counter++;
89   g_static_mutex_unlock (&test_g_static_private_mutex);  
90   return g_new (guint,1);
91 }
92
93 static void
94 test_g_static_private_destructor (gpointer data)
95 {
96   g_static_mutex_lock (&test_g_static_private_mutex);
97   test_g_static_private_counter--;
98   g_static_mutex_unlock (&test_g_static_private_mutex);  
99   g_free (data);
100 }
101
102
103 static void
104 test_g_static_private_thread (gpointer data)
105 {
106   guint number = GPOINTER_TO_INT (data);
107   guint i;
108   guint* private;
109   for (i = 0; i < 10; i++)
110     {
111       number = number * 11 + 1; /* A very simple and bad RNG ;-) */
112       private = g_static_private_get (&test_g_static_private_private);
113       if (!private || number % 7 > 3)
114         {
115           private = test_g_static_private_constructor ();
116           g_static_private_set (&test_g_static_private_private, private,
117                                 test_g_static_private_destructor);
118         }
119       *private = number;
120       g_usleep (G_MICROSEC / 5);
121       g_assert (number == *private);
122     }
123 }
124
125 static void
126 test_g_static_private (void)
127 {
128   GThread *threads[THREADS];
129   guint i;
130   for (i = 0; i < THREADS; i++)
131     {
132       threads[i] = g_thread_create (test_g_static_private_thread, 
133                                     GINT_TO_POINTER (i),
134                                     0, TRUE, TRUE, 
135                                     G_THREAD_PRIORITY_NORMAL);      
136     }
137   for (i = 0; i < THREADS; i++)
138     {
139       g_thread_join (threads[i]);
140     }
141   g_assert (test_g_static_private_counter == 0); 
142 }
143
144 /* GStaticRWLock */
145
146 /* -1 = writing; >0 = # of readers */
147 static gint test_g_static_rw_lock_state = 0; 
148 G_LOCK_DEFINE (test_g_static_rw_lock_state);
149
150 static gboolean test_g_static_rw_lock_run = TRUE; 
151 static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
152
153 static void
154 test_g_static_rw_lock_thread (gpointer data)
155 {
156   while (test_g_static_rw_lock_run)
157     {
158       if (g_random_double() > .2) /* I'm a reader */
159         {
160           
161           if (g_random_double() > .2) /* I'll block */
162             g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
163           else /* I'll only try */
164             if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
165               continue;
166           G_LOCK (test_g_static_rw_lock_state);
167           g_assert (test_g_static_rw_lock_state >= 0);
168           test_g_static_rw_lock_state++;
169           G_UNLOCK (test_g_static_rw_lock_state);
170
171           g_usleep (10);
172
173           G_LOCK (test_g_static_rw_lock_state);
174           test_g_static_rw_lock_state--;
175           G_UNLOCK (test_g_static_rw_lock_state);
176
177           g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
178         }
179       else /* I'm a writer */
180         {
181           
182           if (g_random_double() > .2) /* I'll block */ 
183             g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
184           else /* I'll only try */
185             if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
186               continue;
187           G_LOCK (test_g_static_rw_lock_state);
188           g_assert (test_g_static_rw_lock_state == 0);
189           test_g_static_rw_lock_state = -1;
190           G_UNLOCK (test_g_static_rw_lock_state);
191
192           g_usleep (10);
193
194           G_LOCK (test_g_static_rw_lock_state);
195           test_g_static_rw_lock_state = 0;
196           G_UNLOCK (test_g_static_rw_lock_state);
197
198           g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
199         }
200     }
201 }
202
203 static void
204 test_g_static_rw_lock ()
205 {
206   GThread *threads[THREADS];
207   guint i;
208   for (i = 0; i < THREADS; i++)
209     {
210       threads[i] = g_thread_create (test_g_static_rw_lock_thread, 
211                                     0, 0, TRUE, TRUE, 
212                                     G_THREAD_PRIORITY_NORMAL);      
213     }
214   g_usleep (G_MICROSEC);
215   test_g_static_rw_lock_run = FALSE;
216   for (i = 0; i < THREADS; i++)
217     {
218       g_thread_join (threads[i]);
219     }
220   g_assert (test_g_static_rw_lock_state == 0);
221 }
222
223 /* run all the tests */
224 int 
225 main (int   argc,
226       char *argv[])
227 {
228   /* Only run the test, if threads are enabled and a default thread
229      implementation is available */
230 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
231   g_thread_init (NULL);
232   test_g_mutex ();
233   test_g_static_rec_mutex ();
234   test_g_static_private ();
235   test_g_static_rw_lock ();
236 #endif
237   return 0;
238 }