Back to index

lightning-sunbird  0.9+nobinonly
join.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 **
00040 ** Name: dbmalloc1.c
00041 **
00042 ** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
00043 **
00044 ** Modification History:
00045 ** 
00046 ** 19-May-97 AGarcia - separate the four join tests into different unit test modules.
00047 **                AGarcia- Converted the test to accomodate the debug_mode flag.
00048 **          The debug mode will print all of the printfs associated with this test.
00049 **                The regress mode will be the default mode. Since the regress tool limits
00050 **          the output to a one line status:PASS or FAIL,all of the printf statements
00051 **                have been handled with an if (debug_mode) statement. 
00052 ***********************************************************************/
00053 
00054 /***********************************************************************
00055 ** Includes
00056 ***********************************************************************/
00057 /* Used to get the command line option */
00058 #include "plgetopt.h"
00059 #include "prttools.h"
00060 
00061 #include "nspr.h"
00062 
00063 #include <stdio.h>
00064 #include <stdlib.h>
00065 #include <string.h>
00066 
00067 #ifdef XP_MAC
00068 #include "prlog.h"
00069 #define printf PR_LogPrint
00070 extern void SetupMacPrintfLog(char *logFile);
00071 #endif
00072 /***********************************************************************
00073 ** PRIVATE FUNCTION:    Test_Result
00074 ** DESCRIPTION: Used in conjunction with the regress tool, prints out the
00075 **                    status of the test case.
00076 ** INPUTS:      PASS/FAIL
00077 ** OUTPUTS:     None
00078 ** RETURN:      None
00079 ** SIDE EFFECTS:
00080 **      
00081 ** RESTRICTIONS:
00082 **      None
00083 ** MEMORY:      NA
00084 ** ALGORITHM:   Determine what the status is and print accordingly.
00085 **      
00086 ***********************************************************************/
00087 
00088 
00089 static void Test_Result (int result)
00090 {
00091     if (result == PASS)
00092         printf ("PASS\n");
00093     else
00094         printf ("FAIL\n");
00095     exit (1);
00096 }
00097 
00098 
00099 /*
00100     Program to test joining of threads.  Two threads are created.  One
00101     to be waited upon until it has started.  The other to join after it has
00102     completed.
00103 */
00104 
00105 
00106 static void PR_CALLBACK lowPriority(void *arg)
00107 {
00108 }
00109 
00110 static void PR_CALLBACK highPriority(void *arg)
00111 {
00112 }
00113 
00114 static void PR_CALLBACK unjoinable(void *arg)
00115 {
00116     PR_Sleep(PR_INTERVAL_NO_TIMEOUT);
00117 }
00118 
00119 void runTest(PRThreadScope scope1, PRThreadScope scope2)
00120 {
00121     PRThread *low,*high;
00122 
00123     /* create the low and high priority threads */
00124     
00125     low = PR_CreateThread(PR_USER_THREAD,
00126                      lowPriority, 0, 
00127                      PR_PRIORITY_LOW,
00128                      scope1,
00129                      PR_JOINABLE_THREAD,
00130                      0);
00131     if (!low) {
00132         if (debug_mode) printf("\tcannot create low priority thread\n");
00133         else Test_Result(FAIL);
00134         return;
00135     }
00136 
00137     high = PR_CreateThread(PR_USER_THREAD,
00138                      highPriority, 0, 
00139                      PR_PRIORITY_HIGH,
00140                      scope2,
00141                      PR_JOINABLE_THREAD,
00142                      0);
00143     if (!high) {
00144         if (debug_mode) printf("\tcannot create high priority thread\n");
00145         else Test_Result(FAIL);
00146         return;
00147     }
00148 
00149     /* Do the joining for both threads */
00150     if (PR_JoinThread(low) == PR_FAILURE) {
00151         if (debug_mode) printf("\tcannot join low priority thread\n");
00152         else Test_Result (FAIL);
00153         return;
00154     } else {
00155         if (debug_mode) printf("\tjoined low priority thread\n");
00156     }
00157     if (PR_JoinThread(high) == PR_FAILURE) {
00158         if (debug_mode) printf("\tcannot join high priority thread\n");
00159         else Test_Result(FAIL);
00160         return;
00161     } else {
00162         if (debug_mode) printf("\tjoined high priority thread\n");
00163     }
00164 }
00165 
00166 void joinWithUnjoinable(void)
00167 {
00168     PRThread *thread;
00169 
00170     /* create the unjoinable thread */
00171     
00172     thread = PR_CreateThread(PR_USER_THREAD,
00173                      unjoinable, 0, 
00174                      PR_PRIORITY_NORMAL,
00175                      PR_GLOBAL_THREAD,
00176                      PR_UNJOINABLE_THREAD,
00177                      0);
00178     if (!thread) {
00179         if (debug_mode) printf("\tcannot create unjoinable thread\n");
00180         else Test_Result(FAIL);
00181         return;
00182     }
00183 
00184     if (PR_JoinThread(thread) == PR_SUCCESS) {
00185         if (debug_mode) printf("\tsuccessfully joined with unjoinable thread?!\n");
00186         else Test_Result(FAIL);
00187         return;
00188     } else {
00189         if (debug_mode) printf("\tcannot join with unjoinable thread, as expected\n");
00190         if (PR_GetError() != PR_INVALID_ARGUMENT_ERROR) {
00191             if (debug_mode) printf("\tWrong error code\n");
00192             else Test_Result(FAIL);
00193             return;
00194         }
00195     }
00196     if (PR_Interrupt(thread) == PR_FAILURE) {
00197         if (debug_mode) printf("\tcannot interrupt unjoinable thread\n");
00198         else Test_Result(FAIL);
00199         return;
00200     } else {
00201         if (debug_mode) printf("\tinterrupted unjoinable thread\n");
00202     }
00203 }
00204 
00205 static PRIntn PR_CALLBACK RealMain(int argc, char **argv)
00206 {
00207     /* The command line argument: -d is used to determine if the test is being run
00208     in debug mode. The regress tool requires only one line output:PASS or FAIL.
00209     All of the printfs associated with this test has been handled with a if (debug_mode)
00210     test.
00211     Usage: test_name -d
00212     */
00213     
00214     PLOptStatus os;
00215     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00216     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00217     {
00218         if (PL_OPT_BAD == os) continue;
00219         switch (opt->option)
00220         {
00221         case 'd':  /* debug mode */
00222             debug_mode = 1;
00223             break;
00224          default:
00225             break;
00226         }
00227     }
00228     PL_DestroyOptState(opt);
00229 
00230 #ifdef XP_MAC
00231     SetupMacPrintfLog("join.log");
00232     debug_mode = 1;
00233 #endif
00234 
00235     
00236     
00237  /* main test */
00238     printf("User-User test\n");
00239     runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
00240     printf("User-Kernel test\n");
00241     runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
00242     printf("Kernel-User test\n");
00243     runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
00244     printf("Kernel-Kernel test\n");
00245     runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
00246     printf("Join with unjoinable thread\n");
00247     joinWithUnjoinable();
00248 
00249     printf("PASSED\n");
00250 
00251     return 0;
00252 }
00253 
00254 
00255 
00256 
00257 PRIntn main(PRIntn argc, char *argv[])
00258 {
00259     PRIntn rv;
00260     
00261     PR_STDIO_INIT();
00262     rv = PR_Initialize(RealMain, argc, argv, 0);
00263     return rv;
00264 }  /* main */