Update contrib.
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 // This test case is a threaded test case.
32 #include <sys/types.h>
41 #include "mrt2_glib2_test.h"
51 #define GPID_FORMAT "%p"
53 #define GPID_FORMAT "%d"
59 #if defined(G_OS_WIN32) || defined(SYMBIAN)
64 get_a_child (gint ttl)
70 PROCESS_INFORMATION pi;
73 memset (&si, 0, sizeof (si));
75 memset (&pi, 0, sizeof (pi));
77 cmdline = g_strdup_printf( "child-test -c%d", ttl);
79 if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
80 g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
84 CloseHandle (pi.hThread);
88 #elif defined(SYMBIAN)
89 gchar *working_directory = NULL;
91 gpointer user_data = NULL;
95 GSpawnChildSetupFunc child_setup = NULL;
99 int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD;
102 argv = (char **)malloc(3*sizeof(char *));
103 argv[1] = (char *)malloc(10*sizeof(char));
105 sprintf(argv[1],"-c%d",ttl);
107 g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error));
120 #endif /* G_OS_WIN32 */
124 child_watch_callback (GPid pid, gint status, gpointer data)
127 gint ttl = GPOINTER_TO_INT (data);
129 g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
132 g_spawn_close_pid (pid);
135 g_main_loop_quit (main_loop);
141 quit_loop (gpointer data)
143 GMainLoop *main_loop = data;
145 g_main_loop_quit (main_loop);
152 test_thread (gpointer data)
154 GMainLoop *new_main_loop;
157 gint ttl = GPOINTER_TO_INT (data);
159 new_main_loop = g_main_loop_new (NULL, FALSE);
161 pid = get_a_child (ttl);
163 source = g_child_watch_source_new (pid);
164 g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
165 g_source_attach (source, g_main_loop_get_context (new_main_loop));
166 g_source_unref (source);
169 g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
172 g_main_loop_run (new_main_loop);
179 main (int argc, char *argv[])
183 #if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
185 testResultXml("child-test2");
189 #if defined(G_OS_WIN32) || defined(SYMBIAN)
192 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
194 int ttl = atoi (argv[1] + 2);
202 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
203 g_set_print_handler(mrtPrintHandler);
206 /* Only run the test, if threads are enabled and a default thread
207 * implementation is available.
209 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
211 g_thread_init (NULL);
214 main_loop = g_main_loop_new (NULL, FALSE);
215 g_assert(main_loop != NULL);
218 system ("ipconfig /all");
220 system ("/bin/true");
224 g_timeout_add (30000, quit_loop, main_loop);
227 g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
228 g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
230 pid = get_a_child (10);
232 g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
233 GINT_TO_POINTER (10));
234 pid = get_a_child (20);
236 g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
237 GINT_TO_POINTER (20));
240 g_main_loop_run (main_loop);
244 g_warning ("%d children still alive\n", alive);
245 g_assert(FALSE && "some children still alive");
252 testResultXml("child-test2");