win32: fix tests/sources.c
[platform/upstream/glib.git] / tests / sources.c
1 /* This library is free software; you can redistribute it and/or
2  * modify it under the terms of the GNU Lesser General Public
3  * License as published by the Free Software Foundation; either
4  * version 2 of the License, or (at your option) any later version.
5  *
6  * This library is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9  * Lesser General Public License for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public
12  * License along with this library; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 02111-1307, USA.
15  *
16  * Copyright 2012 Red Hat, Inc
17  */
18
19 #undef G_DISABLE_ASSERT
20 #undef G_LOG_DOMAIN
21
22 #include <glib.h>
23
24 #define NSOURCES 50000
25
26 static gboolean
27 callback (gpointer user_data)
28 {
29   g_assert_not_reached ();
30   return FALSE;
31 }
32
33 static void
34 shuffle (GSource **sources, int num)
35 {
36   int i, a, b;
37   GSource *tmp;
38
39   for (i = 0; i < num * 10; i++)
40     {
41       a = g_random_int_range (0, num);
42       b = g_random_int_range (0, num);
43       tmp = sources[a];
44       sources[a] = sources[b];
45       sources[b] = tmp;
46     }
47 }
48
49 static void
50 thread_pool_attach_func (gpointer data,
51                          gpointer user_data)
52 {
53   GMainContext *context = user_data;
54   GSource *source = data;
55
56   g_source_attach (source, context);
57 }
58
59 static void
60 thread_pool_destroy_func (gpointer data,
61                           gpointer user_data)
62 {
63   GSource *source = data;
64
65   g_source_destroy (source);
66 }
67
68 int
69 main (int argc, char **argv)
70 {
71   int i;
72   gint64 start;
73   gint64 end;
74   GMainContext *context;
75   GSource **sources;
76   GThreadPool *pool;
77   GError *error = NULL;
78
79   context = g_main_context_default ();
80   sources = g_new0 (GSource *, NSOURCES);
81
82   start = g_get_monotonic_time ();
83   for (i = 0; i < NSOURCES; i++)
84     {
85       sources[i] = g_idle_source_new ();
86       g_source_set_callback (sources[i], callback, NULL, NULL);
87       g_source_attach (sources[i], context);
88     }
89   end = g_get_monotonic_time ();
90   g_print ("Add same-priority sources: %" G_GINT64_FORMAT "\n",
91            (end - start) / 1000);
92
93 #ifdef SLOW
94   start = g_get_monotonic_time ();
95   for (i = 0; i < NSOURCES; i++)
96     g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
97   end = g_get_monotonic_time ();
98   g_print ("Find each source: %" G_GINT64_FORMAT "\n",
99            (end - start) / 1000);
100 #endif
101
102   shuffle (sources, NSOURCES);
103
104   start = g_get_monotonic_time ();
105   for (i = 0; i < NSOURCES; i++)
106     g_source_destroy (sources[i]);
107   end = g_get_monotonic_time ();
108   g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
109            (end - start) / 1000);
110
111   /* Make sure they really did get removed */
112   g_main_context_iteration (context, FALSE);
113
114   start = g_get_monotonic_time ();
115   for (i = 0; i < NSOURCES; i++)
116     {
117       sources[i] = g_idle_source_new ();
118       g_source_set_callback (sources[i], callback, NULL, NULL);
119       g_source_set_priority (sources[i], i % 100);
120       g_source_attach (sources[i], context);
121     }
122   end = g_get_monotonic_time ();
123   g_print ("Add different-priority sources: %" G_GINT64_FORMAT "\n",
124            (end - start) / 1000);
125
126 #ifdef SLOW
127   start = g_get_monotonic_time ();
128   for (i = 0; i < NSOURCES; i++)
129     g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
130   end = g_get_monotonic_time ();
131   g_print ("Find each source: %" G_GINT64_FORMAT "\n",
132            (end - start) / 1000);
133 #endif
134
135   shuffle (sources, NSOURCES);
136
137   start = g_get_monotonic_time ();
138   for (i = 0; i < NSOURCES; i++)
139     g_source_destroy (sources[i]);
140   end = g_get_monotonic_time ();
141   g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
142            (end - start) / 1000);
143
144   /* Make sure they really did get removed */
145   g_main_context_iteration (context, FALSE);
146
147   pool = g_thread_pool_new (thread_pool_attach_func, context,
148                             20, TRUE, NULL);
149   start = g_get_monotonic_time ();
150   for (i = 0; i < NSOURCES; i++)
151     {
152       sources[i] = g_idle_source_new ();
153       g_source_set_callback (sources[i], callback, NULL, NULL);
154       g_thread_pool_push (pool, sources[i], &error);
155       g_assert_no_error (error);
156     }
157   g_thread_pool_free (pool, FALSE, TRUE);
158   end = g_get_monotonic_time ();
159   g_print ("Add sources from threads: %" G_GINT64_FORMAT "\n",
160            (end - start) / 1000);
161
162   pool = g_thread_pool_new (thread_pool_destroy_func, context,
163                             20, TRUE, NULL);
164   start = g_get_monotonic_time ();
165   for (i = 0; i < NSOURCES; i++)
166     {
167       g_thread_pool_push (pool, sources[i], &error);
168       g_assert_no_error (error);
169     }
170   g_thread_pool_free (pool, FALSE, TRUE);
171   end = g_get_monotonic_time ();
172   g_print ("Remove sources from threads: %" G_GINT64_FORMAT "\n",
173            (end - start) / 1000);
174
175   /* Make sure they really did get removed */
176   g_main_context_iteration (context, FALSE);
177
178   return 0;
179 }