nsprpub/pr/tests/join.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial