nsprpub/pr/tests/lazyinit.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** File: lazyinit.c
michael@0 8 ** Description: Testing lazy initialization
michael@0 9 **
michael@0 10 ** Since you only get to initialize once, you have to rerun the test
michael@0 11 ** for each test case. The test cases are numbered. If you want to
michael@0 12 ** add more tests, take the next number and add it to the switch
michael@0 13 ** statement.
michael@0 14 **
michael@0 15 ** This test is problematic on systems that don't support the notion
michael@0 16 ** of console output. The workarounds to emulate that feature include
michael@0 17 ** initializations themselves, which defeats the purpose here.
michael@0 18 */
michael@0 19
michael@0 20 #include "prcvar.h"
michael@0 21 #include "prenv.h"
michael@0 22 #include "prinit.h"
michael@0 23 #include "prinrval.h"
michael@0 24 #include "prio.h"
michael@0 25 #include "prlock.h"
michael@0 26 #include "prlog.h"
michael@0 27 #include "prthread.h"
michael@0 28 #include "prtypes.h"
michael@0 29
michael@0 30 #include <stdio.h>
michael@0 31 #include <stdlib.h>
michael@0 32
michael@0 33 static void PR_CALLBACK lazyEntry(void *arg)
michael@0 34 {
michael@0 35 PR_ASSERT(NULL == arg);
michael@0 36 } /* lazyEntry */
michael@0 37
michael@0 38
michael@0 39 int main(int argc, char **argv)
michael@0 40 {
michael@0 41 PRUintn pdkey;
michael@0 42 PRStatus status;
michael@0 43 char *path = NULL;
michael@0 44 PRDir *dir = NULL;
michael@0 45 PRLock *ml = NULL;
michael@0 46 PRCondVar *cv = NULL;
michael@0 47 PRThread *thread = NULL;
michael@0 48 PRIntervalTime interval = 0;
michael@0 49 PRFileDesc *file, *udp, *tcp, *pair[2];
michael@0 50 PRIntn test;
michael@0 51
michael@0 52 if ( argc < 2)
michael@0 53 {
michael@0 54 test = 0;
michael@0 55 }
michael@0 56 else
michael@0 57 test = atoi(argv[1]);
michael@0 58
michael@0 59 switch (test)
michael@0 60 {
michael@0 61 case 0: ml = PR_NewLock();
michael@0 62 break;
michael@0 63
michael@0 64 case 1: interval = PR_SecondsToInterval(1);
michael@0 65 break;
michael@0 66
michael@0 67 case 2: thread = PR_CreateThread(
michael@0 68 PR_USER_THREAD, lazyEntry, NULL, PR_PRIORITY_NORMAL,
michael@0 69 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 70 break;
michael@0 71
michael@0 72 case 3: file = PR_Open("/usr/tmp/", PR_RDONLY, 0);
michael@0 73 break;
michael@0 74
michael@0 75 case 4: udp = PR_NewUDPSocket();
michael@0 76 break;
michael@0 77
michael@0 78 case 5: tcp = PR_NewTCPSocket();
michael@0 79 break;
michael@0 80
michael@0 81 case 6: dir = PR_OpenDir("/usr/tmp/");
michael@0 82 break;
michael@0 83
michael@0 84 case 7: (void)PR_NewThreadPrivateIndex(&pdkey, NULL);
michael@0 85 break;
michael@0 86
michael@0 87 case 8: path = PR_GetEnv("PATH");
michael@0 88 break;
michael@0 89
michael@0 90 case 9: status = PR_NewTCPSocketPair(pair);
michael@0 91 break;
michael@0 92
michael@0 93 case 10: PR_SetConcurrency(2);
michael@0 94 break;
michael@0 95
michael@0 96 default:
michael@0 97 printf(
michael@0 98 "lazyinit: unrecognized command line argument: %s\n",
michael@0 99 argv[1] );
michael@0 100 printf( "FAIL\n" );
michael@0 101 exit( 1 );
michael@0 102 break;
michael@0 103 } /* switch() */
michael@0 104 return 0;
michael@0 105 } /* Lazy */
michael@0 106
michael@0 107 /* lazyinit.c */

mercurial