/gio/tests/contexts.c

https://github.com/rikaunite/gst-opera_glib · C · 194 lines · 142 code · 40 blank · 12 comment · 5 complexity · e63d63efd924f47971fb5bc3147e6858 MD5 · raw file

  1. #include <gio/gio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define TEST_FILE (SRCDIR "/Makefile.am")
  5. char *test_file_buffer;
  6. gsize test_file_size;
  7. static char async_read_buffer[8192];
  8. static void
  9. read_data (GObject *source, GAsyncResult *result, gpointer loop)
  10. {
  11. GInputStream *in = G_INPUT_STREAM (source);
  12. GError *error = NULL;
  13. gssize nread;
  14. nread = g_input_stream_read_finish (in, result, &error);
  15. g_assert_no_error (error);
  16. g_assert_cmpint (nread, >, 0);
  17. g_assert_cmpint (nread, <=, MIN(sizeof (async_read_buffer), test_file_size));
  18. g_assert (memcmp (async_read_buffer, test_file_buffer, nread) == 0);
  19. g_main_loop_quit (loop);
  20. }
  21. static void
  22. opened_for_read (GObject *source, GAsyncResult *result, gpointer loop)
  23. {
  24. GFile *file = G_FILE (source);
  25. GFileInputStream *in;
  26. GError *error = NULL;
  27. in = g_file_read_finish (file, result, &error);
  28. g_assert_no_error (error);
  29. memset (async_read_buffer, 0, sizeof (async_read_buffer));
  30. g_input_stream_read_async (G_INPUT_STREAM (in),
  31. async_read_buffer, sizeof (async_read_buffer),
  32. G_PRIORITY_DEFAULT, NULL,
  33. read_data, loop);
  34. g_object_unref (in);
  35. }
  36. /* Test 1: Async I/O started in a thread with a thread-default context
  37. * will stick to that thread, and will complete even if the default
  38. * main loop is blocked. (NB: the last part would not be true if we
  39. * were testing GFileMonitor!)
  40. */
  41. static gboolean idle_start_test1_thread (gpointer loop);
  42. static gpointer test1_thread (gpointer user_data);
  43. static GCond *test1_cond;
  44. static GMutex *test1_mutex;
  45. static void
  46. test_thread_independence (void)
  47. {
  48. GMainLoop *loop;
  49. test1_cond = g_cond_new ();
  50. test1_mutex = g_mutex_new ();
  51. loop = g_main_loop_new (NULL, FALSE);
  52. g_idle_add (idle_start_test1_thread, loop);
  53. g_main_loop_run (loop);
  54. g_main_loop_unref (loop);
  55. g_mutex_free (test1_mutex);
  56. g_cond_free (test1_cond);
  57. }
  58. static gboolean
  59. idle_start_test1_thread (gpointer loop)
  60. {
  61. GTimeVal time;
  62. GThread *thread;
  63. gboolean io_completed;
  64. g_mutex_lock (test1_mutex);
  65. thread = g_thread_create (test1_thread, NULL, TRUE, NULL);
  66. g_get_current_time (&time);
  67. time.tv_sec += 2;
  68. io_completed = g_cond_timed_wait (test1_cond, test1_mutex, &time);
  69. g_assert (io_completed);
  70. g_thread_join (thread);
  71. g_mutex_unlock (test1_mutex);
  72. g_main_loop_quit (loop);
  73. return FALSE;
  74. }
  75. static gpointer
  76. test1_thread (gpointer user_data)
  77. {
  78. GMainContext *context;
  79. GMainLoop *loop;
  80. GFile *file;
  81. /* Wait for main thread to be waiting on test1_cond */
  82. g_mutex_lock (test1_mutex);
  83. g_mutex_unlock (test1_mutex);
  84. context = g_main_context_new ();
  85. g_assert (g_main_context_get_thread_default () == NULL);
  86. g_main_context_push_thread_default (context);
  87. g_assert (g_main_context_get_thread_default () == context);
  88. file = g_file_new_for_path (TEST_FILE);
  89. g_assert (g_file_supports_thread_contexts (file));
  90. loop = g_main_loop_new (context, FALSE);
  91. g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
  92. opened_for_read, loop);
  93. g_object_unref (file);
  94. g_main_loop_run (loop);
  95. g_main_loop_unref (loop);
  96. g_cond_signal (test1_cond);
  97. return NULL;
  98. }
  99. /* Test 2: If we push a thread-default context in the main thread, we
  100. * can run async ops in that context without running the default
  101. * context.
  102. */
  103. static gboolean test2_fail (gpointer user_data);
  104. static void
  105. test_context_independence (void)
  106. {
  107. GMainContext *context;
  108. GMainLoop *loop;
  109. GFile *file;
  110. guint default_timeout;
  111. GSource *thread_default_timeout;
  112. context = g_main_context_new ();
  113. g_assert (g_main_context_get_thread_default () == NULL);
  114. g_main_context_push_thread_default (context);
  115. g_assert (g_main_context_get_thread_default () == context);
  116. file = g_file_new_for_path (TEST_FILE);
  117. g_assert (g_file_supports_thread_contexts (file));
  118. /* Add a timeout to the main loop, to fail immediately if it gets run */
  119. default_timeout = g_timeout_add_full (G_PRIORITY_HIGH, 0,
  120. test2_fail, NULL, NULL);
  121. /* Add a timeout to the alternate loop, to fail if the I/O *doesn't* run */
  122. thread_default_timeout = g_timeout_source_new_seconds (2);
  123. g_source_set_callback (thread_default_timeout, test2_fail, NULL, NULL);
  124. g_source_attach (thread_default_timeout, context);
  125. loop = g_main_loop_new (context, FALSE);
  126. g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
  127. opened_for_read, loop);
  128. g_object_unref (file);
  129. g_main_loop_run (loop);
  130. g_main_loop_unref (loop);
  131. g_source_remove (default_timeout);
  132. g_source_destroy (thread_default_timeout);
  133. g_source_unref (thread_default_timeout);
  134. }
  135. static gboolean
  136. test2_fail (gpointer user_data)
  137. {
  138. g_assert_not_reached ();
  139. return FALSE;
  140. }
  141. int
  142. main (int argc, char **argv)
  143. {
  144. GError *error = NULL;
  145. g_thread_init (NULL);
  146. g_type_init ();
  147. g_test_init (&argc, &argv, NULL);
  148. g_file_get_contents (TEST_FILE, &test_file_buffer,
  149. &test_file_size, &error);
  150. g_assert_no_error (error);
  151. g_test_add_func ("/gio/contexts/thread-independence", test_thread_independence);
  152. g_test_add_func ("/gio/contexts/context-independence", test_context_independence);
  153. return g_test_run();
  154. }