1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
12 #define SEM_NAME1 "c:\\data\\foo.sem"
13 #define SEM_NAME2 "c:\\data\\bar.sem"
15 #define SEM_NAME1 "/tmp/foo.sem"
16 #define SEM_NAME2 "/tmp/bar.sem"
20 static PRBool debug_mode
= PR_FALSE
;
22 static void Help(void)
24 fprintf(stderr
, "semaerr1 test program usage:\n");
25 fprintf(stderr
, "\t-d debug mode (FALSE)\n");
26 fprintf(stderr
, "\t-h this message\n");
29 int main(int argc
, char **argv
)
32 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dh");
35 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
))) {
36 if (PL_OPT_BAD
== os
) continue;
37 switch (opt
->option
) {
38 case 'd': /* debug mode */
47 PL_DestroyOptState(opt
);
50 * PR_SEM_CREATE|PR_SEM_EXCL should be able to
51 * create a nonexistent semaphore.
53 (void) PR_DeleteSemaphore(SEM_NAME2
);
54 sem
= PR_OpenSemaphore(SEM_NAME2
, PR_SEM_CREATE
|PR_SEM_EXCL
, SEM_MODE
, 0);
56 fprintf(stderr
, "PR_OpenSemaphore failed\n");
59 if (PR_CloseSemaphore(sem
) == PR_FAILURE
) {
60 fprintf(stderr
, "PR_CloseSemaphore failed\n");
63 if (PR_DeleteSemaphore(SEM_NAME2
) == PR_FAILURE
) {
64 fprintf(stderr
, "PR_DeleteSemaphore failed\n");
69 * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL.
70 * should fail with PR_FILE_EXISTS_ERROR.
72 sem
= PR_OpenSemaphore(SEM_NAME1
, PR_SEM_CREATE
|PR_SEM_EXCL
, SEM_MODE
, 0);
74 fprintf(stderr
, "PR_OpenSemaphore should fail but succeeded\n");
77 if (PR_GetError() != PR_FILE_EXISTS_ERROR
) {
78 fprintf(stderr
, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR
,
84 * Try again, with just PR_SEM_CREATE. This should succeed.
86 sem
= PR_OpenSemaphore(SEM_NAME1
, PR_SEM_CREATE
, SEM_MODE
, 0);
88 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
89 PR_GetError(), PR_GetOSError());
92 if (PR_CloseSemaphore(sem
) == PR_FAILURE
) {
93 fprintf(stderr
, "PR_CloseSemaphore failed\n");
97 sem
= PR_OpenSemaphore(SEM_NAME2
, PR_SEM_CREATE
|PR_SEM_EXCL
, SEM_MODE
, 0);
99 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
100 PR_GetError(), PR_GetOSError());
103 if (PR_CloseSemaphore(sem
) == PR_FAILURE
) {
104 fprintf(stderr
, "PR_CloseSemaphore failed\n");