glib/tests/mainloop: Acquire the context while asserting its state
[platform/upstream/glib.git] / glib / tests / private.c
index e3e392f..d23259c 100644 (file)
@@ -20,6 +20,9 @@
  * if advised of the possibility of such damage.
  */
 
+/* We are testing some deprecated APIs here */
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
+
 #include <glib.h>
 
 /* test basics:
 static void
 test_private1 (void)
 {
-  GPrivate *private1;
+  static GPrivate private = G_PRIVATE_INIT (NULL);
   gpointer value;
 
-  private1 = g_private_new (NULL);
-
-  value = g_private_get (private1);
+  value = g_private_get (&private);
   g_assert (value == NULL);
 
-  g_private_set (private1, GINT_TO_POINTER(1));
-  value = g_private_get (private1);
+  g_private_set (&private, GINT_TO_POINTER(1));
+  value = g_private_get (&private);
   g_assert_cmpint (GPOINTER_TO_INT (value), ==, 1);
 
-  g_private_set (private1, GINT_TO_POINTER(2));
-  value = g_private_get (private1);
+  g_private_set (&private, GINT_TO_POINTER(2));
+  value = g_private_get (&private);
   g_assert_cmpint (GPOINTER_TO_INT (value), ==, 2);
 }
 
-static GPrivate *private2;
 static gint private2_destroy_count;
 
 static void
@@ -55,6 +55,8 @@ private2_destroy (gpointer data)
   g_atomic_int_inc (&private2_destroy_count);
 }
 
+static GPrivate private2 = G_PRIVATE_INIT (private2_destroy);
+
 static gpointer
 private2_func (gpointer data)
 {
@@ -65,9 +67,9 @@ private2_func (gpointer data)
   for (i = 0; i < 1000; i++)
     {
       v = value + (i % 5);
-      g_private_set (private2, GINT_TO_POINTER(v));
+      g_private_set (&private2, GINT_TO_POINTER (v));
       g_usleep (1000);
-      v2 = GPOINTER_TO_INT(g_private_get (private2));
+      v2 = GPOINTER_TO_INT (g_private_get (&private2));
       g_assert_cmpint (v, ==, v2);
     }
 
@@ -82,6 +84,7 @@ private2_func (gpointer data)
  * - destroy notifies are called for each thread exit
  * - destroy notifies are called for g_thread_exit() too
  * - destroy notifies are not called on g_private_set()
+ * - destroy notifies are called on g_private_replace()
  */
 static void
 test_private2 (void)
@@ -89,7 +92,8 @@ test_private2 (void)
   GThread *thread[10];
   gint i;
 
-  private2 = g_private_new (private2_destroy);
+  g_private_set (&private2, GINT_TO_POINTER (234));
+  g_private_replace (&private2, GINT_TO_POINTER (123));
 
   for (i = 0; i < 10; i++)
     thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
@@ -97,7 +101,59 @@ test_private2 (void)
   for (i = 0; i < 10; i++)
     g_thread_join (thread[i]);
 
-  g_assert_cmpint (private2_destroy_count, ==, 10);
+  g_assert_cmpint (private2_destroy_count, ==, 11);
+}
+
+static gboolean private3_freed;
+
+static void
+private3_free (gpointer data)
+{
+  g_assert (data == (void*) 0x1234);
+  private3_freed = TRUE;
+}
+
+#ifdef G_OS_WIN32
+#include <windows.h>
+#include <process.h>
+
+static guint __stdcall
+#else
+#include <pthread.h>
+
+static gpointer
+#endif
+private3_func (gpointer data)
+{
+  static GPrivate key = G_PRIVATE_INIT (private3_free);
+
+  g_private_set (&key, (void *) 0x1234);
+
+  return 0;
+}
+
+static void
+test_private3 (void)
+{
+  g_assert (!private3_freed);
+
+#ifdef G_OS_WIN32
+  {
+    HANDLE thread;
+    guint ignore;
+    thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
+    WaitForSingleObject (thread, INFINITE);
+    CloseHandle (thread);
+  }
+#else
+  {
+    pthread_t thread;
+
+    pthread_create (&thread, NULL, private3_func, NULL);
+    pthread_join (thread, NULL);
+  }
+#endif
+  g_assert (private3_freed);
 }
 
 /* test basics:
@@ -312,10 +368,6 @@ test_static_private5 (void)
 
   for (i = 0; i < 10; i++)
     g_thread_join (thread[i]);
-
-  g_mutex_clear (&m5);
-  g_cond_clear (&c5a);
-  g_cond_clear (&c5b);
 }
 
 int
@@ -323,10 +375,9 @@ main (int argc, char *argv[])
 {
   g_test_init (&argc, &argv, NULL);
 
-  g_assert (g_thread_get_initialized ());
-
   g_test_add_func ("/thread/private1", test_private1);
   g_test_add_func ("/thread/private2", test_private2);
+  g_test_add_func ("/thread/private3", test_private3);
   g_test_add_func ("/thread/staticprivate1", test_static_private1);
   g_test_add_func ("/thread/staticprivate2", test_static_private2);
   g_test_add_func ("/thread/staticprivate3", test_static_private3);