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) 1998-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 is a test for the io continuation thread machinery
|
---|
40 | * in pthreads.
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include "nspr.h"
|
---|
44 | #include <stdio.h>
|
---|
45 |
|
---|
46 | int num_threads = 10; /* must be an even number */
|
---|
47 | PRThreadScope thread_scope = PR_GLOBAL_THREAD;
|
---|
48 |
|
---|
49 | void ThreadFunc(void *arg)
|
---|
50 | {
|
---|
51 | PRFileDesc *fd = (PRFileDesc *) arg;
|
---|
52 | char buf[1024];
|
---|
53 | PRInt32 nbytes;
|
---|
54 | PRErrorCode err;
|
---|
55 |
|
---|
56 | nbytes = PR_Recv(fd, buf, sizeof(buf), 0, PR_SecondsToInterval(20));
|
---|
57 | if (nbytes == -1) {
|
---|
58 | err = PR_GetError();
|
---|
59 | if (err != PR_PENDING_INTERRUPT_ERROR) {
|
---|
60 | fprintf(stderr, "PR_Recv failed: (%d, %d)\n",
|
---|
61 | err, PR_GetOSError());
|
---|
62 | PR_ProcessExit(1);
|
---|
63 | }
|
---|
64 | /*
|
---|
65 | * After getting an I/O interrupt, this thread must
|
---|
66 | * close the fd before it exits due to a limitation
|
---|
67 | * of our NT implementation.
|
---|
68 | */
|
---|
69 | if (PR_Close(fd) == PR_FAILURE) {
|
---|
70 | fprintf(stderr, "PR_Close failed\n");
|
---|
71 | PR_ProcessExit(1);
|
---|
72 | }
|
---|
73 | } else {
|
---|
74 | fprintf(stderr, "PR_Recv received %d bytes!?\n", nbytes);
|
---|
75 | PR_ProcessExit(1);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | int main(int argc, char **argv)
|
---|
80 | {
|
---|
81 | PRFileDesc **fds;
|
---|
82 | PRThread **threads;
|
---|
83 | PRIntervalTime start, elapsed;
|
---|
84 | int index;
|
---|
85 |
|
---|
86 | fds = (PRFileDesc **) PR_MALLOC(2 * num_threads * sizeof(PRFileDesc *));
|
---|
87 | PR_ASSERT(fds != NULL);
|
---|
88 | threads = (PRThread **) PR_MALLOC(num_threads * sizeof(PRThread *));
|
---|
89 | PR_ASSERT(threads != NULL);
|
---|
90 |
|
---|
91 | for (index = 0; index < num_threads; index++) {
|
---|
92 | if (PR_NewTCPSocketPair(&fds[2 * index]) == PR_FAILURE) {
|
---|
93 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
94 | PR_ProcessExit(1);
|
---|
95 | }
|
---|
96 | threads[index] = PR_CreateThread(
|
---|
97 | PR_USER_THREAD, ThreadFunc, fds[2 * index],
|
---|
98 | PR_PRIORITY_NORMAL, thread_scope, PR_JOINABLE_THREAD, 0);
|
---|
99 | if (NULL == threads[index]) {
|
---|
100 | fprintf(stderr, "PR_CreateThread failed\n");
|
---|
101 | PR_ProcessExit(1);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* Let the threads block in PR_Recv */
|
---|
106 | PR_Sleep(PR_SecondsToInterval(2));
|
---|
107 |
|
---|
108 | printf("Interrupting the threads\n");
|
---|
109 | fflush(stdout);
|
---|
110 | start = PR_IntervalNow();
|
---|
111 | for (index = 0; index < num_threads; index++) {
|
---|
112 | if (PR_Interrupt(threads[index]) == PR_FAILURE) {
|
---|
113 | fprintf(stderr, "PR_Interrupt failed\n");
|
---|
114 | PR_ProcessExit(1);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | for (index = 0; index < num_threads; index++) {
|
---|
118 | if (PR_JoinThread(threads[index]) == PR_FAILURE) {
|
---|
119 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
120 | PR_ProcessExit(1);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start);
|
---|
124 | printf("Threads terminated in %d milliseconds\n",
|
---|
125 | PR_IntervalToMilliseconds(elapsed));
|
---|
126 | fflush(stdout);
|
---|
127 |
|
---|
128 | /* We are being very generous and allow 10 seconds. */
|
---|
129 | if (elapsed >= PR_SecondsToInterval(10)) {
|
---|
130 | fprintf(stderr, "Interrupting threads took longer than 10 seconds!!\n");
|
---|
131 | PR_ProcessExit(1);
|
---|
132 | }
|
---|
133 |
|
---|
134 | for (index = 0; index < num_threads; index++) {
|
---|
135 | /* fds[2 * index] was passed to and closed by threads[index]. */
|
---|
136 | if (PR_Close(fds[2 * index + 1]) == PR_FAILURE) {
|
---|
137 | fprintf(stderr, "PR_Close failed\n");
|
---|
138 | PR_ProcessExit(1);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | PR_DELETE(threads);
|
---|
142 | PR_DELETE(fds);
|
---|
143 | printf("PASS\n");
|
---|
144 | PR_Cleanup();
|
---|
145 | return 0;
|
---|
146 | }
|
---|