tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

intrio.c (3624B)


      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 
     22 static void PR_CALLBACK AbortIO(void* arg) {
     23  PRStatus rv;
     24  PR_Sleep(PR_SecondsToInterval(2));
     25  rv = PR_Interrupt((PRThread*)arg);
     26  PR_ASSERT(PR_SUCCESS == rv);
     27 } /* AbortIO */
     28 
     29 static void PR_CALLBACK IOThread(void* arg) {
     30  PRFileDesc *sock, *newsock;
     31  PRNetAddr addr;
     32 
     33  sock = PR_OpenTCPSocket(PR_AF_INET6);
     34  if (sock == NULL) {
     35    fprintf(stderr, "PR_OpenTCPSocket failed\n");
     36    exit(1);
     37  }
     38  memset(&addr, 0, sizeof(addr));
     39  if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
     40    fprintf(stderr, "PR_SetNetAddr failed\n");
     41    exit(1);
     42  }
     43  if (PR_Bind(sock, &addr) == PR_FAILURE) {
     44    fprintf(stderr, "PR_Bind failed\n");
     45    exit(1);
     46  }
     47  if (PR_Listen(sock, 5) == PR_FAILURE) {
     48    fprintf(stderr, "PR_Listen failed\n");
     49    exit(1);
     50  }
     51  /* tell the main thread that we are ready */
     52  PR_Lock(lock);
     53  iothread_ready = PR_TRUE;
     54  PR_NotifyCondVar(cvar);
     55  PR_Unlock(lock);
     56  newsock = PR_Accept(sock, NULL, PR_INTERVAL_NO_TIMEOUT);
     57  if (newsock != NULL) {
     58    fprintf(stderr, "PR_Accept shouldn't have succeeded\n");
     59    exit(1);
     60  }
     61  if (PR_GetError() != PR_PENDING_INTERRUPT_ERROR) {
     62    fprintf(stderr, "PR_Accept failed (%d, %d)\n", PR_GetError(),
     63            PR_GetOSError());
     64    exit(1);
     65  }
     66  printf("PR_Accept() is interrupted as expected\n");
     67  if (PR_Close(sock) == PR_FAILURE) {
     68    fprintf(stderr, "PR_Close failed\n");
     69    exit(1);
     70  }
     71 }
     72 
     73 static void Test(PRThreadScope scope1, PRThreadScope scope2) {
     74  PRThread *iothread, *abortio;
     75 
     76  printf("A %s thread will be interrupted by a %s thread\n",
     77         (scope1 == PR_LOCAL_THREAD ? "local" : "global"),
     78         (scope2 == PR_LOCAL_THREAD ? "local" : "global"));
     79  iothread_ready = PR_FALSE;
     80  iothread = PR_CreateThread(PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
     81                             scope1, PR_JOINABLE_THREAD, 0);
     82  if (iothread == NULL) {
     83    fprintf(stderr, "cannot create thread\n");
     84    exit(1);
     85  }
     86  PR_Lock(lock);
     87  while (!iothread_ready) {
     88    PR_WaitCondVar(cvar, PR_INTERVAL_NO_TIMEOUT);
     89  }
     90  PR_Unlock(lock);
     91  abortio = PR_CreateThread(PR_USER_THREAD, AbortIO, iothread,
     92                            PR_PRIORITY_NORMAL, scope2, PR_JOINABLE_THREAD, 0);
     93  if (abortio == NULL) {
     94    fprintf(stderr, "cannot create thread\n");
     95    exit(1);
     96  }
     97  if (PR_JoinThread(iothread) == PR_FAILURE) {
     98    fprintf(stderr, "PR_JoinThread failed\n");
     99    exit(1);
    100  }
    101  if (PR_JoinThread(abortio) == PR_FAILURE) {
    102    fprintf(stderr, "PR_JoinThread failed\n");
    103    exit(1);
    104  }
    105 }
    106 
    107 int main(int argc, char** argv) {
    108  lock = PR_NewLock();
    109  if (lock == NULL) {
    110    fprintf(stderr, "PR_NewLock failed\n");
    111    exit(1);
    112  }
    113  cvar = PR_NewCondVar(lock);
    114  if (cvar == NULL) {
    115    fprintf(stderr, "PR_NewCondVar failed\n");
    116    exit(1);
    117  }
    118  /* test all four combinations */
    119  Test(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
    120  Test(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
    121  Test(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
    122  Test(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
    123  printf("PASSED\n");
    124  return 0;
    125 } /* main */