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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
#include "config.h"
#include <glib.h>
#define WAIT 5 /* seconds */
#define MAX_THREADS 10
/* if > 0 the test will run continuously (since the test ends when
* thread count is 0), if -1 it means no limit to unused threads
* if 0 then no unused threads are possible */
#define MAX_UNUSED_THREADS -1
G_LOCK_DEFINE_STATIC (thread_counter_pools);
static gulong abs_thread_counter = 0;
static gulong running_thread_counter = 0;
static gulong leftover_task_counter = 0;
static GThreadPool *idle_pool = NULL;
static void
test_threadpool_functions (void)
{
gint max_unused_threads;
guint max_idle_time;
/* This function attempts to call functions which don't need a
* threadpool to operate to make sure no uninitialised pointers
* accessed and no errors occur.
*/
max_unused_threads = 3;
g_thread_pool_set_max_unused_threads (max_unused_threads);
g_assert_cmpint (g_thread_pool_get_max_unused_threads (), ==,
max_unused_threads);
g_assert_cmpint (g_thread_pool_get_num_unused_threads (), ==, 0);
g_thread_pool_stop_unused_threads ();
max_idle_time = 10 * G_USEC_PER_SEC;
g_thread_pool_set_max_idle_time (max_idle_time);
g_assert_cmpint (g_thread_pool_get_max_idle_time (), ==, max_idle_time);
g_thread_pool_set_max_idle_time (0);
g_assert_cmpint (g_thread_pool_get_max_idle_time (), ==, 0);
}
static void
test_threadpool_stop_unused (void)
{
GThreadPool *pool;
guint i;
guint limit = 100;
/* Spawn a few threads. */
g_thread_pool_set_max_unused_threads (-1);
pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
for (i = 0; i < limit; i++)
g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
/* Wait for the threads to migrate. */
while (g_thread_pool_get_num_threads (pool) != 0)
g_usleep (100);
g_assert_cmpuint (g_thread_pool_get_num_threads (pool), ==, 0);
g_assert_cmpuint (g_thread_pool_get_num_unused_threads (), >, 0);
/* Wait for threads to die. */
g_thread_pool_stop_unused_threads ();
while (g_thread_pool_get_num_unused_threads () != 0)
g_usleep (100);
g_assert_cmpuint (g_thread_pool_get_num_unused_threads (), ==, 0);
g_thread_pool_set_max_unused_threads (MAX_THREADS);
g_assert_cmpuint (g_thread_pool_get_num_threads (pool), ==, 0);
g_assert_cmpuint (g_thread_pool_get_num_unused_threads (), ==, 0);
g_thread_pool_free (pool, FALSE, TRUE);
}
static void
test_threadpool_stop_unused_multiple (void)
{
GThreadPool *pools[10];
guint i, j;
const guint limit = 10;
gboolean all_stopped;
/* Spawn a few threads. */
g_thread_pool_set_max_unused_threads (-1);
for (i = 0; i < G_N_ELEMENTS (pools); i++)
{
pools[i] = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
for (j = 0; j < limit; j++)
g_thread_pool_push (pools[i], GUINT_TO_POINTER (100), NULL);
}
all_stopped = FALSE;
while (!all_stopped)
{
all_stopped = TRUE;
for (i = 0; i < G_N_ELEMENTS (pools); i++)
all_stopped &= (g_thread_pool_get_num_threads (pools[i]) == 0);
}
for (i = 0; i < G_N_ELEMENTS (pools); i++)
{
g_assert_cmpuint (g_thread_pool_get_num_threads (pools[i]), ==, 0);
g_assert_cmpuint (g_thread_pool_get_num_unused_threads (), >, 0);
}
/* Wait for threads to die. */
g_thread_pool_stop_unused_threads ();
while (g_thread_pool_get_num_unused_threads () != 0)
g_usleep (100);
g_assert_cmpuint (g_thread_pool_get_num_unused_threads (), ==, 0);
for (i = 0; i < G_N_ELEMENTS (pools); i++)
g_thread_pool_free (pools[i], FALSE, TRUE);
}
static void
test_threadpool_pools_entry_func (gpointer data, gpointer user_data)
{
G_LOCK (thread_counter_pools);
abs_thread_counter++;
running_thread_counter++;
G_UNLOCK (thread_counter_pools);
g_usleep (g_random_int_range (0, 4000));
G_LOCK (thread_counter_pools);
running_thread_counter--;
leftover_task_counter--;
G_UNLOCK (thread_counter_pools);
}
static void
test_threadpool_pools (void)
{
GThreadPool *pool1, *pool2, *pool3;
guint runs;
guint i;
pool1 = g_thread_pool_new ((GFunc) test_threadpool_pools_entry_func, NULL, 3, FALSE, NULL);
pool2 = g_thread_pool_new ((GFunc) test_threadpool_pools_entry_func, NULL, 5, TRUE, NULL);
pool3 = g_thread_pool_new ((GFunc) test_threadpool_pools_entry_func, NULL, 7, TRUE, NULL);
runs = 300;
for (i = 0; i < runs; i++)
{
g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
G_LOCK (thread_counter_pools);
leftover_task_counter += 3;
G_UNLOCK (thread_counter_pools);
}
g_thread_pool_free (pool1, TRUE, TRUE);
g_thread_pool_free (pool2, FALSE, TRUE);
g_thread_pool_free (pool3, FALSE, TRUE);
g_assert_cmpint (runs * 3, ==, abs_thread_counter + leftover_task_counter);
g_assert_cmpint (running_thread_counter, ==, 0);
}
static gint
test_threadpool_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
{
guint32 id1, id2;
id1 = GPOINTER_TO_UINT (a);
id2 = GPOINTER_TO_UINT (b);
return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
}
static void
test_threadpool_sort_entry_func (gpointer data, gpointer user_data)
{
guint thread_id;
gboolean is_sorted;
static GMutex last_thread_mutex;
static guint last_thread_id = 0;
g_mutex_lock (&last_thread_mutex);
thread_id = GPOINTER_TO_UINT (data);
is_sorted = GPOINTER_TO_INT (user_data);
if (is_sorted)
{
if (last_thread_id != 0)
g_assert_cmpint (last_thread_id, <=, thread_id);
last_thread_id = thread_id;
}
g_mutex_unlock (&last_thread_mutex);
g_usleep (WAIT * 1000);
}
static void
test_threadpool_sort (gconstpointer data)
{
gboolean sort = GPOINTER_TO_UINT (data);
GThreadPool *pool;
guint limit;
guint max_threads;
guint i;
GError *local_error = NULL;
limit = MAX_THREADS * 10;
if (sort) {
max_threads = 1;
} else {
max_threads = MAX_THREADS;
}
/* It is important that we only have a maximum of 1 thread for this
* test since the results can not be guaranteed to be sorted if > 1.
*
* Threads are scheduled by the operating system and are executed at
* random. It cannot be assumed that threads are executed in the
* order they are created. This was discussed in bug #334943.
*
* However, if testing sorting, we start with max-threads=0 so that all the
* work can be enqueued before starting the pool. This prevents a race between
* new work being enqueued out of sorted order, and work being pulled off the
* queue.
*/
pool = g_thread_pool_new (test_threadpool_sort_entry_func,
GINT_TO_POINTER (sort),
sort ? 0 : max_threads,
FALSE,
NULL);
g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
if (sort) {
g_thread_pool_set_sort_function (pool,
test_threadpool_sort_compare_func,
NULL);
}
for (i = 0; i < limit; i++) {
guint id;
id = g_random_int_range (1, limit) + 1;
g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
g_test_message ("%s ===> pushed new thread with id:%d, number "
"of threads:%d, unprocessed:%d",
sort ? "[ sorted]" : "[unsorted]",
id,
g_thread_pool_get_num_threads (pool),
g_thread_pool_unprocessed (pool));
}
if (sort)
{
g_test_message ("Starting thread pool processing");
g_thread_pool_set_max_threads (pool, max_threads, &local_error);
g_assert_no_error (local_error);
}
g_assert_cmpint (g_thread_pool_get_max_threads (pool), ==, (gint) max_threads);
g_assert_cmpuint (g_thread_pool_get_num_threads (pool), <=,
(guint) g_thread_pool_get_max_threads (pool));
g_thread_pool_free (pool, TRUE, TRUE);
}
static void
test_threadpool_idle_time_entry_func (gpointer data, gpointer user_data)
{
g_usleep (WAIT * 1000);
}
static gboolean
test_threadpool_idle_timeout (gpointer data)
{
gboolean *idle_timeout_called = data;
gint i;
*idle_timeout_called = TRUE;
for (i = 0; i < 2; i++) {
g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
}
g_main_context_wakeup (NULL);
return G_SOURCE_REMOVE;
}
static gboolean
poll_cb (gpointer data)
{
g_main_context_wakeup (NULL);
return G_SOURCE_CONTINUE;
}
static void
test_threadpool_idle_time (void)
{
guint limit = 50;
guint interval = 10000;
guint i;
guint idle;
gboolean idle_timeout_called = FALSE;
GSource *timeout_source = NULL;
GSource *poll_source = NULL;
idle_pool = g_thread_pool_new (test_threadpool_idle_time_entry_func,
NULL,
0,
FALSE,
NULL);
g_thread_pool_set_max_threads (idle_pool, MAX_THREADS, NULL);
g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
g_thread_pool_set_max_idle_time (interval);
g_assert_cmpint (g_thread_pool_get_max_threads (idle_pool), ==,
MAX_THREADS);
g_assert_cmpint (g_thread_pool_get_max_unused_threads (), ==,
MAX_UNUSED_THREADS);
g_assert_cmpint (g_thread_pool_get_max_idle_time (), ==, interval);
for (i = 0; i < limit; i++) {
g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
}
g_assert_cmpint (g_thread_pool_unprocessed (idle_pool), <=, limit);
timeout_source = g_timeout_source_new (interval - 1000);
g_source_set_callback (timeout_source, test_threadpool_idle_timeout, &idle_timeout_called, NULL);
g_source_attach (timeout_source, NULL);
/* Wait until the idle timeout has been called at least once and there are no
* unused threads. We need a second timeout for this, to periodically wake
* the main context up, as there’s no way to be notified of changes to `idle`.
*/
poll_source = g_timeout_source_new (500);
g_source_set_callback (poll_source, poll_cb, NULL, NULL);
g_source_attach (poll_source, NULL);
idle = g_thread_pool_get_num_unused_threads ();
while (!idle_timeout_called || idle > 0)
{
g_test_message ("Pool idle thread count: %d, unprocessed jobs: %d",
idle, g_thread_pool_unprocessed (idle_pool));
g_main_context_iteration (NULL, TRUE);
idle = g_thread_pool_get_num_unused_threads ();
}
g_thread_pool_free (idle_pool, FALSE, TRUE);
g_source_destroy (poll_source);
g_source_unref (poll_source);
g_source_destroy (timeout_source);
g_source_unref (timeout_source);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/threadpool/functions", test_threadpool_functions);
g_test_add_func ("/threadpool/stop-unused", test_threadpool_stop_unused);
g_test_add_func ("/threadpool/pools", test_threadpool_pools);
g_test_add_data_func ("/threadpool/no-sort", GUINT_TO_POINTER (FALSE), test_threadpool_sort);
g_test_add_data_func ("/threadpool/sort", GUINT_TO_POINTER (TRUE), test_threadpool_sort);
g_test_add_func ("/threadpool/stop-unused-multiple", test_threadpool_stop_unused_multiple);
g_test_add_func ("/threadpool/idle-time", test_threadpool_idle_time);
return g_test_run ();
}
|