Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
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: concur.c |
michael@0 | 8 | ** Description: test of adding and removing concurrency options |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "prcvar.h" |
michael@0 | 12 | #include "prinit.h" |
michael@0 | 13 | #include "prinrval.h" |
michael@0 | 14 | #include "prlock.h" |
michael@0 | 15 | #include "prprf.h" |
michael@0 | 16 | #include "prmem.h" |
michael@0 | 17 | #include "prlog.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "plgetopt.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "private/pprio.h" |
michael@0 | 22 | |
michael@0 | 23 | #include <stdlib.h> |
michael@0 | 24 | |
michael@0 | 25 | #define DEFAULT_RANGE 10 |
michael@0 | 26 | #define DEFAULT_LOOPS 100 |
michael@0 | 27 | |
michael@0 | 28 | static PRThreadScope thread_scope = PR_LOCAL_THREAD; |
michael@0 | 29 | |
michael@0 | 30 | typedef struct Context |
michael@0 | 31 | { |
michael@0 | 32 | PRLock *ml; |
michael@0 | 33 | PRCondVar *cv; |
michael@0 | 34 | PRIntn want, have; |
michael@0 | 35 | } Context; |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | /* |
michael@0 | 39 | ** Make the instance of 'context' static (not on the stack) |
michael@0 | 40 | ** for Win16 threads |
michael@0 | 41 | */ |
michael@0 | 42 | static Context context = {NULL, NULL, 0, 0}; |
michael@0 | 43 | |
michael@0 | 44 | static void PR_CALLBACK Dull(void *arg) |
michael@0 | 45 | { |
michael@0 | 46 | Context *context = (Context*)arg; |
michael@0 | 47 | PR_Lock(context->ml); |
michael@0 | 48 | context->have += 1; |
michael@0 | 49 | while (context->want >= context->have) |
michael@0 | 50 | PR_WaitCondVar(context->cv, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 51 | context->have -= 1; |
michael@0 | 52 | PR_Unlock(context->ml); |
michael@0 | 53 | } /* Dull */ |
michael@0 | 54 | |
michael@0 | 55 | PRIntn PR_CALLBACK Concur(PRIntn argc, char **argv) |
michael@0 | 56 | { |
michael@0 | 57 | PRUintn cpus; |
michael@0 | 58 | PLOptStatus os; |
michael@0 | 59 | PRThread **threads; |
michael@0 | 60 | PRBool debug = PR_FALSE; |
michael@0 | 61 | PRUintn range = DEFAULT_RANGE; |
michael@0 | 62 | PRStatus rc; |
michael@0 | 63 | PRUintn cnt; |
michael@0 | 64 | PRUintn loops = DEFAULT_LOOPS; |
michael@0 | 65 | PRIntervalTime hundredMills = PR_MillisecondsToInterval(100); |
michael@0 | 66 | PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:r:"); |
michael@0 | 67 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 68 | { |
michael@0 | 69 | if (PL_OPT_BAD == os) continue; |
michael@0 | 70 | switch (opt->option) |
michael@0 | 71 | { |
michael@0 | 72 | case 'G': /* GLOBAL threads */ |
michael@0 | 73 | thread_scope = PR_GLOBAL_THREAD; |
michael@0 | 74 | break; |
michael@0 | 75 | case 'd': /* debug mode */ |
michael@0 | 76 | debug = PR_TRUE; |
michael@0 | 77 | break; |
michael@0 | 78 | case 'r': /* range limit */ |
michael@0 | 79 | range = atoi(opt->value); |
michael@0 | 80 | break; |
michael@0 | 81 | case 'l': /* loop counter */ |
michael@0 | 82 | loops = atoi(opt->value); |
michael@0 | 83 | break; |
michael@0 | 84 | default: |
michael@0 | 85 | break; |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | PL_DestroyOptState(opt); |
michael@0 | 89 | |
michael@0 | 90 | if (0 == range) range = DEFAULT_RANGE; |
michael@0 | 91 | if (0 == loops) loops = DEFAULT_LOOPS; |
michael@0 | 92 | |
michael@0 | 93 | context.ml = PR_NewLock(); |
michael@0 | 94 | context.cv = PR_NewCondVar(context.ml); |
michael@0 | 95 | |
michael@0 | 96 | if (debug) |
michael@0 | 97 | PR_fprintf( |
michael@0 | 98 | PR_STDERR, "Testing with %d CPUs and %d interations\n", range, loops); |
michael@0 | 99 | |
michael@0 | 100 | threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * range); |
michael@0 | 101 | while (--loops > 0) |
michael@0 | 102 | { |
michael@0 | 103 | for (cpus = 1; cpus <= range; ++cpus) |
michael@0 | 104 | { |
michael@0 | 105 | PR_SetConcurrency(cpus); |
michael@0 | 106 | context.want = cpus; |
michael@0 | 107 | |
michael@0 | 108 | threads[cpus - 1] = PR_CreateThread( |
michael@0 | 109 | PR_USER_THREAD, Dull, &context, PR_PRIORITY_NORMAL, |
michael@0 | 110 | thread_scope, PR_JOINABLE_THREAD, 0); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | PR_Sleep(hundredMills); |
michael@0 | 114 | |
michael@0 | 115 | for (cpus = range; cpus > 0; cpus--) |
michael@0 | 116 | { |
michael@0 | 117 | PR_SetConcurrency(cpus); |
michael@0 | 118 | context.want = cpus - 1; |
michael@0 | 119 | |
michael@0 | 120 | PR_Lock(context.ml); |
michael@0 | 121 | PR_NotifyCondVar(context.cv); |
michael@0 | 122 | PR_Unlock(context.ml); |
michael@0 | 123 | } |
michael@0 | 124 | for(cnt = 0; cnt < range; cnt++) { |
michael@0 | 125 | rc = PR_JoinThread(threads[cnt]); |
michael@0 | 126 | PR_ASSERT(rc == PR_SUCCESS); |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | |
michael@0 | 131 | if (debug) |
michael@0 | 132 | PR_fprintf( |
michael@0 | 133 | PR_STDERR, "Waiting for %d thread(s) to exit\n", context.have); |
michael@0 | 134 | |
michael@0 | 135 | while (context.have > 0) PR_Sleep(hundredMills); |
michael@0 | 136 | |
michael@0 | 137 | if (debug) |
michael@0 | 138 | PR_fprintf( |
michael@0 | 139 | PR_STDERR, "Finished [want: %d, have: %d]\n", |
michael@0 | 140 | context.want, context.have); |
michael@0 | 141 | |
michael@0 | 142 | PR_DestroyLock(context.ml); |
michael@0 | 143 | PR_DestroyCondVar(context.cv); |
michael@0 | 144 | PR_DELETE(threads); |
michael@0 | 145 | |
michael@0 | 146 | PR_fprintf(PR_STDERR, "PASSED\n"); |
michael@0 | 147 | |
michael@0 | 148 | return 0; |
michael@0 | 149 | } /* Concur */ |
michael@0 | 150 | |
michael@0 | 151 | int main(int argc, char **argv) |
michael@0 | 152 | { |
michael@0 | 153 | PR_STDIO_INIT(); |
michael@0 | 154 | return PR_Initialize(Concur, argc, argv, 0); |
michael@0 | 155 | } /* main */ |
michael@0 | 156 | |
michael@0 | 157 | /* concur.c */ |