nsprpub/pr/tests/intrio.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial