nsprpub/pr/tests/primblok.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.

     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:        primblok.c
     8  * Purpose:     testing whether the primordial thread can block in a
     9  *              native blocking function without affecting the correct
    10  *              functioning of NSPR I/O functions (Bugzilla bug #30746)
    11  */
    13 #if !defined(WINNT)
    15 #include <stdio.h>
    17 int main(int argc, char **argv)
    18 {
    19     printf("This test is not relevant on this platform\n");
    20     return 0;
    21 }
    23 #else /* WINNT */
    25 #include "nspr.h"
    27 #include <windows.h>
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <string.h>
    32 #define TEST_FILE_NAME "primblok.dat"
    34 /* use InterlockedExchange to update this variable */
    35 static LONG iothread_done;
    37 static void PR_CALLBACK IOThread(void *arg)
    38 {
    39     PRFileDesc *fd;
    40     char buf[32];
    41     PRInt32 nbytes;
    43     /* Give the primordial thread one second to block */
    44     Sleep(1000);
    46     /*
    47      * See if our PR_Write call will hang when the primordial
    48      * thread is blocking in a native blocking function.
    49      */
    50     fd = PR_Open(TEST_FILE_NAME, PR_WRONLY|PR_CREATE_FILE, 0666);
    51     if (NULL == fd) {
    52         fprintf(stderr, "PR_Open failed\n");
    53         exit(1);
    54     }
    55     memset(buf, 0xaf, sizeof(buf));
    56     fprintf(stderr, "iothread: calling PR_Write\n");
    57     nbytes = PR_Write(fd, buf, sizeof(buf));
    58     fprintf(stderr, "iothread: PR_Write returned\n");
    59     if (nbytes != sizeof(buf)) {
    60         fprintf(stderr, "PR_Write returned %d\n", nbytes);
    61         exit(1);
    62     }
    63     if (PR_Close(fd) == PR_FAILURE) {
    64         fprintf(stderr, "PR_Close failed\n");
    65         exit(1);
    66     }
    67     if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
    68         fprintf(stderr, "PR_Delete failed\n");
    69         exit(1);
    70     }
    72     /* Tell the main thread that we are done */
    73     InterlockedExchange(&iothread_done, 1);
    74 }
    76 int main(int argc, char **argv)
    77 {
    78     PRThread *iothread;
    80     /* Must be a global thread */
    81     iothread = PR_CreateThread(
    82         PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
    83         PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
    84     if (iothread == NULL) {
    85         fprintf(stderr, "cannot create thread\n");
    86         exit(1);
    87     }
    89     /*
    90      * Block in a native blocking function.
    91      * Give iothread 5 seconds to finish its task.
    92      */
    93     Sleep(5000);
    95     /*
    96      * Is iothread done or is it hung?
    97      *
    98      * I'm actually only interested in reading the value
    99      * of iothread_done.  I'm using InterlockedExchange as
   100      * a thread-safe way to read iothread_done.
   101      */
   102     if (InterlockedExchange(&iothread_done, 1) == 0) {
   103         fprintf(stderr, "iothread is hung\n");
   104         fprintf(stderr, "FAILED\n");
   105         exit(1);
   106     }
   108     if (PR_JoinThread(iothread) == PR_FAILURE) {
   109         fprintf(stderr, "PR_JoinThread failed\n");
   110         exit(1);
   111     }
   112     printf("PASSED\n");
   113     return 0;
   114 }  /* main */
   116 #endif /* WINNT */

mercurial