nsprpub/pr/tests/joinuk.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     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: joinuk.c
     9 **
    10 ** Description: Join kernel - user
    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 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
    21 **			recognize the return code from tha main program.
    22 ***********************************************************************/
    24 /***********************************************************************
    25 ** Includes
    26 ***********************************************************************/
    27 /* Used to get the command line option */
    28 #include "plgetopt.h"
    30 #include "nspr.h"
    32 #include <stdio.h>
    33 #include <stdlib.h>
    34 #include <string.h>
    36 PRIntn failed_already=0;
    37 PRIntn debug_mode;
    38 /*
    39 	Program to test joining of threads.  Two threads are created.  One
    40 	to be waited upon until it has started.  The other to join after it has
    41 	completed.
    42 */
    45 static void lowPriority(void *arg)
    46 {
    47 }
    49 static void highPriority(void *arg)
    50 {
    51 }
    53 void runTest(PRThreadScope scope1, PRThreadScope scope2)
    54 {
    55 	PRThread *low,*high;
    57 	/* create the low and high priority threads */
    59 	low = PR_CreateThread(PR_USER_THREAD,
    60 				      lowPriority, 0, 
    61 				      PR_PRIORITY_LOW,
    62 				      scope1,
    63     				  PR_JOINABLE_THREAD,
    64 				      0);
    65 	if (!low) {
    66 		if (debug_mode) printf("\tcannot create low priority thread\n");
    67 		else failed_already=1;
    68 		return;
    69 	}
    71 	high = PR_CreateThread(PR_USER_THREAD,
    72 				      highPriority, 0, 
    73 				      PR_PRIORITY_HIGH,
    74 				      scope2,
    75     				  PR_JOINABLE_THREAD,
    76 				      0);
    77 	if (!high) {
    78 		if (debug_mode) printf("\tcannot create high priority thread\n");
    79 		else failed_already=1;
    80 		return;
    81 	}
    83 	/* Do the joining for both threads */
    84 	if (PR_JoinThread(low) == PR_FAILURE) {
    85 		if (debug_mode) printf("\tcannot join low priority thread\n");
    86 		else failed_already=1;
    87 		return;
    88 	} else {
    89     	if (debug_mode) printf("\tjoined low priority thread\n");
    90     }
    91 	if (PR_JoinThread(high) == PR_FAILURE) {
    92 		if (debug_mode) printf("\tcannot join high priority thread\n");
    93 		else failed_already=1;
    94 		return;
    95 	} else {
    96     	if (debug_mode) printf("\tjoined high priority thread\n");
    97     }
    98 }
   100 static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
   101 {
   102 	/* The command line argument: -d is used to determine if the test is being run
   103 	in debug mode. The regress tool requires only one line output:PASS or FAIL.
   104 	All of the printfs associated with this test has been handled with a if (debug_mode)
   105 	test.
   106 	Usage: test_name -d
   107 	*/
   109 	PLOptStatus os;
   110 	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
   111 	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   112     {
   113 		if (PL_OPT_BAD == os) continue;
   114         switch (opt->option)
   115         {
   116         case 'd':  /* debug mode */
   117 			debug_mode = 1;
   118             break;
   119          default:
   120             break;
   121         }
   122     }
   123 	PL_DestroyOptState(opt);
   125     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   126     PR_STDIO_INIT();
   128  /* main test */
   130     if (debug_mode) printf("User-Kernel test\n");
   131     runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
   134 	if(failed_already)	
   135 	{
   136         printf("FAIL\n");
   137 		return 1;
   138     } else 
   139     {
   140         printf("PASS\n");
   141 		return 0;
   142     }
   143 }
   146 int main(int argc, char **argv)
   147 {
   148     PRIntn rv;
   150     PR_STDIO_INIT();
   151     rv = PR_Initialize(RealMain, argc, argv, 0);
   152     return rv;
   153 }  /* main */

mercurial