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
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1999-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * File: multiacc.c
|
---|
40 | *
|
---|
41 | * Description:
|
---|
42 | * This test creates multiple threads that accept on the
|
---|
43 | * same listening socket.
|
---|
44 | */
|
---|
45 |
|
---|
46 | #include "nspr.h"
|
---|
47 |
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 | #include <string.h>
|
---|
51 |
|
---|
52 | #define NUM_SERVER_THREADS 10
|
---|
53 |
|
---|
54 | static int num_server_threads = NUM_SERVER_THREADS;
|
---|
55 | static PRThreadScope thread_scope = PR_GLOBAL_THREAD;
|
---|
56 | static PRBool exit_flag = PR_FALSE;
|
---|
57 |
|
---|
58 | static void ServerThreadFunc(void *arg)
|
---|
59 | {
|
---|
60 | PRFileDesc *listenSock = (PRFileDesc *) arg;
|
---|
61 | PRFileDesc *acceptSock;
|
---|
62 | PRErrorCode err;
|
---|
63 | PRStatus status;
|
---|
64 |
|
---|
65 | while (!exit_flag) {
|
---|
66 | acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
|
---|
67 | if (NULL == acceptSock) {
|
---|
68 | err = PR_GetError();
|
---|
69 | if (PR_PENDING_INTERRUPT_ERROR == err) {
|
---|
70 | printf("server thread is interrupted\n");
|
---|
71 | fflush(stdout);
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 | fprintf(stderr, "PR_Accept failed: %d\n", err);
|
---|
75 | exit(1);
|
---|
76 | }
|
---|
77 | status = PR_Close(acceptSock);
|
---|
78 | if (PR_FAILURE == status) {
|
---|
79 | fprintf(stderr, "PR_Close failed\n");
|
---|
80 | exit(1);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | int main(int argc, char **argv)
|
---|
86 | {
|
---|
87 | PRNetAddr serverAddr;
|
---|
88 | PRFileDesc *dummySock;
|
---|
89 | PRFileDesc *listenSock;
|
---|
90 | PRFileDesc *clientSock;
|
---|
91 | PRThread *dummyThread;
|
---|
92 | PRThread **serverThreads;
|
---|
93 | PRStatus status;
|
---|
94 | PRUint16 port;
|
---|
95 | int idx;
|
---|
96 | PRInt32 nbytes;
|
---|
97 | char buf[1024];
|
---|
98 |
|
---|
99 | serverThreads = (PRThread **)
|
---|
100 | PR_Malloc(num_server_threads * sizeof(PRThread *));
|
---|
101 | if (NULL == serverThreads) {
|
---|
102 | fprintf(stderr, "PR_Malloc failed\n");
|
---|
103 | exit(1);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Create a dummy listening socket and have the first
|
---|
108 | * (dummy) thread listen on it. This is to ensure that
|
---|
109 | * the first thread becomes the I/O continuation thread
|
---|
110 | * in the pthreads implementation (see ptio.c) and remains
|
---|
111 | * so throughout the test, so that we never have to
|
---|
112 | * recycle the I/O continuation thread.
|
---|
113 | */
|
---|
114 | dummySock = PR_NewTCPSocket();
|
---|
115 | if (NULL == dummySock) {
|
---|
116 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
117 | exit(1);
|
---|
118 | }
|
---|
119 | memset(&serverAddr, 0, sizeof(serverAddr));
|
---|
120 | status = PR_InitializeNetAddr(PR_IpAddrAny, 0, &serverAddr);
|
---|
121 | if (PR_FAILURE == status) {
|
---|
122 | fprintf(stderr, "PR_InitializeNetAddr failed\n");
|
---|
123 | exit(1);
|
---|
124 | }
|
---|
125 | status = PR_Bind(dummySock, &serverAddr);
|
---|
126 | if (PR_FAILURE == status) {
|
---|
127 | fprintf(stderr, "PR_Bind failed\n");
|
---|
128 | exit(1);
|
---|
129 | }
|
---|
130 | status = PR_Listen(dummySock, 5);
|
---|
131 | if (PR_FAILURE == status) {
|
---|
132 | fprintf(stderr, "PR_Listen failed\n");
|
---|
133 | exit(1);
|
---|
134 | }
|
---|
135 |
|
---|
136 | listenSock = PR_NewTCPSocket();
|
---|
137 | if (NULL == listenSock) {
|
---|
138 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
139 | exit(1);
|
---|
140 | }
|
---|
141 | memset(&serverAddr, 0, sizeof(serverAddr));
|
---|
142 | status = PR_InitializeNetAddr(PR_IpAddrAny, 0, &serverAddr);
|
---|
143 | if (PR_FAILURE == status) {
|
---|
144 | fprintf(stderr, "PR_InitializeNetAddr failed\n");
|
---|
145 | exit(1);
|
---|
146 | }
|
---|
147 | status = PR_Bind(listenSock, &serverAddr);
|
---|
148 | if (PR_FAILURE == status) {
|
---|
149 | fprintf(stderr, "PR_Bind failed\n");
|
---|
150 | exit(1);
|
---|
151 | }
|
---|
152 | status = PR_GetSockName(listenSock, &serverAddr);
|
---|
153 | if (PR_FAILURE == status) {
|
---|
154 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
155 | exit(1);
|
---|
156 | }
|
---|
157 | port = PR_ntohs(serverAddr.inet.port);
|
---|
158 | status = PR_Listen(listenSock, 5);
|
---|
159 | if (PR_FAILURE == status) {
|
---|
160 | fprintf(stderr, "PR_Listen failed\n");
|
---|
161 | exit(1);
|
---|
162 | }
|
---|
163 |
|
---|
164 | printf("creating dummy thread\n");
|
---|
165 | fflush(stdout);
|
---|
166 | dummyThread = PR_CreateThread(PR_USER_THREAD,
|
---|
167 | ServerThreadFunc, dummySock, PR_PRIORITY_NORMAL,
|
---|
168 | thread_scope, PR_JOINABLE_THREAD, 0);
|
---|
169 | if (NULL == dummyThread) {
|
---|
170 | fprintf(stderr, "PR_CreateThread failed\n");
|
---|
171 | exit(1);
|
---|
172 | }
|
---|
173 | printf("sleeping one second before creating server threads\n");
|
---|
174 | fflush(stdout);
|
---|
175 | PR_Sleep(PR_SecondsToInterval(1));
|
---|
176 | for (idx = 0; idx < num_server_threads; idx++) {
|
---|
177 | serverThreads[idx] = PR_CreateThread(PR_USER_THREAD,
|
---|
178 | ServerThreadFunc, listenSock, PR_PRIORITY_NORMAL,
|
---|
179 | thread_scope, PR_JOINABLE_THREAD, 0);
|
---|
180 | if (NULL == serverThreads[idx]) {
|
---|
181 | fprintf(stderr, "PR_CreateThread failed\n");
|
---|
182 | exit(1);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | memset(&serverAddr, 0, sizeof(serverAddr));
|
---|
187 | PR_InitializeNetAddr(PR_IpAddrLoopback, port, &serverAddr);
|
---|
188 | clientSock = PR_NewTCPSocket();
|
---|
189 | if (NULL == clientSock) {
|
---|
190 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
191 | exit(1);
|
---|
192 | }
|
---|
193 | printf("sleeping one second before connecting\n");
|
---|
194 | fflush(stdout);
|
---|
195 | PR_Sleep(PR_SecondsToInterval(1));
|
---|
196 | status = PR_Connect(clientSock, &serverAddr, PR_INTERVAL_NO_TIMEOUT);
|
---|
197 | if (PR_FAILURE == status) {
|
---|
198 | fprintf(stderr, "PR_Connect failed\n");
|
---|
199 | exit(1);
|
---|
200 | }
|
---|
201 | nbytes = PR_Read(clientSock, buf, sizeof(buf));
|
---|
202 | if (nbytes != 0) {
|
---|
203 | fprintf(stderr, "expected 0 bytes but got %d bytes\n", nbytes);
|
---|
204 | exit(1);
|
---|
205 | }
|
---|
206 | status = PR_Close(clientSock);
|
---|
207 | if (PR_FAILURE == status) {
|
---|
208 | fprintf(stderr, "PR_Close failed\n");
|
---|
209 | exit(1);
|
---|
210 | }
|
---|
211 | printf("sleeping one second before shutting down server threads\n");
|
---|
212 | fflush(stdout);
|
---|
213 | PR_Sleep(PR_SecondsToInterval(1));
|
---|
214 |
|
---|
215 | exit_flag = PR_TRUE;
|
---|
216 | status = PR_Interrupt(dummyThread);
|
---|
217 | if (PR_FAILURE == status) {
|
---|
218 | fprintf(stderr, "PR_Interrupt failed\n");
|
---|
219 | exit(1);
|
---|
220 | }
|
---|
221 | status = PR_JoinThread(dummyThread);
|
---|
222 | if (PR_FAILURE == status) {
|
---|
223 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
224 | exit(1);
|
---|
225 | }
|
---|
226 | for (idx = 0; idx < num_server_threads; idx++) {
|
---|
227 | status = PR_Interrupt(serverThreads[idx]);
|
---|
228 | if (PR_FAILURE == status) {
|
---|
229 | fprintf(stderr, "PR_Interrupt failed\n");
|
---|
230 | exit(1);
|
---|
231 | }
|
---|
232 | status = PR_JoinThread(serverThreads[idx]);
|
---|
233 | if (PR_FAILURE == status) {
|
---|
234 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
235 | exit(1);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | PR_Free(serverThreads);
|
---|
239 | status = PR_Close(dummySock);
|
---|
240 | if (PR_FAILURE == status) {
|
---|
241 | fprintf(stderr, "PR_Close failed\n");
|
---|
242 | exit(1);
|
---|
243 | }
|
---|
244 | status = PR_Close(listenSock);
|
---|
245 | if (PR_FAILURE == status) {
|
---|
246 | fprintf(stderr, "PR_Close failed\n");
|
---|
247 | exit(1);
|
---|
248 | }
|
---|
249 |
|
---|
250 | printf("PASS\n");
|
---|
251 | return 0;
|
---|
252 | }
|
---|