1 #undef G_DISABLE_ASSERT
9 #define DEBUG_MSG(args)
10 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
11 #define PRINT_MSG(args)
12 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
14 #define MAX_THREADS 50
15 #define MAX_SORTS 5 /* only applies if
16 ASYC_QUEUE_DO_SORT is set to 1 */
17 #define MAX_TIME 20 /* seconds */
18 #define MIN_TIME 5 /* seconds */
20 #define SORT_QUEUE_AFTER 1
21 #define SORT_QUEUE_ON_PUSH 1 /* if this is done, the
22 SORT_QUEUE_AFTER is ignored */
23 #define QUIT_WHEN_DONE 1
26 #if SORT_QUEUE_ON_PUSH == 1
27 # undef SORT_QUEUE_AFTER
28 # define SORT_QUEUE_AFTER 0
32 static GMainLoop *main_loop = NULL;
33 static GThreadPool *thread_pool = NULL;
34 static GAsyncQueue *async_queue = NULL;
38 sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
43 id1 = GPOINTER_TO_INT (p1);
44 id2 = GPOINTER_TO_INT (p2);
46 DEBUG_MSG (("comparing #1:%d and #2:%d, returning %d",
47 id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1)));
49 return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
53 sort_queue (gpointer user_data)
55 static gint sorts = 0;
56 static gpointer last_p = NULL;
58 gboolean can_quit = FALSE;
63 sort_multiplier = GPOINTER_TO_INT (user_data);
65 if (SORT_QUEUE_AFTER) {
66 PRINT_MSG (("sorting async queue..."));
67 g_async_queue_sort (async_queue, sort_compare, NULL);
71 if (sorts >= sort_multiplier) {
75 g_async_queue_sort (async_queue, sort_compare, NULL);
76 len = g_async_queue_length (async_queue);
78 PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len));
81 len = g_async_queue_length (async_queue);
82 DEBUG_MSG (("printing queue (size:%d)...", len));
85 for (i = 0, last_p = NULL; i < len; i++) {
86 p = g_async_queue_pop (async_queue);
87 DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p)));
90 g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p));
96 if (can_quit && QUIT_WHEN_DONE) {
97 g_main_loop_quit (main_loop);
104 enter_thread (gpointer data, gpointer user_data)
110 id = GPOINTER_TO_INT (data);
112 ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000);
113 DEBUG_MSG (("entered thread with id:%d, adding to queue in:%ld ms", id, ms));
115 g_usleep (ms * 1000);
117 if (SORT_QUEUE_ON_PUSH) {
118 g_async_queue_push_sorted (async_queue, GINT_TO_POINTER (id), sort_compare, NULL);
120 g_async_queue_push (async_queue, GINT_TO_POINTER (id));
123 len = g_async_queue_length (async_queue);
125 DEBUG_MSG (("thread id:%d added to async queue (size:%d)",
130 main (int argc, char *argv[])
132 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
134 gint max_threads = MAX_THREADS;
135 gint max_unused_threads = MAX_THREADS;
136 gint sort_multiplier = MAX_SORTS;
140 g_thread_init (NULL);
142 PRINT_MSG (("creating async queue..."));
143 async_queue = g_async_queue_new ();
145 g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE);
147 PRINT_MSG (("creating thread pool with max threads:%d, max unused threads:%d...",
148 max_threads, max_unused_threads));
149 thread_pool = g_thread_pool_new (enter_thread,
155 g_return_val_if_fail (thread_pool != NULL, EXIT_FAILURE);
157 g_thread_pool_set_max_unused_threads (max_unused_threads);
159 PRINT_MSG (("creating threads..."));
160 for (i = 1; i <= max_threads; i++) {
161 GError *error = NULL;
163 g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
165 g_assert_no_error (error);
168 if (!SORT_QUEUE_AFTER) {
172 sort_interval = ((MAX_TIME / sort_multiplier) + 2) * 1000;
173 g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier));
175 if (SORT_QUEUE_ON_PUSH) {
176 msg = "sorting when pushing into the queue, checking queue is sorted";
181 PRINT_MSG (("%s %d %s %d ms",
184 sort_multiplier == 1 ? "time in" : "times, once every",
187 DEBUG_MSG (("entering main event loop"));
189 main_loop = g_main_loop_new (NULL, FALSE);
190 g_main_loop_run (main_loop);