File: sources.c

package info (click to toggle)
glib2.0 2.58.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-proposed-updates
  • size: 48,744 kB
  • sloc: ansic: 452,196; xml: 16,781; python: 6,149; makefile: 3,776; sh: 1,499; perl: 1,140; cpp: 9
file content (185 lines) | stat: -rw-r--r-- 5,157 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2012 Red Hat, Inc
 */

#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#include <glib.h>

#define NSOURCES 50000

static gboolean
callback (gpointer user_data)
{
  g_assert_not_reached ();
  return FALSE;
}

static void
shuffle (GSource **sources, int num)
{
  int i, a, b;
  GSource *tmp;

  for (i = 0; i < num * 10; i++)
    {
      a = g_random_int_range (0, num);
      b = g_random_int_range (0, num);
      tmp = sources[a];
      sources[a] = sources[b];
      sources[b] = tmp;
    }
}

static void
thread_pool_attach_func (gpointer data,
                         gpointer user_data)
{
  GMainContext *context = user_data;
  GSource *source = data;

  g_source_attach (source, context);
  g_source_unref (source);
}

static void
thread_pool_destroy_func (gpointer data,
                          gpointer user_data)
{
  GSource *source = data;

  g_source_destroy (source);
}

int
main (int argc, char **argv)
{
  int i;
  gint64 start;
  gint64 end;
  GMainContext *context;
  GSource **sources;
  GThreadPool *pool;
  GError *error = NULL;

  context = g_main_context_default ();
  sources = g_new0 (GSource *, NSOURCES);

  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    {
      sources[i] = g_idle_source_new ();
      g_source_set_callback (sources[i], callback, NULL, NULL);
      g_source_attach (sources[i], context);
    }
  end = g_get_monotonic_time ();
  g_print ("Add same-priority sources: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);

#ifdef SLOW
  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
  end = g_get_monotonic_time ();
  g_print ("Find each source: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);
#endif

  shuffle (sources, NSOURCES);

  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    {
      g_source_destroy (sources[i]);
      g_source_unref (sources[i]);
    }
  end = g_get_monotonic_time ();
  g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);

  /* Make sure they really did get removed */
  g_main_context_iteration (context, FALSE);

  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    {
      sources[i] = g_idle_source_new ();
      g_source_set_callback (sources[i], callback, NULL, NULL);
      g_source_set_priority (sources[i], i % 100);
      g_source_attach (sources[i], context);
    }
  end = g_get_monotonic_time ();
  g_print ("Add different-priority sources: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);

#ifdef SLOW
  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    g_assert (sources[i] == g_main_context_find_source_by_id (context, g_source_get_id (sources[i])));
  end = g_get_monotonic_time ();
  g_print ("Find each source: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);
#endif

  shuffle (sources, NSOURCES);

  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    {
      g_source_destroy (sources[i]);
      g_source_unref (sources[i]);
    }
  end = g_get_monotonic_time ();
  g_print ("Remove in random order: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);

  /* Make sure they really did get removed */
  g_main_context_iteration (context, FALSE);

  pool = g_thread_pool_new (thread_pool_attach_func, context,
                            20, TRUE, NULL);
  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    {
      sources[i] = g_idle_source_new ();
      g_source_set_callback (sources[i], callback, NULL, NULL);
      g_thread_pool_push (pool, sources[i], &error);
      g_assert_no_error (error);
    }
  g_thread_pool_free (pool, FALSE, TRUE);
  end = g_get_monotonic_time ();
  g_print ("Add sources from threads: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);

  pool = g_thread_pool_new (thread_pool_destroy_func, context,
                            20, TRUE, NULL);
  start = g_get_monotonic_time ();
  for (i = 0; i < NSOURCES; i++)
    {
      g_thread_pool_push (pool, sources[i], &error);
      g_assert_no_error (error);
    }
  g_thread_pool_free (pool, FALSE, TRUE);
  end = g_get_monotonic_time ();
  g_print ("Remove sources from threads: %" G_GINT64_FORMAT "\n",
           (end - start) / 1000);

  /* Make sure they really did get removed */
  g_main_context_iteration (context, FALSE);

  g_free (sources);
  return 0;
}