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 | #define SEM_NAME1 "/tmp/foo.sem" |
michael@0 | 12 | #define SEM_NAME2 "/tmp/bar.sem" |
michael@0 | 13 | #define SEM_MODE 0666 |
michael@0 | 14 | #define ITERATIONS 1000 |
michael@0 | 15 | |
michael@0 | 16 | static PRBool debug_mode = PR_FALSE; |
michael@0 | 17 | static PRIntn iterations = ITERATIONS; |
michael@0 | 18 | static PRIntn counter; |
michael@0 | 19 | static PRSem *sem1, *sem2; |
michael@0 | 20 | |
michael@0 | 21 | /* |
michael@0 | 22 | * Thread 2 waits on semaphore 2 and posts to semaphore 1. |
michael@0 | 23 | */ |
michael@0 | 24 | void ThreadFunc(void *arg) |
michael@0 | 25 | { |
michael@0 | 26 | PRIntn i; |
michael@0 | 27 | |
michael@0 | 28 | for (i = 0; i < iterations; i++) { |
michael@0 | 29 | if (PR_WaitSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 30 | fprintf(stderr, "PR_WaitSemaphore failed\n"); |
michael@0 | 31 | exit(1); |
michael@0 | 32 | } |
michael@0 | 33 | if (counter == 2*i+1) { |
michael@0 | 34 | if (debug_mode) printf("thread 2: counter = %d\n", counter); |
michael@0 | 35 | } else { |
michael@0 | 36 | fprintf(stderr, "thread 2: counter should be %d but is %d\n", |
michael@0 | 37 | 2*i+1, counter); |
michael@0 | 38 | exit(1); |
michael@0 | 39 | } |
michael@0 | 40 | counter++; |
michael@0 | 41 | if (PR_PostSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 42 | fprintf(stderr, "PR_PostSemaphore failed\n"); |
michael@0 | 43 | exit(1); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | static void Help(void) |
michael@0 | 49 | { |
michael@0 | 50 | fprintf(stderr, "sema test program usage:\n"); |
michael@0 | 51 | fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
michael@0 | 52 | fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS); |
michael@0 | 53 | fprintf(stderr, "\t-h this message\n"); |
michael@0 | 54 | } /* Help */ |
michael@0 | 55 | |
michael@0 | 56 | int main(int argc, char **argv) |
michael@0 | 57 | { |
michael@0 | 58 | PRThread *thred; |
michael@0 | 59 | PRIntn i; |
michael@0 | 60 | PLOptStatus os; |
michael@0 | 61 | PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h"); |
michael@0 | 62 | |
michael@0 | 63 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
michael@0 | 64 | if (PL_OPT_BAD == os) continue; |
michael@0 | 65 | switch (opt->option) { |
michael@0 | 66 | case 'd': /* debug mode */ |
michael@0 | 67 | debug_mode = PR_TRUE; |
michael@0 | 68 | break; |
michael@0 | 69 | case 'c': /* loop count */ |
michael@0 | 70 | iterations = atoi(opt->value); |
michael@0 | 71 | break; |
michael@0 | 72 | case 'h': |
michael@0 | 73 | default: |
michael@0 | 74 | Help(); |
michael@0 | 75 | return 2; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | PL_DestroyOptState(opt); |
michael@0 | 79 | |
michael@0 | 80 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) { |
michael@0 | 81 | fprintf(stderr, "warning: removed semaphore %s left over " |
michael@0 | 82 | "from previous run\n", SEM_NAME1); |
michael@0 | 83 | } |
michael@0 | 84 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) { |
michael@0 | 85 | fprintf(stderr, "warning: removed semaphore %s left over " |
michael@0 | 86 | "from previous run\n", SEM_NAME2); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1); |
michael@0 | 90 | if (NULL == sem1) { |
michael@0 | 91 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
michael@0 | 92 | PR_GetError(), PR_GetOSError()); |
michael@0 | 93 | exit(1); |
michael@0 | 94 | } |
michael@0 | 95 | sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0); |
michael@0 | 96 | if (NULL == sem2) { |
michael@0 | 97 | fprintf(stderr, "PR_OpenSemaphore failed\n"); |
michael@0 | 98 | exit(1); |
michael@0 | 99 | } |
michael@0 | 100 | thred = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, |
michael@0 | 101 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); |
michael@0 | 102 | if (NULL == thred) { |
michael@0 | 103 | fprintf(stderr, "PR_CreateThread failed\n"); |
michael@0 | 104 | exit(1); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | * Thread 1 waits on semaphore 1 and posts to semaphore 2. |
michael@0 | 109 | */ |
michael@0 | 110 | for (i = 0; i < iterations; i++) { |
michael@0 | 111 | if (PR_WaitSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 112 | fprintf(stderr, "PR_WaitSemaphore failed\n"); |
michael@0 | 113 | exit(1); |
michael@0 | 114 | } |
michael@0 | 115 | if (counter == 2*i) { |
michael@0 | 116 | if (debug_mode) printf("thread 1: counter = %d\n", counter); |
michael@0 | 117 | } else { |
michael@0 | 118 | fprintf(stderr, "thread 1: counter should be %d but is %d\n", |
michael@0 | 119 | 2*i, counter); |
michael@0 | 120 | exit(1); |
michael@0 | 121 | } |
michael@0 | 122 | counter++; |
michael@0 | 123 | if (PR_PostSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 124 | fprintf(stderr, "PR_PostSemaphore failed\n"); |
michael@0 | 125 | exit(1); |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | if (PR_JoinThread(thred) == PR_FAILURE) { |
michael@0 | 130 | fprintf(stderr, "PR_JoinThread failed\n"); |
michael@0 | 131 | exit(1); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | if (PR_CloseSemaphore(sem1) == PR_FAILURE) { |
michael@0 | 135 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 136 | } |
michael@0 | 137 | if (PR_CloseSemaphore(sem2) == PR_FAILURE) { |
michael@0 | 138 | fprintf(stderr, "PR_CloseSemaphore failed\n"); |
michael@0 | 139 | } |
michael@0 | 140 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) { |
michael@0 | 141 | fprintf(stderr, "PR_DeleteSemaphore failed\n"); |
michael@0 | 142 | } |
michael@0 | 143 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) { |
michael@0 | 144 | fprintf(stderr, "PR_DeleteSemaphore failed\n"); |
michael@0 | 145 | } |
michael@0 | 146 | printf("PASS\n"); |
michael@0 | 147 | return 0; |
michael@0 | 148 | } |