Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / nsprpub / pr / tests / io_timeoutk.c
blob26e3beeb6fc4526f702fdf85f0c3c8d42195315d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 ** name io_timeoutk.c
40 ** Description:Test socket IO timeouts (kernel level)
42 ** Modification History:
43 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
44 ** The debug mode will print all of the printfs associated with this test.
45 ** The regress mode will be the default mode. Since the regress tool limits
46 ** the output to a one line status:PASS or FAIL,all of the printf statements
47 ** have been handled with an if (debug_mode) statement.
48 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
49 ** recognize the return code from tha main program.
50 ***********************************************************************/
51 /***********************************************************************
52 ** Includes
53 ***********************************************************************/
54 /* Used to get the command line option */
55 #include "plgetopt.h"
57 #include <stdio.h>
58 #include "nspr.h"
60 #define NUM_THREADS 1
61 #define BASE_PORT 8000
62 #define DEFAULT_ACCEPT_TIMEOUT 2
64 typedef struct threadInfo {
65 PRInt16 id;
66 PRInt16 accept_timeout;
67 PRLock *dead_lock;
68 PRCondVar *dead_cv;
69 PRInt32 *alive;
70 } threadInfo;
72 PRIntn failed_already=0;
73 PRIntn debug_mode;
76 void
77 thread_main(void *_info)
79 threadInfo *info = (threadInfo *)_info;
80 PRNetAddr listenAddr;
81 PRNetAddr clientAddr;
82 PRFileDesc *listenSock = NULL;
83 PRFileDesc *clientSock;
84 PRStatus rv;
86 if (debug_mode) printf("thread %d is alive\n", info->id);
88 listenSock = PR_NewTCPSocket();
89 if (!listenSock) {
90 if (debug_mode) printf("unable to create listen socket\n");
91 goto dead;
94 listenAddr.inet.family = AF_INET;
95 listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
96 listenAddr.inet.ip = PR_htonl(INADDR_ANY);
97 rv = PR_Bind(listenSock, &listenAddr);
98 if (rv == PR_FAILURE) {
99 if (debug_mode) printf("unable to bind\n");
100 goto dead;
103 rv = PR_Listen(listenSock, 4);
104 if (rv == PR_FAILURE) {
105 if (debug_mode) printf("unable to listen\n");
106 goto dead;
109 if (debug_mode) printf("thread %d going into accept for %d seconds\n",
110 info->id, info->accept_timeout + info->id);
112 clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
114 if (clientSock == NULL) {
115 if (PR_GetError() == PR_IO_TIMEOUT_ERROR)
116 if (debug_mode) {
117 printf("PR_Accept() timeout worked!\n");
118 printf("TEST FAILED! PR_Accept() returned error %d\n",
119 PR_GetError());
121 else failed_already=1;
122 } else {
123 if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
124 else failed_already=1;
125 PR_Close(clientSock);
128 dead:
129 if (listenSock) {
130 PR_Close(listenSock);
132 PR_Lock(info->dead_lock);
133 (*info->alive)--;
134 PR_NotifyCondVar(info->dead_cv);
135 PR_Unlock(info->dead_lock);
137 if (debug_mode) printf("thread %d is dead\n", info->id);
140 void
141 thread_test(PRInt32 scope, PRInt32 num_threads)
143 PRInt32 index;
144 PRThread *thr;
145 PRLock *dead_lock;
146 PRCondVar *dead_cv;
147 PRInt32 alive;
149 if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
151 dead_lock = PR_NewLock();
152 dead_cv = PR_NewCondVar(dead_lock);
153 alive = num_threads;
155 for (index = 0; index < num_threads; index++) {
156 threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
158 info->id = index;
159 info->dead_lock = dead_lock;
160 info->dead_cv = dead_cv;
161 info->alive = &alive;
162 info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
164 thr = PR_CreateThread( PR_USER_THREAD,
165 thread_main,
166 (void *)info,
167 PR_PRIORITY_NORMAL,
168 scope,
169 PR_UNJOINABLE_THREAD,
172 if (!thr) {
173 PR_Lock(dead_lock);
174 alive--;
175 PR_Unlock(dead_lock);
179 PR_Lock(dead_lock);
180 while(alive) {
181 if (debug_mode) printf("main loop awake; alive = %d\n", alive);
182 PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
184 PR_Unlock(dead_lock);
187 int main(int argc, char **argv)
189 PRInt32 num_threads;
191 /* The command line argument: -d is used to determine if the test is being run
192 in debug mode. The regress tool requires only one line output:PASS or FAIL.
193 All of the printfs associated with this test has been handled with a if (debug_mode)
194 test.
195 Usage: test_name -d
197 PLOptStatus os;
198 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
199 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
201 if (PL_OPT_BAD == os) continue;
202 switch (opt->option)
204 case 'd': /* debug mode */
205 debug_mode = 1;
206 break;
207 default:
208 break;
211 PL_DestroyOptState(opt);
213 /* main test */
215 if (argc > 2)
216 num_threads = atoi(argv[2]);
217 else
218 num_threads = NUM_THREADS;
220 PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
221 PR_STDIO_INIT();
223 if (debug_mode) printf("kernel level test\n");
224 thread_test(PR_GLOBAL_THREAD, num_threads);
226 PR_Cleanup();
228 if(failed_already)
229 return 1;
230 else
231 return 0;