nsprpub/pr/tests/intrio.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/intrio.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * File:        intrio.c
    1.11 + * Purpose:     testing i/o interrupts (see Bugzilla bug #31120)
    1.12 + */
    1.13 +
    1.14 +#include "nspr.h"
    1.15 +
    1.16 +#include <stdio.h>
    1.17 +#include <stdlib.h>
    1.18 +#include <string.h>
    1.19 +
    1.20 +/* for synchronization between the main thread and iothread */
    1.21 +static PRLock *lock;
    1.22 +static PRCondVar *cvar;
    1.23 +static PRBool iothread_ready;
    1.24 +
    1.25 +static void PR_CALLBACK AbortIO(void *arg)
    1.26 +{
    1.27 +    PRStatus rv;
    1.28 +    PR_Sleep(PR_SecondsToInterval(2));
    1.29 +    rv = PR_Interrupt((PRThread*)arg);
    1.30 +    PR_ASSERT(PR_SUCCESS == rv);
    1.31 +}  /* AbortIO */
    1.32 +
    1.33 +static void PR_CALLBACK IOThread(void *arg)
    1.34 +{
    1.35 +    PRFileDesc *sock, *newsock;
    1.36 +    PRNetAddr addr;
    1.37 +
    1.38 +    sock = PR_OpenTCPSocket(PR_AF_INET6);
    1.39 +    if (sock == NULL) {
    1.40 +        fprintf(stderr, "PR_OpenTCPSocket failed\n");
    1.41 +        exit(1);
    1.42 +    }
    1.43 +    memset(&addr, 0, sizeof(addr));
    1.44 +    if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
    1.45 +        fprintf(stderr, "PR_SetNetAddr failed\n");
    1.46 +        exit(1);
    1.47 +    }
    1.48 +    if (PR_Bind(sock, &addr) == PR_FAILURE) {
    1.49 +        fprintf(stderr, "PR_Bind failed\n");
    1.50 +        exit(1);
    1.51 +    }
    1.52 +    if (PR_Listen(sock, 5) == PR_FAILURE) {
    1.53 +        fprintf(stderr, "PR_Listen failed\n");
    1.54 +        exit(1);
    1.55 +    }
    1.56 +    /* tell the main thread that we are ready */
    1.57 +    PR_Lock(lock);
    1.58 +    iothread_ready = PR_TRUE;
    1.59 +    PR_NotifyCondVar(cvar);
    1.60 +    PR_Unlock(lock);
    1.61 +    newsock = PR_Accept(sock, NULL, PR_INTERVAL_NO_TIMEOUT);
    1.62 +    if (newsock != NULL) {
    1.63 +        fprintf(stderr, "PR_Accept shouldn't have succeeded\n");
    1.64 +        exit(1);
    1.65 +    }
    1.66 +    if (PR_GetError() != PR_PENDING_INTERRUPT_ERROR) {
    1.67 +        fprintf(stderr, "PR_Accept failed (%d, %d)\n",
    1.68 +            PR_GetError(), PR_GetOSError());
    1.69 +        exit(1);
    1.70 +    }
    1.71 +    printf("PR_Accept() is interrupted as expected\n");
    1.72 +    if (PR_Close(sock) == PR_FAILURE) {
    1.73 +        fprintf(stderr, "PR_Close failed\n");
    1.74 +        exit(1);
    1.75 +    }
    1.76 +}
    1.77 +
    1.78 +static void Test(PRThreadScope scope1, PRThreadScope scope2)
    1.79 +{
    1.80 +    PRThread *iothread, *abortio;
    1.81 +
    1.82 +    printf("A %s thread will be interrupted by a %s thread\n",
    1.83 +        (scope1 == PR_LOCAL_THREAD ? "local" : "global"),
    1.84 +        (scope2 == PR_LOCAL_THREAD ? "local" : "global"));
    1.85 +    iothread_ready = PR_FALSE;
    1.86 +    iothread = PR_CreateThread(
    1.87 +        PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
    1.88 +        scope1, PR_JOINABLE_THREAD, 0);
    1.89 +    if (iothread == NULL) {
    1.90 +        fprintf(stderr, "cannot create thread\n");
    1.91 +        exit(1);
    1.92 +    }
    1.93 +    PR_Lock(lock);
    1.94 +    while (!iothread_ready)
    1.95 +        PR_WaitCondVar(cvar, PR_INTERVAL_NO_TIMEOUT);
    1.96 +    PR_Unlock(lock);
    1.97 +    abortio = PR_CreateThread(
    1.98 +        PR_USER_THREAD, AbortIO, iothread, PR_PRIORITY_NORMAL,
    1.99 +        scope2, PR_JOINABLE_THREAD, 0);
   1.100 +    if (abortio == NULL) {
   1.101 +        fprintf(stderr, "cannot create thread\n");
   1.102 +        exit(1);
   1.103 +    }
   1.104 +    if (PR_JoinThread(iothread) == PR_FAILURE) {
   1.105 +        fprintf(stderr, "PR_JoinThread failed\n");
   1.106 +        exit(1);
   1.107 +    }
   1.108 +    if (PR_JoinThread(abortio) == PR_FAILURE) {
   1.109 +        fprintf(stderr, "PR_JoinThread failed\n");
   1.110 +        exit(1);
   1.111 +    }
   1.112 +}
   1.113 +
   1.114 +int main(int argc, char **argv)
   1.115 +{
   1.116 +    PR_STDIO_INIT();
   1.117 +    lock = PR_NewLock();
   1.118 +    if (lock == NULL) {
   1.119 +        fprintf(stderr, "PR_NewLock failed\n");
   1.120 +        exit(1);
   1.121 +    }
   1.122 +    cvar = PR_NewCondVar(lock);
   1.123 +    if (cvar == NULL) {
   1.124 +        fprintf(stderr, "PR_NewCondVar failed\n");
   1.125 +        exit(1);
   1.126 +    }
   1.127 +    /* test all four combinations */
   1.128 +    Test(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
   1.129 +    Test(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
   1.130 +    Test(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
   1.131 +    Test(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
   1.132 +    printf("PASSED\n");
   1.133 +    return 0;
   1.134 +}  /* main */

mercurial