nsprpub/pr/tests/sema.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/sema.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     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 +#define SEM_NAME1 "/tmp/foo.sem"
    1.15 +#define SEM_NAME2 "/tmp/bar.sem"
    1.16 +#define SEM_MODE  0666
    1.17 +#define ITERATIONS 1000
    1.18 +
    1.19 +static PRBool debug_mode = PR_FALSE;
    1.20 +static PRIntn iterations = ITERATIONS;
    1.21 +static PRIntn counter;
    1.22 +static PRSem *sem1, *sem2;
    1.23 +
    1.24 +/*
    1.25 + * Thread 2 waits on semaphore 2 and posts to semaphore 1.
    1.26 + */
    1.27 +void ThreadFunc(void *arg)
    1.28 +{
    1.29 +    PRIntn i;
    1.30 +
    1.31 +    for (i = 0; i < iterations; i++) {
    1.32 +        if (PR_WaitSemaphore(sem2) == PR_FAILURE) {
    1.33 +            fprintf(stderr, "PR_WaitSemaphore failed\n");
    1.34 +            exit(1);
    1.35 +        }
    1.36 +        if (counter == 2*i+1) {
    1.37 +            if (debug_mode) printf("thread 2: counter = %d\n", counter);
    1.38 +        } else {
    1.39 +            fprintf(stderr, "thread 2: counter should be %d but is %d\n",
    1.40 +                    2*i+1, counter);
    1.41 +            exit(1);
    1.42 +        }
    1.43 +        counter++;
    1.44 +        if (PR_PostSemaphore(sem1) == PR_FAILURE) {
    1.45 +            fprintf(stderr, "PR_PostSemaphore failed\n");
    1.46 +            exit(1);
    1.47 +        }
    1.48 +    }
    1.49 +}
    1.50 +
    1.51 +static void Help(void)
    1.52 +{
    1.53 +    fprintf(stderr, "sema test program usage:\n");
    1.54 +    fprintf(stderr, "\t-d           debug mode         (FALSE)\n");
    1.55 +    fprintf(stderr, "\t-c <count>   loop count         (%d)\n", ITERATIONS);
    1.56 +    fprintf(stderr, "\t-h           this message\n");
    1.57 +}  /* Help */
    1.58 +
    1.59 +int main(int argc, char **argv)
    1.60 +{
    1.61 +    PRThread *thred;
    1.62 +    PRIntn i;
    1.63 +    PLOptStatus os;
    1.64 +    PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
    1.65 +
    1.66 +    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
    1.67 +        if (PL_OPT_BAD == os) continue;
    1.68 +        switch (opt->option) {
    1.69 +            case 'd':  /* debug mode */
    1.70 +                debug_mode = PR_TRUE;
    1.71 +                break;
    1.72 +            case 'c':  /* loop count */
    1.73 +                iterations = atoi(opt->value);
    1.74 +                break;
    1.75 +            case 'h':
    1.76 +            default:
    1.77 +                Help();
    1.78 +                return 2;
    1.79 +        }
    1.80 +    }
    1.81 +    PL_DestroyOptState(opt);
    1.82 +
    1.83 +    if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
    1.84 +        fprintf(stderr, "warning: removed semaphore %s left over "
    1.85 +                "from previous run\n", SEM_NAME1);
    1.86 +    }
    1.87 +    if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
    1.88 +        fprintf(stderr, "warning: removed semaphore %s left over "
    1.89 +                "from previous run\n", SEM_NAME2);
    1.90 +    }
    1.91 +
    1.92 +    sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
    1.93 +    if (NULL == sem1) {
    1.94 +        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
    1.95 +                PR_GetError(), PR_GetOSError());
    1.96 +        exit(1);
    1.97 +    }
    1.98 +    sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
    1.99 +    if (NULL == sem2) {
   1.100 +        fprintf(stderr, "PR_OpenSemaphore failed\n");
   1.101 +        exit(1);
   1.102 +    }
   1.103 +    thred = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
   1.104 +            PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
   1.105 +    if (NULL == thred) {
   1.106 +        fprintf(stderr, "PR_CreateThread failed\n");
   1.107 +        exit(1);
   1.108 +    }
   1.109 +
   1.110 +    /*
   1.111 +     * Thread 1 waits on semaphore 1 and posts to semaphore 2.
   1.112 +     */
   1.113 +    for (i = 0; i < iterations; i++) {
   1.114 +        if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
   1.115 +            fprintf(stderr, "PR_WaitSemaphore failed\n");
   1.116 +            exit(1);
   1.117 +        }
   1.118 +        if (counter == 2*i) {
   1.119 +            if (debug_mode) printf("thread 1: counter = %d\n", counter);
   1.120 +        } else {
   1.121 +            fprintf(stderr, "thread 1: counter should be %d but is %d\n",
   1.122 +                    2*i, counter);
   1.123 +            exit(1);
   1.124 +        }
   1.125 +        counter++;
   1.126 +        if (PR_PostSemaphore(sem2) == PR_FAILURE) {
   1.127 +            fprintf(stderr, "PR_PostSemaphore failed\n");
   1.128 +            exit(1);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    if (PR_JoinThread(thred) == PR_FAILURE) {
   1.133 +        fprintf(stderr, "PR_JoinThread failed\n");
   1.134 +        exit(1);
   1.135 +    }
   1.136 +
   1.137 +    if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
   1.138 +        fprintf(stderr, "PR_CloseSemaphore failed\n");
   1.139 +    }
   1.140 +    if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
   1.141 +        fprintf(stderr, "PR_CloseSemaphore failed\n");
   1.142 +    }
   1.143 +    if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
   1.144 +        fprintf(stderr, "PR_DeleteSemaphore failed\n");
   1.145 +    }
   1.146 +    if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
   1.147 +        fprintf(stderr, "PR_DeleteSemaphore failed\n");
   1.148 +    }
   1.149 +    printf("PASS\n");
   1.150 +    return 0;
   1.151 +}

mercurial