nsprpub/pr/tests/semapong.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/semapong.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nspr.h"
    1.10 +#include "plgetopt.h"
    1.11 +
    1.12 +#include <stdio.h>
    1.13 +
    1.14 +#ifdef SYMBIAN
    1.15 +#define SHM_NAME "c:\\data\\counter"
    1.16 +#define SEM_NAME1 "c:\\data\\foo.sem"
    1.17 +#define SEM_NAME2 "c:\\data\\bar.sem"
    1.18 +#else
    1.19 +#define SHM_NAME "/tmp/counter"
    1.20 +#define SEM_NAME1 "/tmp/foo.sem"
    1.21 +#define SEM_NAME2 "/tmp/bar.sem"
    1.22 +#endif
    1.23 +#define ITERATIONS 1000
    1.24 +
    1.25 +static PRBool debug_mode = PR_FALSE;
    1.26 +static PRIntn iterations = ITERATIONS;
    1.27 +static PRSem *sem1, *sem2;
    1.28 +
    1.29 +static void Help(void)
    1.30 +{
    1.31 +    fprintf(stderr, "semapong test program usage:\n");
    1.32 +    fprintf(stderr, "\t-d           debug mode         (FALSE)\n");
    1.33 +    fprintf(stderr, "\t-c <count>   loop count         (%d)\n", ITERATIONS);
    1.34 +    fprintf(stderr, "\t-h           this message\n");
    1.35 +}  /* Help */
    1.36 +
    1.37 +int main(int argc, char **argv)
    1.38 +{
    1.39 +    PRIntn i;
    1.40 +    PRSharedMemory *shm;
    1.41 +    PRIntn *counter_addr;
    1.42 +    PLOptStatus os;
    1.43 +    PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
    1.44 +
    1.45 +    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
    1.46 +        if (PL_OPT_BAD == os) continue;
    1.47 +        switch (opt->option) {
    1.48 +            case 'd':  /* debug mode */
    1.49 +                debug_mode = PR_TRUE;
    1.50 +                break;
    1.51 +            case 'c':  /* loop count */
    1.52 +                iterations = atoi(opt->value);
    1.53 +                break;
    1.54 +            case 'h':
    1.55 +            default:
    1.56 +                Help();
    1.57 +                return 2;
    1.58 +        }
    1.59 +    }
    1.60 +    PL_DestroyOptState(opt);
    1.61 +
    1.62 +    shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), 0, 0666);
    1.63 +    if (NULL == shm) {
    1.64 +        fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
    1.65 +                PR_GetError(), PR_GetOSError());
    1.66 +        exit(1);
    1.67 +    }
    1.68 +    sem1 = PR_OpenSemaphore(SEM_NAME1, 0, 0, 0);
    1.69 +    if (NULL == sem1) {
    1.70 +        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
    1.71 +                PR_GetError(), PR_GetOSError());
    1.72 +        exit(1);
    1.73 +    }
    1.74 +    sem2 = PR_OpenSemaphore(SEM_NAME2, 0, 0, 0);
    1.75 +    if (NULL == sem2) {
    1.76 +        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
    1.77 +                PR_GetError(), PR_GetOSError());
    1.78 +        exit(1);
    1.79 +    }
    1.80 +
    1.81 +    counter_addr = PR_AttachSharedMemory(shm, 0);
    1.82 +    if (NULL == counter_addr) {
    1.83 +        fprintf(stderr, "PR_AttachSharedMemory failed\n");
    1.84 +        exit(1);
    1.85 +    }
    1.86 +
    1.87 +    /*
    1.88 +     * Process 2 waits on semaphore 2 and posts to semaphore 1.
    1.89 +     */
    1.90 +    for (i = 0; i < iterations; i++) {
    1.91 +        if (PR_WaitSemaphore(sem2) == PR_FAILURE) {
    1.92 +            fprintf(stderr, "PR_WaitSemaphore failed\n");
    1.93 +            exit(1);
    1.94 +        }
    1.95 +        if (*counter_addr == 2*i+1) {
    1.96 +            if (debug_mode) printf("process 2: counter = %d\n", *counter_addr);
    1.97 +        } else {
    1.98 +            fprintf(stderr, "process 2: counter should be %d but is %d\n",
    1.99 +                    2*i+1, *counter_addr);
   1.100 +            exit(1);
   1.101 +        }
   1.102 +        (*counter_addr)++;
   1.103 +        if (PR_PostSemaphore(sem1) == PR_FAILURE) {
   1.104 +            fprintf(stderr, "PR_PostSemaphore failed\n");
   1.105 +            exit(1);
   1.106 +        }
   1.107 +    }
   1.108 +    if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
   1.109 +        fprintf(stderr, "PR_DetachSharedMemory failed\n");
   1.110 +        exit(1);
   1.111 +    }
   1.112 +    if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
   1.113 +        fprintf(stderr, "PR_CloseSharedMemory failed\n");
   1.114 +        exit(1);
   1.115 +    }
   1.116 +    if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
   1.117 +        fprintf(stderr, "PR_CloseSemaphore failed\n");
   1.118 +    }
   1.119 +    if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
   1.120 +        fprintf(stderr, "PR_CloseSemaphore failed\n");
   1.121 +    }
   1.122 +    printf("PASS\n");
   1.123 +    return 0;
   1.124 +}

mercurial