sl@0
|
1 |
/* GLIB - Library of useful routines for C programming
|
sl@0
|
2 |
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
sl@0
|
3 |
* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
sl@0
|
4 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
5 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
6 |
* License as published by the Free Software Foundation; either
|
sl@0
|
7 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
12 |
* Lesser General Public License for more details.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* You should have received a copy of the GNU Lesser General Public
|
sl@0
|
15 |
* License along with this library; if not, write to the
|
sl@0
|
16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
sl@0
|
17 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
|
sl@0
|
20 |
/*
|
sl@0
|
21 |
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
sl@0
|
22 |
* file for a list of people on the GLib Team. See the ChangeLog
|
sl@0
|
23 |
* files for a list of changes. These files are distributed with
|
sl@0
|
24 |
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
sl@0
|
25 |
*/
|
sl@0
|
26 |
|
sl@0
|
27 |
// This test case is a threaded test case.
|
sl@0
|
28 |
|
sl@0
|
29 |
#include "config.h"
|
sl@0
|
30 |
#include <stdio.h>
|
sl@0
|
31 |
|
sl@0
|
32 |
#include <sys/types.h>
|
sl@0
|
33 |
#ifdef HAVE_UNISTD_H
|
sl@0
|
34 |
#include <unistd.h>
|
sl@0
|
35 |
#endif
|
sl@0
|
36 |
#include <stdlib.h>
|
sl@0
|
37 |
|
sl@0
|
38 |
#include <glib.h>
|
sl@0
|
39 |
|
sl@0
|
40 |
#ifdef SYMBIAN
|
sl@0
|
41 |
#include "mrt2_glib2_test.h"
|
sl@0
|
42 |
#endif /*SYMBIAN*/
|
sl@0
|
43 |
|
sl@0
|
44 |
#define TEST_THREAD 1
|
sl@0
|
45 |
|
sl@0
|
46 |
#ifdef G_OS_WIN32
|
sl@0
|
47 |
#include <windows.h>
|
sl@0
|
48 |
#endif
|
sl@0
|
49 |
|
sl@0
|
50 |
#ifdef G_OS_WIN32
|
sl@0
|
51 |
#define GPID_FORMAT "%p"
|
sl@0
|
52 |
#else
|
sl@0
|
53 |
#define GPID_FORMAT "%d"
|
sl@0
|
54 |
#endif
|
sl@0
|
55 |
|
sl@0
|
56 |
GMainLoop *main_loop;
|
sl@0
|
57 |
gint alive;
|
sl@0
|
58 |
|
sl@0
|
59 |
#if defined(G_OS_WIN32) || defined(SYMBIAN)
|
sl@0
|
60 |
char *argv0;
|
sl@0
|
61 |
#endif
|
sl@0
|
62 |
|
sl@0
|
63 |
GPid
|
sl@0
|
64 |
get_a_child (gint ttl)
|
sl@0
|
65 |
{
|
sl@0
|
66 |
GPid pid;
|
sl@0
|
67 |
|
sl@0
|
68 |
#ifdef G_OS_WIN32
|
sl@0
|
69 |
STARTUPINFO si;
|
sl@0
|
70 |
PROCESS_INFORMATION pi;
|
sl@0
|
71 |
gchar *cmdline;
|
sl@0
|
72 |
|
sl@0
|
73 |
memset (&si, 0, sizeof (si));
|
sl@0
|
74 |
si.cb = sizeof (&si);
|
sl@0
|
75 |
memset (&pi, 0, sizeof (pi));
|
sl@0
|
76 |
|
sl@0
|
77 |
cmdline = g_strdup_printf( "child-test -c%d", ttl);
|
sl@0
|
78 |
|
sl@0
|
79 |
if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
|
sl@0
|
80 |
g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
|
sl@0
|
81 |
|
sl@0
|
82 |
g_free(cmdline);
|
sl@0
|
83 |
|
sl@0
|
84 |
CloseHandle (pi.hThread);
|
sl@0
|
85 |
pid = pi.hProcess;
|
sl@0
|
86 |
|
sl@0
|
87 |
return pid;
|
sl@0
|
88 |
#elif defined(SYMBIAN)
|
sl@0
|
89 |
gchar *working_directory = NULL;
|
sl@0
|
90 |
gchar **envp = NULL;
|
sl@0
|
91 |
gpointer user_data = NULL;
|
sl@0
|
92 |
GError *error = NULL;
|
sl@0
|
93 |
GPid child_pid;
|
sl@0
|
94 |
|
sl@0
|
95 |
GSpawnChildSetupFunc child_setup = NULL;
|
sl@0
|
96 |
|
sl@0
|
97 |
int retVal = 0;
|
sl@0
|
98 |
|
sl@0
|
99 |
int flags = G_SPAWN_FILE_AND_ARGV_ZERO|G_SPAWN_DO_NOT_REAP_CHILD;
|
sl@0
|
100 |
|
sl@0
|
101 |
char **argv = NULL;
|
sl@0
|
102 |
argv = (char **)malloc(3*sizeof(char *));
|
sl@0
|
103 |
argv[1] = (char *)malloc(10*sizeof(char));
|
sl@0
|
104 |
argv[0] = argv0;
|
sl@0
|
105 |
sprintf(argv[1],"-c%d",ttl);
|
sl@0
|
106 |
argv[2] = NULL;
|
sl@0
|
107 |
g_assert(g_spawn_async(working_directory,argv,envp,flags,child_setup,user_data,&child_pid,&error));
|
sl@0
|
108 |
return child_pid;
|
sl@0
|
109 |
|
sl@0
|
110 |
#else
|
sl@0
|
111 |
pid = fork ();
|
sl@0
|
112 |
if (pid < 0)
|
sl@0
|
113 |
exit (1);
|
sl@0
|
114 |
|
sl@0
|
115 |
if (pid > 0)
|
sl@0
|
116 |
return pid;
|
sl@0
|
117 |
|
sl@0
|
118 |
sleep (ttl);
|
sl@0
|
119 |
_exit (0);
|
sl@0
|
120 |
#endif /* G_OS_WIN32 */
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
gboolean
|
sl@0
|
124 |
child_watch_callback (GPid pid, gint status, gpointer data)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
#ifdef VERBOSE
|
sl@0
|
127 |
gint ttl = GPOINTER_TO_INT (data);
|
sl@0
|
128 |
|
sl@0
|
129 |
g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
|
sl@0
|
130 |
#endif
|
sl@0
|
131 |
|
sl@0
|
132 |
g_spawn_close_pid (pid);
|
sl@0
|
133 |
|
sl@0
|
134 |
if (--alive == 0)
|
sl@0
|
135 |
g_main_loop_quit (main_loop);
|
sl@0
|
136 |
|
sl@0
|
137 |
return TRUE;
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
static gboolean
|
sl@0
|
141 |
quit_loop (gpointer data)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
GMainLoop *main_loop = data;
|
sl@0
|
144 |
|
sl@0
|
145 |
g_main_loop_quit (main_loop);
|
sl@0
|
146 |
|
sl@0
|
147 |
return TRUE;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
#ifdef TEST_THREAD
|
sl@0
|
151 |
static gpointer
|
sl@0
|
152 |
test_thread (gpointer data)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
GMainLoop *new_main_loop;
|
sl@0
|
155 |
GSource *source;
|
sl@0
|
156 |
GPid pid = 0;
|
sl@0
|
157 |
gint ttl = GPOINTER_TO_INT (data);
|
sl@0
|
158 |
|
sl@0
|
159 |
new_main_loop = g_main_loop_new (NULL, FALSE);
|
sl@0
|
160 |
|
sl@0
|
161 |
pid = get_a_child (ttl);
|
sl@0
|
162 |
g_assert(pid != 0);
|
sl@0
|
163 |
source = g_child_watch_source_new (pid);
|
sl@0
|
164 |
g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
|
sl@0
|
165 |
g_source_attach (source, g_main_loop_get_context (new_main_loop));
|
sl@0
|
166 |
g_source_unref (source);
|
sl@0
|
167 |
|
sl@0
|
168 |
#ifdef VERBOSE
|
sl@0
|
169 |
g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
|
sl@0
|
170 |
#endif
|
sl@0
|
171 |
|
sl@0
|
172 |
g_main_loop_run (new_main_loop);
|
sl@0
|
173 |
|
sl@0
|
174 |
return NULL;
|
sl@0
|
175 |
}
|
sl@0
|
176 |
#endif
|
sl@0
|
177 |
|
sl@0
|
178 |
int
|
sl@0
|
179 |
main (int argc, char *argv[])
|
sl@0
|
180 |
{
|
sl@0
|
181 |
GPid pid;
|
sl@0
|
182 |
|
sl@0
|
183 |
#if defined(SYMBIAN) && (defined(__WINS__) || defined(__WINSCW__))
|
sl@0
|
184 |
|
sl@0
|
185 |
testResultXml("child-test2");
|
sl@0
|
186 |
return 0;
|
sl@0
|
187 |
|
sl@0
|
188 |
#endif // EMULATOR
|
sl@0
|
189 |
#if defined(G_OS_WIN32) || defined(SYMBIAN)
|
sl@0
|
190 |
argv0 = argv[0];
|
sl@0
|
191 |
|
sl@0
|
192 |
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
|
sl@0
|
193 |
{
|
sl@0
|
194 |
int ttl = atoi (argv[1] + 2);
|
sl@0
|
195 |
usleep (ttl);
|
sl@0
|
196 |
exit (0);
|
sl@0
|
197 |
}
|
sl@0
|
198 |
#endif
|
sl@0
|
199 |
|
sl@0
|
200 |
#ifdef SYMBIAN
|
sl@0
|
201 |
|
sl@0
|
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);
|
sl@0
|
203 |
g_set_print_handler(mrtPrintHandler);
|
sl@0
|
204 |
#endif /*SYMBIAN*/
|
sl@0
|
205 |
|
sl@0
|
206 |
/* Only run the test, if threads are enabled and a default thread
|
sl@0
|
207 |
* implementation is available.
|
sl@0
|
208 |
*/
|
sl@0
|
209 |
#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
|
sl@0
|
210 |
#ifdef TEST_THREAD
|
sl@0
|
211 |
g_thread_init (NULL);
|
sl@0
|
212 |
#endif
|
sl@0
|
213 |
|
sl@0
|
214 |
main_loop = g_main_loop_new (NULL, FALSE);
|
sl@0
|
215 |
g_assert(main_loop != NULL);
|
sl@0
|
216 |
|
sl@0
|
217 |
#ifdef G_OS_WIN32
|
sl@0
|
218 |
system ("ipconfig /all");
|
sl@0
|
219 |
#else
|
sl@0
|
220 |
system ("/bin/true");
|
sl@0
|
221 |
#endif
|
sl@0
|
222 |
|
sl@0
|
223 |
alive = 2;
|
sl@0
|
224 |
g_timeout_add (30000, quit_loop, main_loop);
|
sl@0
|
225 |
|
sl@0
|
226 |
#ifdef TEST_THREAD
|
sl@0
|
227 |
g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
|
sl@0
|
228 |
g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
|
sl@0
|
229 |
#else
|
sl@0
|
230 |
pid = get_a_child (10);
|
sl@0
|
231 |
g_assert(pid != 0);
|
sl@0
|
232 |
g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
|
sl@0
|
233 |
GINT_TO_POINTER (10));
|
sl@0
|
234 |
pid = get_a_child (20);
|
sl@0
|
235 |
g_assert(pid != 0);
|
sl@0
|
236 |
g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
|
sl@0
|
237 |
GINT_TO_POINTER (20));
|
sl@0
|
238 |
#endif
|
sl@0
|
239 |
|
sl@0
|
240 |
g_main_loop_run (main_loop);
|
sl@0
|
241 |
|
sl@0
|
242 |
if (alive > 0)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
g_warning ("%d children still alive\n", alive);
|
sl@0
|
245 |
g_assert(FALSE && "some children still alive");
|
sl@0
|
246 |
return 1;
|
sl@0
|
247 |
}
|
sl@0
|
248 |
|
sl@0
|
249 |
#endif
|
sl@0
|
250 |
|
sl@0
|
251 |
#if SYMBIAN
|
sl@0
|
252 |
testResultXml("child-test2");
|
sl@0
|
253 |
#endif
|
sl@0
|
254 |
|
sl@0
|
255 |
return 0;
|
sl@0
|
256 |
}
|