michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nspr.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef SYMBIAN michael@0: #define SHM_NAME "c:\\data\\counter" michael@0: #define SEM_NAME1 "c:\\data\\foo.sem" michael@0: #define SEM_NAME2 "c:\\data\\bar.sem" michael@0: #define EXE_NAME "nspr_tests_semapong.exe" michael@0: #else michael@0: #define SHM_NAME "/tmp/counter" michael@0: #define SEM_NAME1 "/tmp/foo.sem" michael@0: #define SEM_NAME2 "/tmp/bar.sem" michael@0: #define EXE_NAME "semapong" michael@0: #endif michael@0: #define SEM_MODE 0666 michael@0: #define SHM_MODE 0666 michael@0: #define ITERATIONS 1000 michael@0: michael@0: static PRBool debug_mode = PR_FALSE; michael@0: static PRIntn iterations = ITERATIONS; michael@0: static PRSem *sem1, *sem2; michael@0: michael@0: static void Help(void) michael@0: { michael@0: fprintf(stderr, "semaping test program usage:\n"); michael@0: fprintf(stderr, "\t-d debug mode (FALSE)\n"); michael@0: fprintf(stderr, "\t-c loop count (%d)\n", ITERATIONS); michael@0: fprintf(stderr, "\t-h this message\n"); michael@0: } /* Help */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRProcess *proc; michael@0: PRIntn i; michael@0: char *child_argv[32]; michael@0: char **child_arg; michael@0: char iterations_buf[32]; michael@0: PRSharedMemory *shm; michael@0: PRIntn *counter_addr; michael@0: PRInt32 exit_code; michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = PR_TRUE; michael@0: break; michael@0: case 'c': /* loop count */ michael@0: iterations = atoi(opt->value); michael@0: break; michael@0: case 'h': michael@0: default: michael@0: Help(); michael@0: return 2; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) { michael@0: fprintf(stderr, "warning: removed shared memory %s left over " michael@0: "from previous run\n", SHM_NAME); michael@0: } michael@0: if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) { michael@0: fprintf(stderr, "warning: removed semaphore %s left over " michael@0: "from previous run\n", SEM_NAME1); michael@0: } michael@0: if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) { michael@0: fprintf(stderr, "warning: removed semaphore %s left over " michael@0: "from previous run\n", SEM_NAME2); michael@0: } michael@0: michael@0: shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE); michael@0: if (NULL == shm) { michael@0: fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: counter_addr = PR_AttachSharedMemory(shm, 0); michael@0: if (NULL == counter_addr) { michael@0: fprintf(stderr, "PR_AttachSharedMemory failed\n"); michael@0: exit(1); michael@0: } michael@0: *counter_addr = 0; michael@0: sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1); michael@0: if (NULL == sem1) { michael@0: fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0); michael@0: if (NULL == sem2) { michael@0: fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: michael@0: child_arg = &child_argv[0]; michael@0: *child_arg++ = EXE_NAME; michael@0: if (debug_mode != PR_FALSE) { michael@0: *child_arg++ = "-d"; michael@0: } michael@0: if (iterations != ITERATIONS) { michael@0: *child_arg++ = "-c"; michael@0: PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations); michael@0: *child_arg++ = iterations_buf; michael@0: } michael@0: *child_arg = NULL; michael@0: proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); michael@0: if (NULL == proc) { michael@0: fprintf(stderr, "PR_CreateProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* michael@0: * Process 1 waits on semaphore 1 and posts to semaphore 2. michael@0: */ michael@0: for (i = 0; i < iterations; i++) { michael@0: if (PR_WaitSemaphore(sem1) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_WaitSemaphore failed\n"); michael@0: exit(1); michael@0: } michael@0: if (*counter_addr == 2*i) { michael@0: if (debug_mode) printf("process 1: counter = %d\n", *counter_addr); michael@0: } else { michael@0: fprintf(stderr, "process 1: counter should be %d but is %d\n", michael@0: 2*i, *counter_addr); michael@0: exit(1); michael@0: } michael@0: (*counter_addr)++; michael@0: if (PR_PostSemaphore(sem2) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_PostSemaphore failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_DetachSharedMemory failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_CloseSharedMemory(shm) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CloseSharedMemory failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_CloseSemaphore(sem1) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CloseSemaphore failed\n"); michael@0: } michael@0: if (PR_CloseSemaphore(sem2) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CloseSemaphore failed\n"); michael@0: } michael@0: michael@0: if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_WaitProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: if (exit_code != 0) { michael@0: fprintf(stderr, "process 2 failed with exit code %d\n", exit_code); michael@0: exit(1); michael@0: } michael@0: michael@0: if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_DeleteSharedMemory failed\n"); michael@0: } michael@0: if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_DeleteSemaphore failed\n"); michael@0: } michael@0: if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_DeleteSemaphore failed\n"); michael@0: } michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }