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
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) 2000
20 * the Initial Developer. All Rights Reserved.
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 * This test exercises the code that allocates and frees the syspoll_list
40 * array of PRThread in the pthreads version. This test is intended to be
41 * run under Purify to verify that there is no memory leak.
50 #define POLL_DESC_COUNT 256 /* This should be greater than the
51 * STACK_POLL_DESC_COUNT macro in
52 * ptio.c to cause syspoll_list to
55 static PRPollDesc pd
[POLL_DESC_COUNT
];
57 static void Test(void)
61 PRIntervalTime timeout
;
63 timeout
= PR_MillisecondsToInterval(10);
64 /* cause syspoll_list to grow */
65 for (i
= 1; i
<= POLL_DESC_COUNT
; i
++) {
66 rv
= PR_Poll(pd
, i
, timeout
);
69 "PR_Poll should time out but returns %d (%d, %d)\n",
70 (int) rv
, (int) PR_GetError(), (int) PR_GetOSError());
74 /* syspoll_list should be large enough for all these */
75 for (i
= POLL_DESC_COUNT
; i
>= 1; i
--) {
76 rv
= PR_Poll(pd
, i
, timeout
);
78 fprintf(stderr
, "PR_Poll should time out but returns %d\n",
85 static void ThreadFunc(void *arg
)
90 int main(int argc
, char **argv
)
97 memset(&addr
, 0, sizeof(addr
));
98 addr
.inet
.family
= PR_AF_INET
;
99 addr
.inet
.port
= PR_htons(0);
100 addr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
101 for (i
= 0; i
< POLL_DESC_COUNT
; i
++) {
102 sock
= PR_NewTCPSocket();
104 fprintf(stderr
, "PR_NewTCPSocket failed (%d, %d)\n",
105 (int) PR_GetError(), (int) PR_GetOSError());
106 fprintf(stderr
, "Ensure the per process file descriptor limit "
107 "is greater than %d.", POLL_DESC_COUNT
);
110 if (PR_Bind(sock
, &addr
) == PR_FAILURE
) {
111 fprintf(stderr
, "PR_Bind failed (%d, %d)\n",
112 (int) PR_GetError(), (int) PR_GetOSError());
115 if (PR_Listen(sock
, 5) == PR_FAILURE
) {
116 fprintf(stderr
, "PR_Listen failed (%d, %d)\n",
117 (int) PR_GetError(), (int) PR_GetOSError());
122 pd
[i
].in_flags
= PR_POLL_READ
;
125 /* first run the test on the primordial thread */
128 /* then run the test on all three kinds of threads */
129 thread
= PR_CreateThread(PR_USER_THREAD
, ThreadFunc
, NULL
,
130 PR_PRIORITY_NORMAL
, PR_LOCAL_THREAD
, PR_JOINABLE_THREAD
, 0);
131 if (NULL
== thread
) {
132 fprintf(stderr
, "PR_CreateThread failed\n");
135 if (PR_JoinThread(thread
) == PR_FAILURE
) {
136 fprintf(stderr
, "PR_JoinThread failed\n");
139 thread
= PR_CreateThread(PR_USER_THREAD
, ThreadFunc
, NULL
,
140 PR_PRIORITY_NORMAL
, PR_GLOBAL_THREAD
, PR_JOINABLE_THREAD
, 0);
141 if (NULL
== thread
) {
142 fprintf(stderr
, "PR_CreateThread failed\n");
145 if (PR_JoinThread(thread
) == PR_FAILURE
) {
146 fprintf(stderr
, "PR_JoinThread failed\n");
149 thread
= PR_CreateThread(PR_USER_THREAD
, ThreadFunc
, NULL
,
150 PR_PRIORITY_NORMAL
, PR_GLOBAL_BOUND_THREAD
, PR_JOINABLE_THREAD
, 0);
151 if (NULL
== thread
) {
152 fprintf(stderr
, "PR_CreateThread failed\n");
155 if (PR_JoinThread(thread
) == PR_FAILURE
) {
156 fprintf(stderr
, "PR_JoinThread failed\n");
159 for (i
= 0; i
< POLL_DESC_COUNT
; i
++) {
160 if (PR_Close(pd
[i
].fd
) == PR_FAILURE
) {
161 fprintf(stderr
, "PR_Close failed\n");