1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * File: intrio.c
8 * Purpose: testing i/o interrupts (see Bugzilla bug #31120)
9 */
10
11 #include "nspr.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 /* for synchronization between the main thread and iothread */
18 static PRLock *lock;
19 static PRCondVar *cvar;
20 static PRBool iothread_ready;
21
AbortIO(void * arg)22 static void PR_CALLBACK AbortIO(void *arg)
23 {
24 PRStatus rv;
25 PR_Sleep(PR_SecondsToInterval(2));
26 rv = PR_Interrupt((PRThread*)arg);
27 PR_ASSERT(PR_SUCCESS == rv);
28 } /* AbortIO */
29
IOThread(void * arg)30 static void PR_CALLBACK IOThread(void *arg)
31 {
32 PRFileDesc *sock, *newsock;
33 PRNetAddr addr;
34
35 sock = PR_OpenTCPSocket(PR_AF_INET6);
36 if (sock == NULL) {
37 fprintf(stderr, "PR_OpenTCPSocket failed\n");
38 exit(1);
39 }
40 memset(&addr, 0, sizeof(addr));
41 if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
42 fprintf(stderr, "PR_SetNetAddr failed\n");
43 exit(1);
44 }
45 if (PR_Bind(sock, &addr) == PR_FAILURE) {
46 fprintf(stderr, "PR_Bind failed\n");
47 exit(1);
48 }
49 if (PR_Listen(sock, 5) == PR_FAILURE) {
50 fprintf(stderr, "PR_Listen failed\n");
51 exit(1);
52 }
53 /* tell the main thread that we are ready */
54 PR_Lock(lock);
55 iothread_ready = PR_TRUE;
56 PR_NotifyCondVar(cvar);
57 PR_Unlock(lock);
58 newsock = PR_Accept(sock, NULL, PR_INTERVAL_NO_TIMEOUT);
59 if (newsock != NULL) {
60 fprintf(stderr, "PR_Accept shouldn't have succeeded\n");
61 exit(1);
62 }
63 if (PR_GetError() != PR_PENDING_INTERRUPT_ERROR) {
64 fprintf(stderr, "PR_Accept failed (%d, %d)\n",
65 PR_GetError(), PR_GetOSError());
66 exit(1);
67 }
68 printf("PR_Accept() is interrupted as expected\n");
69 if (PR_Close(sock) == PR_FAILURE) {
70 fprintf(stderr, "PR_Close failed\n");
71 exit(1);
72 }
73 }
74
Test(PRThreadScope scope1,PRThreadScope scope2)75 static void Test(PRThreadScope scope1, PRThreadScope scope2)
76 {
77 PRThread *iothread, *abortio;
78
79 printf("A %s thread will be interrupted by a %s thread\n",
80 (scope1 == PR_LOCAL_THREAD ? "local" : "global"),
81 (scope2 == PR_LOCAL_THREAD ? "local" : "global"));
82 iothread_ready = PR_FALSE;
83 iothread = PR_CreateThread(
84 PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
85 scope1, PR_JOINABLE_THREAD, 0);
86 if (iothread == NULL) {
87 fprintf(stderr, "cannot create thread\n");
88 exit(1);
89 }
90 PR_Lock(lock);
91 while (!iothread_ready) {
92 PR_WaitCondVar(cvar, PR_INTERVAL_NO_TIMEOUT);
93 }
94 PR_Unlock(lock);
95 abortio = PR_CreateThread(
96 PR_USER_THREAD, AbortIO, iothread, PR_PRIORITY_NORMAL,
97 scope2, PR_JOINABLE_THREAD, 0);
98 if (abortio == NULL) {
99 fprintf(stderr, "cannot create thread\n");
100 exit(1);
101 }
102 if (PR_JoinThread(iothread) == PR_FAILURE) {
103 fprintf(stderr, "PR_JoinThread failed\n");
104 exit(1);
105 }
106 if (PR_JoinThread(abortio) == PR_FAILURE) {
107 fprintf(stderr, "PR_JoinThread failed\n");
108 exit(1);
109 }
110 }
111
main(int argc,char ** argv)112 int main(int argc, char **argv)
113 {
114 PR_STDIO_INIT();
115 lock = PR_NewLock();
116 if (lock == NULL) {
117 fprintf(stderr, "PR_NewLock failed\n");
118 exit(1);
119 }
120 cvar = PR_NewCondVar(lock);
121 if (cvar == NULL) {
122 fprintf(stderr, "PR_NewCondVar failed\n");
123 exit(1);
124 }
125 /* test all four combinations */
126 Test(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
127 Test(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
128 Test(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
129 Test(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
130 printf("PASSED\n");
131 return 0;
132 } /* main */
133