Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
6 /*
7 * File: intrio.c
8 * Purpose: testing i/o interrupts (see Bugzilla bug #31120)
9 */
11 #include "nspr.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 /* for synchronization between the main thread and iothread */
18 static PRLock *lock;
19 static PRCondVar *cvar;
20 static PRBool iothread_ready;
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 */
30 static void PR_CALLBACK IOThread(void *arg)
31 {
32 PRFileDesc *sock, *newsock;
33 PRNetAddr addr;
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 }
75 static void Test(PRThreadScope scope1, PRThreadScope scope2)
76 {
77 PRThread *iothread, *abortio;
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 PR_Unlock(lock);
94 abortio = PR_CreateThread(
95 PR_USER_THREAD, AbortIO, iothread, PR_PRIORITY_NORMAL,
96 scope2, PR_JOINABLE_THREAD, 0);
97 if (abortio == NULL) {
98 fprintf(stderr, "cannot create thread\n");
99 exit(1);
100 }
101 if (PR_JoinThread(iothread) == PR_FAILURE) {
102 fprintf(stderr, "PR_JoinThread failed\n");
103 exit(1);
104 }
105 if (PR_JoinThread(abortio) == PR_FAILURE) {
106 fprintf(stderr, "PR_JoinThread failed\n");
107 exit(1);
108 }
109 }
111 int main(int argc, char **argv)
112 {
113 PR_STDIO_INIT();
114 lock = PR_NewLock();
115 if (lock == NULL) {
116 fprintf(stderr, "PR_NewLock failed\n");
117 exit(1);
118 }
119 cvar = PR_NewCondVar(lock);
120 if (cvar == NULL) {
121 fprintf(stderr, "PR_NewCondVar failed\n");
122 exit(1);
123 }
124 /* test all four combinations */
125 Test(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
126 Test(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
127 Test(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
128 Test(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
129 printf("PASSED\n");
130 return 0;
131 } /* main */