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.
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.
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.
16 * Copyright 2012 Red Hat, Inc
19 #undef G_DISABLE_ASSERT
23 #include <sys/resource.h>
27 #define NSOURCES 50000
30 callback (gpointer user_data)
32 g_assert_not_reached ();
37 shuffle (GSource **sources, int num)
42 for (i = 0; i < num * 10; i++)
44 a = g_random_int_range (0, num);
45 b = g_random_int_range (0, num);
47 sources[a] = sources[b];
53 thread_pool_attach_func (gpointer data,
56 GMainContext *context = user_data;
57 GSource *source = data;
59 g_source_attach (source, context);
63 thread_pool_destroy_func (gpointer data,
66 GSource *source = data;
68 g_source_destroy (source);
72 difftimeval (struct timeval *old, struct timeval *new)
75 (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
79 main (int argc, char **argv)
82 struct rusage old_usage;
83 struct rusage new_usage;
84 GMainContext *context;
89 context = g_main_context_default ();
90 sources = g_new0 (GSource *, NSOURCES);
92 getrusage (RUSAGE_SELF, &old_usage);
93 for (i = 0; i < NSOURCES; i++)
95 sources[i] = g_idle_source_new ();
96 g_source_set_callback (sources[i], callback, NULL, NULL);
97 g_source_attach (sources[i], context);
99 getrusage (RUSAGE_SELF, &new_usage);
100 g_print ("Add same-priority sources: %g\n",
101 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
104 getrusage (RUSAGE_SELF, &old_usage);
105 for (i = 0; i < NSOURCES; i++)
106 g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
107 getrusage (RUSAGE_SELF, &new_usage);
108 g_print ("Find each source: %g\n",
109 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
112 shuffle (sources, NSOURCES);
114 getrusage (RUSAGE_SELF, &old_usage);
115 for (i = 0; i < NSOURCES; i++)
116 g_source_destroy (sources[i]);
117 getrusage (RUSAGE_SELF, &new_usage);
118 g_print ("Remove in random order: %g\n",
119 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
121 /* Make sure they really did get removed */
122 g_main_context_iteration (context, FALSE);
124 getrusage (RUSAGE_SELF, &old_usage);
125 for (i = 0; i < NSOURCES; i++)
127 sources[i] = g_idle_source_new ();
128 g_source_set_callback (sources[i], callback, NULL, NULL);
129 g_source_set_priority (sources[i], i % 100);
130 g_source_attach (sources[i], context);
132 getrusage (RUSAGE_SELF, &new_usage);
133 g_print ("Add different-priority sources: %g\n",
134 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
137 getrusage (RUSAGE_SELF, &old_usage);
138 for (i = 0; i < NSOURCES; i++)
139 g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
140 getrusage (RUSAGE_SELF, &new_usage);
141 g_print ("Find each source: %g\n",
142 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
145 shuffle (sources, NSOURCES);
147 getrusage (RUSAGE_SELF, &old_usage);
148 for (i = 0; i < NSOURCES; i++)
149 g_source_destroy (sources[i]);
150 getrusage (RUSAGE_SELF, &new_usage);
151 g_print ("Remove in random order: %g\n",
152 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
154 /* Make sure they really did get removed */
155 g_main_context_iteration (context, FALSE);
157 pool = g_thread_pool_new (thread_pool_attach_func, context,
159 getrusage (RUSAGE_SELF, &old_usage);
160 for (i = 0; i < NSOURCES; i++)
162 sources[i] = g_idle_source_new ();
163 g_source_set_callback (sources[i], callback, NULL, NULL);
164 g_thread_pool_push (pool, sources[i], &error);
165 g_assert_no_error (error);
167 g_thread_pool_free (pool, FALSE, TRUE);
168 getrusage (RUSAGE_SELF, &new_usage);
169 g_print ("Add sources from threads: %g\n",
170 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
172 pool = g_thread_pool_new (thread_pool_destroy_func, context,
174 getrusage (RUSAGE_SELF, &old_usage);
175 for (i = 0; i < NSOURCES; i++)
177 g_thread_pool_push (pool, sources[i], &error);
178 g_assert_no_error (error);
180 g_thread_pool_free (pool, FALSE, TRUE);
181 getrusage (RUSAGE_SELF, &new_usage);
182 g_print ("Remove sources from threads: %g\n",
183 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
185 /* Make sure they really did get removed */
186 g_main_context_iteration (context, FALSE);