/bluetooth/glib/tests/threadpool-test.c

https://github.com/fards/Dell_Streak_5_405_Gingerbread_2.6.35 · C · 476 lines · 340 code · 110 blank · 26 comment · 39 complexity · 7f43c23b20a009ae0e0db1f7fc39474b MD5 · raw file

  1. #undef G_DISABLE_ASSERT
  2. #undef G_LOG_DOMAIN
  3. #include "config.h"
  4. #include <glib.h>
  5. #define DEBUG_MSG(x)
  6. /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
  7. #define WAIT 5 /* seconds */
  8. #define MAX_THREADS 10
  9. /* if > 0 the test will run continously (since the test ends when
  10. * thread count is 0), if -1 it means no limit to unused threads
  11. * if 0 then no unused threads are possible */
  12. #define MAX_UNUSED_THREADS -1
  13. G_LOCK_DEFINE_STATIC (thread_counter_pools);
  14. static gulong abs_thread_counter = 0;
  15. static gulong running_thread_counter = 0;
  16. static gulong leftover_task_counter = 0;
  17. G_LOCK_DEFINE_STATIC (last_thread);
  18. static guint last_thread_id = 0;
  19. G_LOCK_DEFINE_STATIC (thread_counter_sort);
  20. static gulong sort_thread_counter = 0;
  21. static GThreadPool *idle_pool = NULL;
  22. static GMainLoop *main_loop = NULL;
  23. static void
  24. test_thread_functions (void)
  25. {
  26. gint max_unused_threads;
  27. guint max_idle_time;
  28. /* This function attempts to call functions which don't need a
  29. * threadpool to operate to make sure no uninitialised pointers
  30. * accessed and no errors occur.
  31. */
  32. max_unused_threads = 3;
  33. DEBUG_MSG (("[funcs] Setting max unused threads to %d",
  34. max_unused_threads));
  35. g_thread_pool_set_max_unused_threads (max_unused_threads);
  36. DEBUG_MSG (("[funcs] Getting max unused threads = %d",
  37. g_thread_pool_get_max_unused_threads ()));
  38. g_assert (g_thread_pool_get_max_unused_threads() == max_unused_threads);
  39. DEBUG_MSG (("[funcs] Getting num unused threads = %d",
  40. g_thread_pool_get_num_unused_threads ()));
  41. g_assert (g_thread_pool_get_num_unused_threads () == 0);
  42. DEBUG_MSG (("[funcs] Stopping unused threads"));
  43. g_thread_pool_stop_unused_threads ();
  44. max_idle_time = 10 * G_USEC_PER_SEC;
  45. DEBUG_MSG (("[funcs] Setting max idle time to %d",
  46. max_idle_time));
  47. g_thread_pool_set_max_idle_time (max_idle_time);
  48. DEBUG_MSG (("[funcs] Getting max idle time = %d",
  49. g_thread_pool_get_max_idle_time ()));
  50. g_assert (g_thread_pool_get_max_idle_time () == max_idle_time);
  51. DEBUG_MSG (("[funcs] Setting max idle time to 0"));
  52. g_thread_pool_set_max_idle_time (0);
  53. DEBUG_MSG (("[funcs] Getting max idle time = %d",
  54. g_thread_pool_get_max_idle_time ()));
  55. g_assert (g_thread_pool_get_max_idle_time () == 0);
  56. }
  57. static void
  58. test_count_threads_foreach (GThread *thread,
  59. guint *count)
  60. {
  61. ++*count;
  62. }
  63. static guint
  64. test_count_threads (void)
  65. {
  66. guint count = 0;
  67. g_thread_foreach ((GFunc) test_count_threads_foreach, &count);
  68. /* Exclude main thread */
  69. return count - 1;
  70. }
  71. static void
  72. test_thread_stop_unused (void)
  73. {
  74. GThreadPool *pool;
  75. guint i;
  76. guint limit = 100;
  77. /* Spawn a few threads. */
  78. g_thread_pool_set_max_unused_threads (-1);
  79. pool = g_thread_pool_new ((GFunc) g_usleep, NULL, -1, FALSE, NULL);
  80. for (i = 0; i < limit; i++)
  81. g_thread_pool_push (pool, GUINT_TO_POINTER (1000), NULL);
  82. DEBUG_MSG (("[unused] ===> pushed %d threads onto the idle pool",
  83. limit));
  84. /* Wait for the threads to migrate. */
  85. g_usleep (G_USEC_PER_SEC);
  86. DEBUG_MSG (("[unused] current threads %d",
  87. test_count_threads()));
  88. DEBUG_MSG (("[unused] stopping unused threads"));
  89. g_thread_pool_stop_unused_threads ();
  90. DEBUG_MSG (("[unused] waiting ONE second for threads to die"));
  91. /* Some time for threads to die. */
  92. g_usleep (G_USEC_PER_SEC);
  93. DEBUG_MSG (("[unused] stopped idle threads, %d remain, %d threads still exist",
  94. g_thread_pool_get_num_unused_threads (),
  95. test_count_threads ()));
  96. g_assert (g_thread_pool_get_num_unused_threads () == test_count_threads ());
  97. g_assert (g_thread_pool_get_num_unused_threads () == 0);
  98. g_thread_pool_set_max_unused_threads (MAX_THREADS);
  99. DEBUG_MSG (("[unused] cleaning up thread pool"));
  100. g_thread_pool_free (pool, FALSE, TRUE);
  101. }
  102. static void
  103. test_thread_pools_entry_func (gpointer data, gpointer user_data)
  104. {
  105. guint id = 0;
  106. id = GPOINTER_TO_UINT (data);
  107. DEBUG_MSG (("[pool] ---> [%3.3d] entered thread.", id));
  108. G_LOCK (thread_counter_pools);
  109. abs_thread_counter++;
  110. running_thread_counter++;
  111. G_UNLOCK (thread_counter_pools);
  112. g_usleep (g_random_int_range (0, 4000));
  113. G_LOCK (thread_counter_pools);
  114. running_thread_counter--;
  115. leftover_task_counter--;
  116. DEBUG_MSG (("[pool] ---> [%3.3d] exiting thread (abs count:%ld, "
  117. "running count:%ld, left over:%ld)",
  118. id, abs_thread_counter,
  119. running_thread_counter, leftover_task_counter));
  120. G_UNLOCK (thread_counter_pools);
  121. }
  122. static void
  123. test_thread_pools (void)
  124. {
  125. GThreadPool *pool1, *pool2, *pool3;
  126. guint runs;
  127. guint i;
  128. pool1 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 3, FALSE, NULL);
  129. pool2 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 5, TRUE, NULL);
  130. pool3 = g_thread_pool_new ((GFunc)test_thread_pools_entry_func, NULL, 7, TRUE, NULL);
  131. runs = 300;
  132. for (i = 0; i < runs; i++)
  133. {
  134. g_thread_pool_push (pool1, GUINT_TO_POINTER (i + 1), NULL);
  135. g_thread_pool_push (pool2, GUINT_TO_POINTER (i + 1), NULL);
  136. g_thread_pool_push (pool3, GUINT_TO_POINTER (i + 1), NULL);
  137. G_LOCK (thread_counter_pools);
  138. leftover_task_counter += 3;
  139. G_UNLOCK (thread_counter_pools);
  140. }
  141. g_thread_pool_free (pool1, TRUE, TRUE);
  142. g_thread_pool_free (pool2, FALSE, TRUE);
  143. g_thread_pool_free (pool3, FALSE, TRUE);
  144. g_assert (runs * 3 == abs_thread_counter + leftover_task_counter);
  145. g_assert (running_thread_counter == 0);
  146. }
  147. static gint
  148. test_thread_sort_compare_func (gconstpointer a, gconstpointer b, gpointer user_data)
  149. {
  150. guint32 id1, id2;
  151. id1 = GPOINTER_TO_UINT (a);
  152. id2 = GPOINTER_TO_UINT (b);
  153. return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
  154. }
  155. static void
  156. test_thread_sort_entry_func (gpointer data, gpointer user_data)
  157. {
  158. guint thread_id;
  159. gboolean is_sorted;
  160. G_LOCK (last_thread);
  161. thread_id = GPOINTER_TO_UINT (data);
  162. is_sorted = GPOINTER_TO_INT (user_data);
  163. DEBUG_MSG (("%s ---> entered thread:%2.2d, last thread:%2.2d",
  164. is_sorted ? "[ sorted]" : "[unsorted]",
  165. thread_id, last_thread_id));
  166. if (is_sorted) {
  167. static gboolean last_failed = FALSE;
  168. if (last_thread_id > thread_id) {
  169. if (last_failed) {
  170. g_assert (last_thread_id <= thread_id);
  171. }
  172. /* Here we remember one fail and if it concurrently fails, it
  173. * can not be sorted. the last thread id might be < this thread
  174. * id if something is added to the queue since threads were
  175. * created
  176. */
  177. last_failed = TRUE;
  178. } else {
  179. last_failed = FALSE;
  180. }
  181. last_thread_id = thread_id;
  182. }
  183. G_UNLOCK (last_thread);
  184. g_usleep (WAIT * 1000);
  185. }
  186. static void
  187. test_thread_sort (gboolean sort)
  188. {
  189. GThreadPool *pool;
  190. guint limit;
  191. guint max_threads;
  192. gint i;
  193. limit = MAX_THREADS * 10;
  194. if (sort) {
  195. max_threads = 1;
  196. } else {
  197. max_threads = MAX_THREADS;
  198. }
  199. /* It is important that we only have a maximum of 1 thread for this
  200. * test since the results can not be guranteed to be sorted if > 1.
  201. *
  202. * Threads are scheduled by the operating system and are executed at
  203. * random. It cannot be assumed that threads are executed in the
  204. * order they are created. This was discussed in bug #334943.
  205. */
  206. pool = g_thread_pool_new (test_thread_sort_entry_func,
  207. GINT_TO_POINTER (sort),
  208. max_threads,
  209. FALSE,
  210. NULL);
  211. g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
  212. if (sort) {
  213. g_thread_pool_set_sort_function (pool,
  214. test_thread_sort_compare_func,
  215. GUINT_TO_POINTER (69));
  216. }
  217. for (i = 0; i < limit; i++) {
  218. guint id;
  219. id = g_random_int_range (1, limit) + 1;
  220. g_thread_pool_push (pool, GUINT_TO_POINTER (id), NULL);
  221. DEBUG_MSG (("%s ===> pushed new thread with id:%d, number "
  222. "of threads:%d, unprocessed:%d",
  223. sort ? "[ sorted]" : "[unsorted]",
  224. id,
  225. g_thread_pool_get_num_threads (pool),
  226. g_thread_pool_unprocessed (pool)));
  227. }
  228. g_assert (g_thread_pool_get_max_threads (pool) == max_threads);
  229. g_assert (g_thread_pool_get_num_threads (pool) == g_thread_pool_get_max_threads (pool));
  230. }
  231. static void
  232. test_thread_idle_time_entry_func (gpointer data, gpointer user_data)
  233. {
  234. guint thread_id;
  235. thread_id = GPOINTER_TO_UINT (data);
  236. DEBUG_MSG (("[idle] ---> entered thread:%2.2d", thread_id));
  237. g_usleep (WAIT * 1000);
  238. DEBUG_MSG (("[idle] <--- exiting thread:%2.2d", thread_id));
  239. }
  240. static gboolean
  241. test_thread_idle_timeout (gpointer data)
  242. {
  243. guint interval;
  244. gint i;
  245. interval = GPOINTER_TO_UINT (data);
  246. for (i = 0; i < 2; i++) {
  247. g_thread_pool_push (idle_pool, GUINT_TO_POINTER (100 + i), NULL);
  248. DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, number "
  249. "of threads:%d, unprocessed:%d",
  250. 100 + i,
  251. g_thread_pool_get_num_threads (idle_pool),
  252. g_thread_pool_unprocessed (idle_pool)));
  253. }
  254. return FALSE;
  255. }
  256. static void
  257. test_thread_idle_time ()
  258. {
  259. guint limit = 50;
  260. guint interval = 10000;
  261. gint i;
  262. idle_pool = g_thread_pool_new (test_thread_idle_time_entry_func,
  263. NULL,
  264. MAX_THREADS,
  265. FALSE,
  266. NULL);
  267. g_thread_pool_set_max_unused_threads (MAX_UNUSED_THREADS);
  268. g_thread_pool_set_max_idle_time (interval);
  269. g_assert (g_thread_pool_get_max_unused_threads () == MAX_UNUSED_THREADS);
  270. g_assert (g_thread_pool_get_max_idle_time () == interval);
  271. for (i = 0; i < limit; i++) {
  272. g_thread_pool_push (idle_pool, GUINT_TO_POINTER (i + 1), NULL);
  273. DEBUG_MSG (("[idle] ===> pushed new thread with id:%d, "
  274. "number of threads:%d, unprocessed:%d",
  275. i,
  276. g_thread_pool_get_num_threads (idle_pool),
  277. g_thread_pool_unprocessed (idle_pool)));
  278. }
  279. g_timeout_add ((interval - 1000),
  280. test_thread_idle_timeout,
  281. GUINT_TO_POINTER (interval));
  282. }
  283. static gboolean
  284. test_check_start_and_stop (gpointer user_data)
  285. {
  286. static guint test_number = 0;
  287. static gboolean run_next = FALSE;
  288. gboolean continue_timeout = TRUE;
  289. gboolean quit = TRUE;
  290. if (test_number == 0) {
  291. run_next = TRUE;
  292. DEBUG_MSG (("***** RUNNING TEST %2.2d *****", test_number));
  293. }
  294. if (run_next) {
  295. test_number++;
  296. switch (test_number) {
  297. case 1:
  298. test_thread_functions ();
  299. break;
  300. case 2:
  301. test_thread_stop_unused ();
  302. break;
  303. case 3:
  304. test_thread_pools ();
  305. break;
  306. case 4:
  307. test_thread_sort (FALSE);
  308. break;
  309. case 5:
  310. test_thread_sort (TRUE);
  311. break;
  312. case 6:
  313. test_thread_stop_unused ();
  314. break;
  315. case 7:
  316. test_thread_idle_time ();
  317. break;
  318. default:
  319. DEBUG_MSG (("***** END OF TESTS *****"));
  320. g_main_loop_quit (main_loop);
  321. continue_timeout = FALSE;
  322. break;
  323. }
  324. run_next = FALSE;
  325. return TRUE;
  326. }
  327. if (test_number == 3) {
  328. G_LOCK (thread_counter_pools);
  329. quit &= running_thread_counter <= 0;
  330. DEBUG_MSG (("***** POOL RUNNING THREAD COUNT:%ld",
  331. running_thread_counter));
  332. G_UNLOCK (thread_counter_pools);
  333. }
  334. if (test_number == 4 || test_number == 5) {
  335. G_LOCK (thread_counter_sort);
  336. quit &= sort_thread_counter <= 0;
  337. DEBUG_MSG (("***** POOL SORT THREAD COUNT:%ld",
  338. sort_thread_counter));
  339. G_UNLOCK (thread_counter_sort);
  340. }
  341. if (test_number == 7) {
  342. guint idle;
  343. idle = g_thread_pool_get_num_unused_threads ();
  344. quit &= idle < 1;
  345. DEBUG_MSG (("***** POOL IDLE THREAD COUNT:%d, UNPROCESSED JOBS:%d",
  346. idle, g_thread_pool_unprocessed (idle_pool)));
  347. }
  348. if (quit) {
  349. run_next = TRUE;
  350. }
  351. return continue_timeout;
  352. }
  353. int
  354. main (int argc, char *argv[])
  355. {
  356. /* Only run the test, if threads are enabled and a default thread
  357. implementation is available */
  358. #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
  359. g_thread_init (NULL);
  360. DEBUG_MSG (("Starting... (in one second)"));
  361. g_timeout_add (1000, test_check_start_and_stop, NULL);
  362. main_loop = g_main_loop_new (NULL, FALSE);
  363. g_main_loop_run (main_loop);
  364. #endif
  365. return 0;
  366. }