tor-browser

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

join.c (6053B)


      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 **
      8 ** Name: dbmalloc1.c
      9 **
     10 ** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
     11 **
     12 ** Modification History:
     13 **
     14 ** 19-May-97 AGarcia - separate the four join tests into different unit test
     15 *modules.
     16 **          AGarcia- Converted the test to accomodate the debug_mode flag.
     17 **          The debug mode will print all of the printfs associated with this
     18 *test.
     19 **          The regress mode will be the default mode. Since the regress tool
     20 *limits
     21 **          the output to a one line status:PASS or FAIL,all of the printf
     22 *statements
     23 **          have been handled with an if (debug_mode) statement.
     24 ***********************************************************************/
     25 
     26 /***********************************************************************
     27 ** Includes
     28 ***********************************************************************/
     29 /* Used to get the command line option */
     30 #include "plgetopt.h"
     31 #include "prttools.h"
     32 
     33 #include "nspr.h"
     34 
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 
     39 /***********************************************************************
     40 ** PRIVATE FUNCTION:    Test_Result
     41 ** DESCRIPTION: Used in conjunction with the regress tool, prints out the
     42 **              status of the test case.
     43 ** INPUTS:      PASS/FAIL
     44 ** OUTPUTS:     None
     45 ** RETURN:      None
     46 ** SIDE EFFECTS:
     47 **
     48 ** RESTRICTIONS:
     49 **      None
     50 ** MEMORY:      NA
     51 ** ALGORITHM:   Determine what the status is and print accordingly.
     52 **
     53 ***********************************************************************/
     54 
     55 static void Test_Result(int result) {
     56  if (result == PASS) {
     57    printf("PASS\n");
     58  } else {
     59    printf("FAIL\n");
     60  }
     61  exit(1);
     62 }
     63 
     64 /*
     65    Program to test joining of threads.  Two threads are created.  One
     66    to be waited upon until it has started.  The other to join after it has
     67    completed.
     68 */
     69 
     70 static void PR_CALLBACK lowPriority(void* arg) {}
     71 
     72 static void PR_CALLBACK highPriority(void* arg) {}
     73 
     74 static void PR_CALLBACK unjoinable(void* arg) {
     75  PR_Sleep(PR_INTERVAL_NO_TIMEOUT);
     76 }
     77 
     78 void runTest(PRThreadScope scope1, PRThreadScope scope2) {
     79  PRThread *low, *high;
     80 
     81  /* create the low and high priority threads */
     82 
     83  low = PR_CreateThread(PR_USER_THREAD, lowPriority, 0, PR_PRIORITY_LOW, scope1,
     84                        PR_JOINABLE_THREAD, 0);
     85  if (!low) {
     86    if (debug_mode) {
     87      printf("\tcannot create low priority thread\n");
     88    } else {
     89      Test_Result(FAIL);
     90    }
     91    return;
     92  }
     93 
     94  high = PR_CreateThread(PR_USER_THREAD, highPriority, 0, PR_PRIORITY_HIGH,
     95                         scope2, PR_JOINABLE_THREAD, 0);
     96  if (!high) {
     97    if (debug_mode) {
     98      printf("\tcannot create high priority thread\n");
     99    } else {
    100      Test_Result(FAIL);
    101    }
    102    return;
    103  }
    104 
    105  /* Do the joining for both threads */
    106  if (PR_JoinThread(low) == PR_FAILURE) {
    107    if (debug_mode) {
    108      printf("\tcannot join low priority thread\n");
    109    } else {
    110      Test_Result(FAIL);
    111    }
    112    return;
    113  } else {
    114    if (debug_mode) {
    115      printf("\tjoined low priority thread\n");
    116    }
    117  }
    118  if (PR_JoinThread(high) == PR_FAILURE) {
    119    if (debug_mode) {
    120      printf("\tcannot join high priority thread\n");
    121    } else {
    122      Test_Result(FAIL);
    123    }
    124    return;
    125  } else {
    126    if (debug_mode) {
    127      printf("\tjoined high priority thread\n");
    128    }
    129  }
    130 }
    131 
    132 void joinWithUnjoinable(void) {
    133  PRThread* thread;
    134 
    135  /* create the unjoinable thread */
    136 
    137  thread = PR_CreateThread(PR_USER_THREAD, unjoinable, 0, PR_PRIORITY_NORMAL,
    138                           PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0);
    139  if (!thread) {
    140    if (debug_mode) {
    141      printf("\tcannot create unjoinable thread\n");
    142    } else {
    143      Test_Result(FAIL);
    144    }
    145    return;
    146  }
    147 
    148  if (PR_JoinThread(thread) == PR_SUCCESS) {
    149    if (debug_mode) {
    150      printf("\tsuccessfully joined with unjoinable thread?!\n");
    151    } else {
    152      Test_Result(FAIL);
    153    }
    154    return;
    155  } else {
    156    if (debug_mode) {
    157      printf("\tcannot join with unjoinable thread, as expected\n");
    158    }
    159    if (PR_GetError() != PR_INVALID_ARGUMENT_ERROR) {
    160      if (debug_mode) {
    161        printf("\tWrong error code\n");
    162      } else {
    163        Test_Result(FAIL);
    164      }
    165      return;
    166    }
    167  }
    168  if (PR_Interrupt(thread) == PR_FAILURE) {
    169    if (debug_mode) {
    170      printf("\tcannot interrupt unjoinable thread\n");
    171    } else {
    172      Test_Result(FAIL);
    173    }
    174    return;
    175  } else {
    176    if (debug_mode) {
    177      printf("\tinterrupted unjoinable thread\n");
    178    }
    179  }
    180 }
    181 
    182 static PRIntn PR_CALLBACK RealMain(int argc, char** argv) {
    183  /* The command line argument: -d is used to determine if the test is being run
    184  in debug mode. The regress tool requires only one line output:PASS or FAIL.
    185  All of the printfs associated with this test has been handled with a if
    186  (debug_mode) test. Usage: test_name -d
    187  */
    188 
    189  PLOptStatus os;
    190  PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
    191  while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
    192    if (PL_OPT_BAD == os) {
    193      continue;
    194    }
    195    switch (opt->option) {
    196      case 'd': /* debug mode */
    197        debug_mode = 1;
    198        break;
    199      default:
    200        break;
    201    }
    202  }
    203  PL_DestroyOptState(opt);
    204 
    205  /* main test */
    206  printf("User-User test\n");
    207  runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
    208  printf("User-Kernel test\n");
    209  runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
    210  printf("Kernel-User test\n");
    211  runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
    212  printf("Kernel-Kernel test\n");
    213  runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
    214  printf("Join with unjoinable thread\n");
    215  joinWithUnjoinable();
    216 
    217  printf("PASSED\n");
    218 
    219  return 0;
    220 }
    221 
    222 int main(int argc, char** argv) {
    223  PRIntn rv;
    224 
    225  rv = PR_Initialize(RealMain, argc, argv, 0);
    226  return rv;
    227 } /* main */