nsprpub/pr/tests/ioconthr.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

     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  * This is a test for the io continuation thread machinery
     8  * in pthreads.
     9  */
    11 #include "nspr.h"
    12 #include <stdio.h>
    14 int num_threads = 10;  /* must be an even number */
    15 PRThreadScope thread_scope = PR_GLOBAL_THREAD;
    17 void ThreadFunc(void *arg)
    18 {
    19     PRFileDesc *fd = (PRFileDesc *) arg;
    20     char buf[1024];
    21     PRInt32 nbytes;
    22     PRErrorCode err;
    24     nbytes = PR_Recv(fd, buf, sizeof(buf), 0, PR_SecondsToInterval(20));
    25     if (nbytes == -1) {
    26         err = PR_GetError();
    27         if (err != PR_PENDING_INTERRUPT_ERROR) {
    28             fprintf(stderr, "PR_Recv failed: (%d, %d)\n",
    29                     err, PR_GetOSError());
    30             PR_ProcessExit(1);
    31         }
    32         /*
    33          * After getting an I/O interrupt, this thread must
    34          * close the fd before it exits due to a limitation
    35          * of our NT implementation.
    36          */
    37         if (PR_Close(fd) == PR_FAILURE) {
    38             fprintf(stderr, "PR_Close failed\n");
    39             PR_ProcessExit(1);
    40         }
    41     } else {
    42         fprintf(stderr, "PR_Recv received %d bytes!?\n", nbytes);
    43         PR_ProcessExit(1);
    44     }
    45 }
    47 int main(int argc, char **argv)
    48 {
    49     PRFileDesc **fds;
    50     PRThread **threads;
    51     PRIntervalTime start, elapsed;
    52     int index;
    54     fds = (PRFileDesc **) PR_MALLOC(2 * num_threads * sizeof(PRFileDesc *));
    55     PR_ASSERT(fds != NULL);
    56     threads = (PRThread **) PR_MALLOC(num_threads * sizeof(PRThread *));
    57     PR_ASSERT(threads != NULL);
    59     for (index = 0; index < num_threads; index++) {
    60         if (PR_NewTCPSocketPair(&fds[2 * index]) == PR_FAILURE) {
    61             fprintf(stderr, "PR_NewTCPSocket failed\n");
    62             PR_ProcessExit(1);
    63         }
    64         threads[index] = PR_CreateThread(
    65                 PR_USER_THREAD, ThreadFunc, fds[2 * index],
    66                 PR_PRIORITY_NORMAL, thread_scope, PR_JOINABLE_THREAD, 0);
    67         if (NULL == threads[index]) {
    68             fprintf(stderr, "PR_CreateThread failed\n");
    69             PR_ProcessExit(1);
    70         }
    71     }
    73     /* Let the threads block in PR_Recv */
    74     PR_Sleep(PR_SecondsToInterval(2));
    76     printf("Interrupting the threads\n");
    77     fflush(stdout);
    78     start = PR_IntervalNow();
    79     for (index = 0; index < num_threads; index++) {
    80         if (PR_Interrupt(threads[index]) == PR_FAILURE) {
    81             fprintf(stderr, "PR_Interrupt failed\n");
    82             PR_ProcessExit(1);
    83         }
    84     }
    85     for (index = 0; index < num_threads; index++) {
    86         if (PR_JoinThread(threads[index]) == PR_FAILURE) {
    87             fprintf(stderr, "PR_JoinThread failed\n");
    88             PR_ProcessExit(1);
    89         }
    90     }
    91     elapsed = (PRIntervalTime)(PR_IntervalNow() - start);
    92     printf("Threads terminated in %d milliseconds\n",
    93             PR_IntervalToMilliseconds(elapsed));
    94     fflush(stdout);
    96     /* We are being very generous and allow 10 seconds. */
    97     if (elapsed >= PR_SecondsToInterval(10)) {
    98         fprintf(stderr, "Interrupting threads took longer than 10 seconds!!\n");
    99         PR_ProcessExit(1);
   100     }
   102     for (index = 0; index < num_threads; index++) {
   103         /* fds[2 * index] was passed to and closed by threads[index]. */
   104         if (PR_Close(fds[2 * index + 1]) == PR_FAILURE) {
   105             fprintf(stderr, "PR_Close failed\n");
   106             PR_ProcessExit(1);
   107         }
   108     }
   109     PR_DELETE(threads);
   110     PR_DELETE(fds);
   111     printf("PASS\n");
   112     PR_Cleanup();
   113     return 0;
   114 }

mercurial