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 | * File: intrio.c
|
---|
40 | * Purpose: testing i/o interrupts (see Bugzilla bug #31120)
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include "nspr.h"
|
---|
44 |
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <string.h>
|
---|
48 |
|
---|
49 | #ifdef XP_MAC
|
---|
50 | #include "prlog.h"
|
---|
51 | #define printf PR_LogPrint
|
---|
52 | extern void SetupMacPrintfLog(char *logFile);
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | /* for synchronization between the main thread and iothread */
|
---|
56 | static PRLock *lock;
|
---|
57 | static PRCondVar *cvar;
|
---|
58 | static PRBool iothread_ready;
|
---|
59 |
|
---|
60 | static void PR_CALLBACK AbortIO(void *arg)
|
---|
61 | {
|
---|
62 | PRStatus rv;
|
---|
63 | PR_Sleep(PR_SecondsToInterval(2));
|
---|
64 | rv = PR_Interrupt((PRThread*)arg);
|
---|
65 | PR_ASSERT(PR_SUCCESS == rv);
|
---|
66 | } /* AbortIO */
|
---|
67 |
|
---|
68 | static void PR_CALLBACK IOThread(void *arg)
|
---|
69 | {
|
---|
70 | PRFileDesc *sock, *newsock;
|
---|
71 | PRNetAddr addr;
|
---|
72 |
|
---|
73 | sock = PR_OpenTCPSocket(PR_AF_INET6);
|
---|
74 | if (sock == NULL) {
|
---|
75 | fprintf(stderr, "PR_OpenTCPSocket failed\n");
|
---|
76 | exit(1);
|
---|
77 | }
|
---|
78 | memset(&addr, 0, sizeof(addr));
|
---|
79 | if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
|
---|
80 | fprintf(stderr, "PR_SetNetAddr failed\n");
|
---|
81 | exit(1);
|
---|
82 | }
|
---|
83 | if (PR_Bind(sock, &addr) == PR_FAILURE) {
|
---|
84 | fprintf(stderr, "PR_Bind failed\n");
|
---|
85 | exit(1);
|
---|
86 | }
|
---|
87 | if (PR_Listen(sock, 5) == PR_FAILURE) {
|
---|
88 | fprintf(stderr, "PR_Listen failed\n");
|
---|
89 | exit(1);
|
---|
90 | }
|
---|
91 | /* tell the main thread that we are ready */
|
---|
92 | PR_Lock(lock);
|
---|
93 | iothread_ready = PR_TRUE;
|
---|
94 | PR_NotifyCondVar(cvar);
|
---|
95 | PR_Unlock(lock);
|
---|
96 | newsock = PR_Accept(sock, NULL, PR_INTERVAL_NO_TIMEOUT);
|
---|
97 | if (newsock != NULL) {
|
---|
98 | fprintf(stderr, "PR_Accept shouldn't have succeeded\n");
|
---|
99 | exit(1);
|
---|
100 | }
|
---|
101 | if (PR_GetError() != PR_PENDING_INTERRUPT_ERROR) {
|
---|
102 | fprintf(stderr, "PR_Accept failed (%d, %d)\n",
|
---|
103 | PR_GetError(), PR_GetOSError());
|
---|
104 | exit(1);
|
---|
105 | }
|
---|
106 | printf("PR_Accept() is interrupted as expected\n");
|
---|
107 | if (PR_Close(sock) == PR_FAILURE) {
|
---|
108 | fprintf(stderr, "PR_Close failed\n");
|
---|
109 | exit(1);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void Test(PRThreadScope scope1, PRThreadScope scope2)
|
---|
114 | {
|
---|
115 | PRThread *iothread, *abortio;
|
---|
116 |
|
---|
117 | printf("A %s thread will be interrupted by a %s thread\n",
|
---|
118 | (scope1 == PR_LOCAL_THREAD ? "local" : "global"),
|
---|
119 | (scope2 == PR_LOCAL_THREAD ? "local" : "global"));
|
---|
120 | iothread_ready = PR_FALSE;
|
---|
121 | iothread = PR_CreateThread(
|
---|
122 | PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
|
---|
123 | scope1, PR_JOINABLE_THREAD, 0);
|
---|
124 | if (iothread == NULL) {
|
---|
125 | fprintf(stderr, "cannot create thread\n");
|
---|
126 | exit(1);
|
---|
127 | }
|
---|
128 | PR_Lock(lock);
|
---|
129 | while (!iothread_ready)
|
---|
130 | PR_WaitCondVar(cvar, PR_INTERVAL_NO_TIMEOUT);
|
---|
131 | PR_Unlock(lock);
|
---|
132 | abortio = PR_CreateThread(
|
---|
133 | PR_USER_THREAD, AbortIO, iothread, PR_PRIORITY_NORMAL,
|
---|
134 | scope2, PR_JOINABLE_THREAD, 0);
|
---|
135 | if (abortio == NULL) {
|
---|
136 | fprintf(stderr, "cannot create thread\n");
|
---|
137 | exit(1);
|
---|
138 | }
|
---|
139 | if (PR_JoinThread(iothread) == PR_FAILURE) {
|
---|
140 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
141 | exit(1);
|
---|
142 | }
|
---|
143 | if (PR_JoinThread(abortio) == PR_FAILURE) {
|
---|
144 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
145 | exit(1);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | PRIntn main(PRIntn argc, char **argv)
|
---|
150 | {
|
---|
151 | PR_STDIO_INIT();
|
---|
152 | lock = PR_NewLock();
|
---|
153 | if (lock == NULL) {
|
---|
154 | fprintf(stderr, "PR_NewLock failed\n");
|
---|
155 | exit(1);
|
---|
156 | }
|
---|
157 | cvar = PR_NewCondVar(lock);
|
---|
158 | if (cvar == NULL) {
|
---|
159 | fprintf(stderr, "PR_NewCondVar failed\n");
|
---|
160 | exit(1);
|
---|
161 | }
|
---|
162 | /* test all four combinations */
|
---|
163 | Test(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
|
---|
164 | Test(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
|
---|
165 | Test(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
|
---|
166 | Test(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
|
---|
167 | printf("PASSED\n");
|
---|
168 | return 0;
|
---|
169 | } /* main */
|
---|