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 | #else |
michael@0 | 16 | #define SHM_NAME "/tmp/counter" |
michael@0 | 17 | #define SEM_NAME1 "/tmp/foo.sem" |
michael@0 | 18 | #define SEM_NAME2 "/tmp/bar.sem" |
michael@0 | 19 | #endif |
michael@0 | 20 | #define ITERATIONS 1000 |
michael@0 | 21 | |
michael@0 | 22 | static PRBool debug_mode = PR_FALSE; |
michael@0 | 23 | static PRIntn iterations = ITERATIONS; |
michael@0 | 24 | static PRSem *sem1, *sem2; |
michael@0 | 25 | |
michael@0 | 26 | static void Help(void) |
michael@0 | 27 | { |
michael@0 | 28 | fprintf(stderr, "semapong test program usage:\n"); |
michael@0 | 29 | fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
michael@0 | 30 | fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS); |
michael@0 | 31 | fprintf(stderr, "\t-h this message\n"); |
michael@0 | 32 | } /* Help */ |
michael@0 | 33 | |
michael@0 | 34 | int main(int argc, char **argv) |
michael@0 | 35 | { |
michael@0 | 36 | PRIntn i; |
michael@0 | 37 | PRSharedMemory *shm; |
michael@0 | 38 | PRIntn *counter_addr; |
michael@0 | 39 | PLOptStatus os; |
michael@0 | 40 | PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h"); |
michael@0 | 41 | |
michael@0 | 42 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
michael@0 | 43 | if (PL_OPT_BAD == os) continue; |
michael@0 | 44 | switch (opt->option) { |
michael@0 | 45 | case 'd': /* debug mode */ |
michael@0 | 46 | debug_mode = PR_TRUE; |
michael@0 | 47 | break; |
michael@0 | 48 | case 'c': /* loop count */ |
michael@0 | 49 | iterations = atoi(opt->value); |
michael@0 | 50 | break; |
michael@0 | 51 | case 'h': |
michael@0 | 52 | default: |
michael@0 | 53 | Help(); |
michael@0 | 54 | return 2; |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | PL_DestroyOptState(opt); |
michael@0 | 58 | |
michael@0 | 59 | shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), 0, 0666); |
michael@0 | 60 | if (NULL == shm) { |
michael@0 | 61 | fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n", |
michael@0 | 62 | PR_GetError(), PR_GetOSError()); |
michael@0 | 63 | exit(1); |
michael@0 | 64 | } |
michael@0 | 65 | sem1 = PR_OpenSemaphore(SEM_NAME1, 0, 0, 0); |
michael@0 | 66 | if (NULL == sem1) { |
michael@0 | 67 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
michael@0 | 68 | PR_GetError(), PR_GetOSError()); |
michael@0 | 69 | exit(1); |
michael@0 | 70 | } |
michael@0 | 71 | sem2 = PR_OpenSemaphore(SEM_NAME2, 0, 0, 0); |
michael@0 | 72 | if (NULL == sem2) { |
michael@0 | 73 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
michael@0 | 74 | PR_GetError(), PR_GetOSError()); |
michael@0 | 75 | exit(1); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | counter_addr = PR_AttachSharedMemory(shm, 0); |
michael@0 | 79 | if (NULL == counter_addr) { |
michael@0 | 80 | fprintf(stderr, "PR_AttachSharedMemory failed\n"); |
michael@0 | 81 | exit(1); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /* |
michael@0 | 85 | * Process 2 waits on semaphore 2 and posts to semaphore 1. |
michael@0 | 86 | */ |
michael@0 | 87 | for (i = 0; i < iterations; i++) { |
michael@0 | 88 | if (PR_WaitSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 89 | fprintf(stderr, "PR_WaitSemaphore failed\n"); |
michael@0 | 90 | exit(1); |
michael@0 | 91 | } |
michael@0 | 92 | if (*counter_addr == 2*i+1) { |
michael@0 | 93 | if (debug_mode) printf("process 2: counter = %d\n", *counter_addr); |
michael@0 | 94 | } else { |
michael@0 | 95 | fprintf(stderr, "process 2: counter should be %d but is %d\n", |
michael@0 | 96 | 2*i+1, *counter_addr); |
michael@0 | 97 | exit(1); |
michael@0 | 98 | } |
michael@0 | 99 | (*counter_addr)++; |
michael@0 | 100 | if (PR_PostSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 101 | fprintf(stderr, "PR_PostSemaphore failed\n"); |
michael@0 | 102 | exit(1); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) { |
michael@0 | 106 | fprintf(stderr, "PR_DetachSharedMemory failed\n"); |
michael@0 | 107 | exit(1); |
michael@0 | 108 | } |
michael@0 | 109 | if (PR_CloseSharedMemory(shm) == PR_FAILURE) { |
michael@0 | 110 | fprintf(stderr, "PR_CloseSharedMemory failed\n"); |
michael@0 | 111 | exit(1); |
michael@0 | 112 | } |
michael@0 | 113 | if (PR_CloseSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 114 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 115 | } |
michael@0 | 116 | if (PR_CloseSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 117 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 118 | } |
michael@0 | 119 | printf("PASS\n"); |
michael@0 | 120 | return 0; |
michael@0 | 121 | } |