tests: Add a test program for crashing with threads
authorŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 13 Dec 2016 13:01:19 +0000 (14:01 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 13 Dec 2016 13:34:29 +0000 (14:34 +0100)
Change-Id: I0b9ef8567b0bb1203fdd9fe3e0aa753e25ff50df

tests/CMakeLists.txt
tests/test3.c [new file with mode: 0644]

index 50573557e4e41d334ac206328b20eb5ba819826d..dc3caee0123906e4ae4e123b686d4ca8b18aafde 100644 (file)
@@ -16,3 +16,7 @@ pkg_check_modules (GLIB2 glib-2.0)
 add_executable(test2 test2.c)
 target_include_directories(test2 SYSTEM PUBLIC ${GLIB2_INCLUDE_DIRS})
 target_link_libraries(test2 ${GLIB2_LDFLAGS})
+
+add_executable(test3 test3.c)
+target_include_directories(test3 SYSTEM PUBLIC ${GLIB2_INCLUDE_DIRS})
+target_link_libraries(test3 ${GLIB2_LDFLAGS} -lpthread)
diff --git a/tests/test3.c b/tests/test3.c
new file mode 100644 (file)
index 0000000..ca6ae03
--- /dev/null
@@ -0,0 +1,46 @@
+#include <glib.h>
+#include <gio/gio.h>
+#include <stdio.h>
+
+int crash_other_thread = 0;
+#define CRASHING_THREAD 19
+
+gboolean crasher(gpointer data)
+{
+       *(int*)data = 1;
+       return TRUE;
+}
+
+void *fun(void *arg)
+{
+       int threadnum = (int)arg;
+       if (crash_other_thread && threadnum == CRASHING_THREAD)
+               crasher(NULL);
+       else
+               while (1);
+       return NULL;
+}
+
+int main(int argc)
+{
+       GMainLoop *loop = g_main_loop_new(NULL, FALSE);
+
+       crash_other_thread = argc > 1;
+
+       if (!crash_other_thread)
+       {
+               GSource *idle_source = g_idle_source_new();
+               g_source_set_callback(idle_source, crasher, NULL, NULL);
+               g_source_attach(idle_source, g_main_context_ref_thread_default());
+       }
+
+       pthread_t t;
+       int i;
+
+       for (i = 0; i <= CRASHING_THREAD; i++)
+               pthread_create(&t, NULL, fun, (void *)i);
+
+       g_main_loop_run(loop);
+
+       return 0;
+}