Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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 | #include "nspr.h" |
michael@0 | 7 | #include "plgetopt.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <stdio.h> |
michael@0 | 10 | |
michael@0 | 11 | #ifdef SYMBIAN |
michael@0 | 12 | #define SHM_NAME "c:\\data\\counter" |
michael@0 | 13 | #define SEM_NAME1 "c:\\data\\foo.sem" |
michael@0 | 14 | #define SEM_NAME2 "c:\\data\\bar.sem" |
michael@0 | 15 | #define EXE_NAME "nspr_tests_semapong.exe" |
michael@0 | 16 | #else |
michael@0 | 17 | #define SHM_NAME "/tmp/counter" |
michael@0 | 18 | #define SEM_NAME1 "/tmp/foo.sem" |
michael@0 | 19 | #define SEM_NAME2 "/tmp/bar.sem" |
michael@0 | 20 | #define EXE_NAME "semapong" |
michael@0 | 21 | #endif |
michael@0 | 22 | #define SEM_MODE 0666 |
michael@0 | 23 | #define SHM_MODE 0666 |
michael@0 | 24 | #define ITERATIONS 1000 |
michael@0 | 25 | |
michael@0 | 26 | static PRBool debug_mode = PR_FALSE; |
michael@0 | 27 | static PRIntn iterations = ITERATIONS; |
michael@0 | 28 | static PRSem *sem1, *sem2; |
michael@0 | 29 | |
michael@0 | 30 | static void Help(void) |
michael@0 | 31 | { |
michael@0 | 32 | fprintf(stderr, "semaping test program usage:\n"); |
michael@0 | 33 | fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
michael@0 | 34 | fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS); |
michael@0 | 35 | fprintf(stderr, "\t-h this message\n"); |
michael@0 | 36 | } /* Help */ |
michael@0 | 37 | |
michael@0 | 38 | int main(int argc, char **argv) |
michael@0 | 39 | { |
michael@0 | 40 | PRProcess *proc; |
michael@0 | 41 | PRIntn i; |
michael@0 | 42 | char *child_argv[32]; |
michael@0 | 43 | char **child_arg; |
michael@0 | 44 | char iterations_buf[32]; |
michael@0 | 45 | PRSharedMemory *shm; |
michael@0 | 46 | PRIntn *counter_addr; |
michael@0 | 47 | PRInt32 exit_code; |
michael@0 | 48 | PLOptStatus os; |
michael@0 | 49 | PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h"); |
michael@0 | 50 | |
michael@0 | 51 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
michael@0 | 52 | if (PL_OPT_BAD == os) continue; |
michael@0 | 53 | switch (opt->option) { |
michael@0 | 54 | case 'd': /* debug mode */ |
michael@0 | 55 | debug_mode = PR_TRUE; |
michael@0 | 56 | break; |
michael@0 | 57 | case 'c': /* loop count */ |
michael@0 | 58 | iterations = atoi(opt->value); |
michael@0 | 59 | break; |
michael@0 | 60 | case 'h': |
michael@0 | 61 | default: |
michael@0 | 62 | Help(); |
michael@0 | 63 | return 2; |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | PL_DestroyOptState(opt); |
michael@0 | 67 | |
michael@0 | 68 | if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) { |
michael@0 | 69 | fprintf(stderr, "warning: removed shared memory %s left over " |
michael@0 | 70 | "from previous run\n", SHM_NAME); |
michael@0 | 71 | } |
michael@0 | 72 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) { |
michael@0 | 73 | fprintf(stderr, "warning: removed semaphore %s left over " |
michael@0 | 74 | "from previous run\n", SEM_NAME1); |
michael@0 | 75 | } |
michael@0 | 76 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) { |
michael@0 | 77 | fprintf(stderr, "warning: removed semaphore %s left over " |
michael@0 | 78 | "from previous run\n", SEM_NAME2); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE); |
michael@0 | 82 | if (NULL == shm) { |
michael@0 | 83 | fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n", |
michael@0 | 84 | PR_GetError(), PR_GetOSError()); |
michael@0 | 85 | exit(1); |
michael@0 | 86 | } |
michael@0 | 87 | counter_addr = PR_AttachSharedMemory(shm, 0); |
michael@0 | 88 | if (NULL == counter_addr) { |
michael@0 | 89 | fprintf(stderr, "PR_AttachSharedMemory failed\n"); |
michael@0 | 90 | exit(1); |
michael@0 | 91 | } |
michael@0 | 92 | *counter_addr = 0; |
michael@0 | 93 | sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1); |
michael@0 | 94 | if (NULL == sem1) { |
michael@0 | 95 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
michael@0 | 96 | PR_GetError(), PR_GetOSError()); |
michael@0 | 97 | exit(1); |
michael@0 | 98 | } |
michael@0 | 99 | sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0); |
michael@0 | 100 | if (NULL == sem2) { |
michael@0 | 101 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
michael@0 | 102 | PR_GetError(), PR_GetOSError()); |
michael@0 | 103 | exit(1); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | child_arg = &child_argv[0]; |
michael@0 | 107 | *child_arg++ = EXE_NAME; |
michael@0 | 108 | if (debug_mode != PR_FALSE) { |
michael@0 | 109 | *child_arg++ = "-d"; |
michael@0 | 110 | } |
michael@0 | 111 | if (iterations != ITERATIONS) { |
michael@0 | 112 | *child_arg++ = "-c"; |
michael@0 | 113 | PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations); |
michael@0 | 114 | *child_arg++ = iterations_buf; |
michael@0 | 115 | } |
michael@0 | 116 | *child_arg = NULL; |
michael@0 | 117 | proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); |
michael@0 | 118 | if (NULL == proc) { |
michael@0 | 119 | fprintf(stderr, "PR_CreateProcess failed\n"); |
michael@0 | 120 | exit(1); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /* |
michael@0 | 124 | * Process 1 waits on semaphore 1 and posts to semaphore 2. |
michael@0 | 125 | */ |
michael@0 | 126 | for (i = 0; i < iterations; i++) { |
michael@0 | 127 | if (PR_WaitSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 128 | fprintf(stderr, "PR_WaitSemaphore failed\n"); |
michael@0 | 129 | exit(1); |
michael@0 | 130 | } |
michael@0 | 131 | if (*counter_addr == 2*i) { |
michael@0 | 132 | if (debug_mode) printf("process 1: counter = %d\n", *counter_addr); |
michael@0 | 133 | } else { |
michael@0 | 134 | fprintf(stderr, "process 1: counter should be %d but is %d\n", |
michael@0 | 135 | 2*i, *counter_addr); |
michael@0 | 136 | exit(1); |
michael@0 | 137 | } |
michael@0 | 138 | (*counter_addr)++; |
michael@0 | 139 | if (PR_PostSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 140 | fprintf(stderr, "PR_PostSemaphore failed\n"); |
michael@0 | 141 | exit(1); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) { |
michael@0 | 145 | fprintf(stderr, "PR_DetachSharedMemory failed\n"); |
michael@0 | 146 | exit(1); |
michael@0 | 147 | } |
michael@0 | 148 | if (PR_CloseSharedMemory(shm) == PR_FAILURE) { |
michael@0 | 149 | fprintf(stderr, "PR_CloseSharedMemory failed\n"); |
michael@0 | 150 | exit(1); |
michael@0 | 151 | } |
michael@0 | 152 | if (PR_CloseSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 153 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 154 | } |
michael@0 | 155 | if (PR_CloseSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 156 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) { |
michael@0 | 160 | fprintf(stderr, "PR_WaitProcess failed\n"); |
michael@0 | 161 | exit(1); |
michael@0 | 162 | } |
michael@0 | 163 | if (exit_code != 0) { |
michael@0 | 164 | fprintf(stderr, "process 2 failed with exit code %d\n", exit_code); |
michael@0 | 165 | exit(1); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) { |
michael@0 | 169 | fprintf(stderr, "PR_DeleteSharedMemory failed\n"); |
michael@0 | 170 | } |
michael@0 | 171 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) { |
michael@0 | 172 | fprintf(stderr, "PR_DeleteSemaphore failed\n"); |
michael@0 | 173 | } |
michael@0 | 174 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) { |
michael@0 | 175 | fprintf(stderr, "PR_DeleteSemaphore failed\n"); |
michael@0 | 176 | } |
michael@0 | 177 | printf("PASS\n"); |
michael@0 | 178 | return 0; |
michael@0 | 179 | } |