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) 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 | * 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.
|
---|
42 | */
|
---|
43 |
|
---|
44 | #include "nspr.h"
|
---|
45 |
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <string.h>
|
---|
49 |
|
---|
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
|
---|
53 | * be created. */
|
---|
54 |
|
---|
55 | static PRPollDesc pd[POLL_DESC_COUNT];
|
---|
56 |
|
---|
57 | static void Test(void)
|
---|
58 | {
|
---|
59 | int i;
|
---|
60 | PRInt32 rv;
|
---|
61 | PRIntervalTime timeout;
|
---|
62 |
|
---|
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);
|
---|
67 | if (rv != 0) {
|
---|
68 | fprintf(stderr,
|
---|
69 | "PR_Poll should time out but returns %d (%d, %d)\n",
|
---|
70 | rv, PR_GetError(), PR_GetOSError());
|
---|
71 | exit(1);
|
---|
72 | }
|
---|
73 | }
|
---|
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);
|
---|
77 | if (rv != 0) {
|
---|
78 | fprintf(stderr, "PR_Poll should time out but returns %d\n", rv);
|
---|
79 | exit(1);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void ThreadFunc(void *arg)
|
---|
85 | {
|
---|
86 | Test();
|
---|
87 | }
|
---|
88 |
|
---|
89 | int main(int argc, char **argv)
|
---|
90 | {
|
---|
91 | int i;
|
---|
92 | PRThread *thread;
|
---|
93 | PRFileDesc *sock;
|
---|
94 | PRNetAddr addr;
|
---|
95 |
|
---|
96 | memset(&addr, 0, sizeof(addr));
|
---|
97 | addr.inet.family = PR_AF_INET;
|
---|
98 | addr.inet.port = PR_htons(0);
|
---|
99 | addr.inet.ip = PR_htonl(PR_INADDR_ANY);
|
---|
100 | for (i = 0; i < POLL_DESC_COUNT; i++) {
|
---|
101 | sock = PR_NewTCPSocket();
|
---|
102 | if (sock == NULL) {
|
---|
103 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
104 | exit(1);
|
---|
105 | }
|
---|
106 | if (PR_Bind(sock, &addr) == PR_FAILURE) {
|
---|
107 | fprintf(stderr, "PR_Bind failed\n");
|
---|
108 | exit(1);
|
---|
109 | }
|
---|
110 | if (PR_Listen(sock, 5) == PR_FAILURE) {
|
---|
111 | fprintf(stderr, "PR_Listen failed\n");
|
---|
112 | exit(1);
|
---|
113 | }
|
---|
114 |
|
---|
115 | pd[i].fd = sock;
|
---|
116 | pd[i].in_flags = PR_POLL_READ;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* first run the test on the primordial thread */
|
---|
120 | Test();
|
---|
121 |
|
---|
122 | /* then run the test on all three kinds of threads */
|
---|
123 | thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
|
---|
124 | PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
125 | if (NULL == thread) {
|
---|
126 | fprintf(stderr, "PR_CreateThread failed\n");
|
---|
127 | exit(1);
|
---|
128 | }
|
---|
129 | if (PR_JoinThread(thread) == PR_FAILURE) {
|
---|
130 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
131 | exit(1);
|
---|
132 | }
|
---|
133 | thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
|
---|
134 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
135 | if (NULL == thread) {
|
---|
136 | fprintf(stderr, "PR_CreateThread failed\n");
|
---|
137 | exit(1);
|
---|
138 | }
|
---|
139 | if (PR_JoinThread(thread) == PR_FAILURE) {
|
---|
140 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
141 | exit(1);
|
---|
142 | }
|
---|
143 | thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
|
---|
144 | PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
145 | if (NULL == thread) {
|
---|
146 | fprintf(stderr, "PR_CreateThread failed\n");
|
---|
147 | exit(1);
|
---|
148 | }
|
---|
149 | if (PR_JoinThread(thread) == PR_FAILURE) {
|
---|
150 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
151 | exit(1);
|
---|
152 | }
|
---|
153 | for (i = 0; i < POLL_DESC_COUNT; i++) {
|
---|
154 | if (PR_Close(pd[i].fd) == PR_FAILURE) {
|
---|
155 | fprintf(stderr, "PR_Close failed\n");
|
---|
156 | exit(1);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | PR_Cleanup();
|
---|
160 | printf("PASS\n");
|
---|
161 | return 0;
|
---|
162 | }
|
---|